AP CSP
AP Computer Science Principles / All lessons / Big Idea 3: Algorithms and Programming
3Big Idea 3: Algorithms and Programming
AP CSP topic 3.4

Strings

A string is an ordered sequence of characters, such as a word or sentence. Concatenation joins strings end to end, so "data" and "base" become "database". A substring is part of a larger string, like "cat" inside "concatenate". Programs use strings to work with names, messages, passwords, and other text.

Strings vocabulary

  • String: An ordered sequence of characters, such as "hello".
  • Concatenation: Joining two or more strings end to end.
  • Substring: A part of a string, taken from inside a larger string.
  • Length: The number of characters in a string.

AP-style practice questions

Try each one, then open the answer. To save your progress, take this quiz in the interactive course.

Skill: Code Analysis
first ← "Ada"
last ← "Lovelace"
full ← concat(first, concat(" ", last))

1. Assume concat joins two strings end to end. What is the value of full?

  1. (A) "AdaLovelace"
  2. (B) "Ada Lovelace"
  3. (C) "Lovelace Ada"
  4. (D) "Ada concat Lovelace"
Show answer

(B) "Ada Lovelace" A space is joined to "Lovelace", then "Ada" is joined in front.

Skill: Abstraction in Program Development

2. Which of the following is a substring of "keyboard"?

  1. (A) "bored"
  2. (B) "key"
  3. (C) "yeb"
  4. (D) "keys"
Show answer

(B) "key" "key" appears in order inside "keyboard".

Skill: Algorithms and Program Development

3. A program checks that a password has at least 12 characters. Which string property does it need?

  1. (A) Its first character
  2. (B) Its length
  3. (C) Its last character
  4. (D) Whether it contains spaces
Show answer

(B) Its length The length of a string is the number of characters it contains.

Skill: Code Analysis

4. What is the result of concatenating "5" and "3" as strings?

  1. (A) 8
  2. (B) "8"
  3. (C) "53"
  4. (D) "35"
Show answer

(C) "53" String concatenation joins characters; it does not add numbers.

Open 3.4 in the interactive course