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?
- (A) "AdaLovelace"
- (B) "Ada Lovelace"
- (C) "Lovelace Ada"
- (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"?
- (A) "bored"
- (B) "key"
- (C) "yeb"
- (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?
- (A) Its first character
- (B) Its length
- (C) Its last character
- (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?
- (A) 8
- (B) "8"
- (C) "53"
- (D) "35"
Show answer
(C) "53" String concatenation joins characters; it does not add numbers.