What this quiz covers
This quiz focuses on Strings, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science Principles.
A program is intended to create an acronym from a three-word phrase stored in the string variables w1, w2, and w3. For example, if w1 is "Computer", w2 is "Science", and w3 is "Principles", the acronym should be "CSP". Assuming a procedure FIRSTLETTER(str) returns the first character of a string, which expression creates the correct acronym?
AP Computer Science Principles Quiz
Practice Strings in AP Computer Science Principles with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.
This quiz focuses on Strings, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science Principles.
Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.
A program is intended to create an acronym from a three-word phrase stored in the string variables w1, w2, and w3. For example, if w1 is "Computer", w2 is "Science", and w3 is "Principles", the acronym should be "CSP". Assuming a procedure FIRSTLETTER(str) returns the first character of a string, which expression creates the correct acronym?
Explanation: The expression correctly takes the first letter of each of the three string variables and concatenates them in order. For the example values, this would be "C" + "S" + "P", which results in "CSP". The other options produce incorrect results, such as concatenating the full words or adding spaces.
A username is stored in the string variable user. A program needs to display a welcome message, for example, "Welcome, user123!". Which of the following DISPLAY statements will produce the correctly formatted message?
Explanation: This statement correctly concatenates the literal string "Welcome, ", the value stored in the user variable, and the literal string "!" to form the complete message. The other options either use the literal word "user" instead of the variable or contain syntax errors.
Consider the string message which is assigned the value "AP CSP rocks!". What is the result of concatenating the first character of message with the last character of message? Assume 1-based indexing and a LENGTH procedure.
Explanation: The first character of the string "AP CSP rocks!" is "A". The last character is "!". Concatenating the first character with the last character results in the string "A!".
Consider the following code segment. The procedure LENGTH(str) returns the number of characters in the string str.
word ← "racecar" len ← LENGTH(word) firstHalf ← SUBSTRING(word, 1, 3) secondHalf ← SUBSTRING(word, 5, 3)
Which of the following expressions would evaluate to the string "racecar"?
Explanation: The variable firstHalf is "rac" and secondHalf is "car". The original word is "racecar". To reconstruct it, the middle character, 'e', which is at index 4, needs to be placed between the two halves. SUBSTRING(word, 4, 1) correctly extracts this character. Concatenating firstHalf, the middle character, and secondHalf in that order reconstructs the original word.
A string is an ordered sequence of characters. A program needs to determine if a given password string is valid. One rule is that the password must be at least 8 characters long. Which of the following conditions can be used to check if password meets this requirement? Assume a procedure LENGTH(str) that returns the number of characters in a string.
Explanation: The LENGTH procedure returns the number of characters in the string. The phrase "at least 8 characters long" means the length can be 8 or more. The condition LENGTH(password) ≥ 8 correctly evaluates to true if the password's length is 8 or greater.
A programmer is creating a program to format phone numbers. The program uses a string variable named pNum which holds ten digits, such as "1234567890". The programmer wants to display the number in the format (123) 456-7890. Which of the following expressions correctly formats the string? Assume SUBSTRING(str, start, length) is available.
Explanation: This expression correctly uses concatenation and the SUBSTRING procedure to build the formatted phone number. It correctly extracts the area code (first 3 digits), the prefix (next 3 digits), and the line number (last 4 digits) and places the parentheses, space, and hyphen in the correct positions.
A program contains two string variables, prefix and suffix. The variable prefix is assigned the value "compu" and the variable suffix is assigned the value "ter". Which of the following expressions evaluates to the string "computer"?
Explanation: The + operator, when used with strings, performs concatenation, which joins strings end-to-end. Concatenating "compu" and "ter" results in "computer". Distractor B reverses the order. Distractor C adds an extra space. Distractor D concatenates the literal words "prefix" and "suffix", not the values of the variables.
The procedure SUBSTRING(aString, start, length) returns a portion of aString. The first character of aString is at index 1. The start parameter is the starting index and the length parameter is the number of characters to include. What is displayed by the statement DISPLAY(SUBSTRING("innovation", 3, 4))?
Explanation: The substring starts at the 3rd character of "innovation", which is 'n'. It includes 4 characters from that position. The characters at indices 3, 4, 5, and 6 are 'n', 'o', 'v', and 'a'. Therefore, the result is "nova".
Consider the following code segment where word1, word2, and word3 are string variables.
word1 ← "apple" word2 ← "sauce" word3 ← word1 + word2 word1 ← word2 word2 ← word3
What are the final values of word1 and word2 after this code segment executes?
word1 is "sauce", and word2 is "applesauce" (correct answer)word1 is "apple", and word2 is "sauce"word1 is "applesauce", and word2 is "sauce"word1 is "sauce", and word2 is "sauce"Explanation: Initially, word1 is "apple" and word2 is "sauce". The third line sets word3 to "applesauce". The fourth line then changes word1 to the current value of word2, which is "sauce". Finally, the fifth line changes word2 to the current value of word3, which is "applesauce".
A programmer has a string variable data assigned the value "user-id:12345". They want to extract the ID number "12345". Assume SUBSTRING(str, start) returns the portion of str from the 1-based index start to the end of the string. The colon is always the 9th character. Which expression will extract the ID?
Explanation: The colon : is the 9th character in the string. The ID number "12345" begins at the 10th character. The expression SUBSTRING(data, 10) correctly extracts the portion of the string from the 10th character to the end, which is the desired ID.
Consider the following code segment. The procedure CONCAT(s1, s2) concatenates strings s1 and s2.
strA ← "rock" strB ← "paper" strC ← "scissors"
result ← CONCAT(strA, CONCAT(strC, strB))
What is the final value of the result variable?
Explanation: The expression is evaluated from the inside out. First, CONCAT(strC, strB) is evaluated, which concatenates "scissors" and "paper" to produce "scissorspaper". Then, the outer CONCAT is evaluated: CONCAT(strA, "scissorspaper"), which concatenates "rock" and "scissorspaper" to produce "rockscissorspaper".
A program uses a procedure REPLACE(original,toreplace,replacement) which finds the first occurrence of toreplace in original and replaces it with replacement. What is the result of the following call?
REPLACE("happy hippo", "pp", "b")
Explanation: The procedure finds the first occurrence of the substring "pp" in "happy hippo". This first occurrence is within the word "happy". It replaces that "pp" with "b", resulting in "haby". The rest of the string remains unchanged, yielding the final result "haby hippo".
A program needs to check if a file name, stored in the string fileName, ends with the extension ".txt". Which of the following describes a general algorithm to perform this check? Assume a LENGTH(str) procedure and a SUBSTRING(str, start, length) procedure.
Explanation: To check the last four characters of a string, we need a substring of length 4 that ends at the last character. The last character is at index LENGTH(fileName). Therefore, the substring must start at index LENGTH(fileName) - 3. Option A correctly identifies this logic. Option B checks the beginning of the string. Option C starts one character too early. Option D only checks if the file name has a length of 4.
A city code is a three-letter string, for example "ATL". A program needs to create a flight code by concatenating a two-letter airline code (e.g., "DL") with a four-digit flight number (e.g., "0824") and the city code. If airline is "DL", flightNum is "0824", and city is "ATL", which expression creates the flight code "DL0824ATL"?
Explanation: String concatenation joins strings in the order they appear in the expression. To get the result "DL0824ATL", the variables must be concatenated in the order airline, then flightNum, then city. The other options result in a different order or include unnecessary spaces.
A programmer wants to extract the domain name "example.com" from the email address "test@example.com". A procedure FIND(text, char) returns the 1-based index of char. A procedure SUBSTRING(text, start) returns the substring from start to the end. Which expression correctly extracts the domain name?
Explanation: The FIND procedure will return the index of the "@" symbol. The domain name begins at the character immediately following the "@". Therefore, the starting position for the substring is the index of "@" plus one. This expression correctly identifies that starting position and extracts the rest of the string.
What is a substring?
Explanation: A substring is defined as a contiguous sequence of characters within a string. Answer A describes concatenation. Answer C describes a terminator character, which is a concept used in some low-level languages but is not the definition of a substring. Answer D describes a specific type of string, not the definition of a substring.
Which of the following best describes the process of string concatenation?
Explanation: String concatenation is the operation of joining character strings end-to-end. For example, concatenating "snow" and "ball" yields "snowball". Answer A describes finding the length. Answer B describes creating a substring. Answer D describes a string comparison.
Consider the code segment below.
str ← "start" REPEAT 3 TIMES { str ← str + "-middle" } str ← str + "-end"
What is the final value of the string variable str after the code executes?
Explanation: The variable str starts as "start". The loop executes 3 times. Each time, "-middle" is concatenated to str. After the first iteration, str is "start-middle". After the second, it is "start-middle-middle". After the third, it is "start-middle-middle-middle". Finally, after the loop, "-end" is concatenated, resulting in the final string.
Consider two string variables, s1 and s2. s1 is assigned "123" and s2 is assigned "45". What is the result of the expression s1 + s2?
Explanation: When the + operator is used with strings, it performs concatenation, not arithmetic addition. The values "123" and "45" are treated as sequences of characters. Concatenating them joins them end-to-end, resulting in the string "12345".