Home

Tutoring

Subjects

Live Classes

Study Coach

Essay Review

On-Demand Courses

Colleges

Games


Sign up

Log in

Opening subject page...

Loading your content

Practice

  • All Subjects
  • Algebra Flashcards
  • SAT Math Practice Tests
  • Math Question of the Day
  • Live Classes
  • On-Demand Courses

Varsity Tutors

  • Find a Tutor
  • Test Prep
  • Online Classes
  • K-12 Learning
  • College Search
  • VarsityTutors.com

© 2026 Varsity Tutors. All rights reserved.

← Back to quizzes

AP Computer Science Principles Quiz

AP Computer Science Principles Quiz: Strings

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.

Question 1 / 19

0 of 19 answered

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)FIRST_LETTER(str)FIRSTL​ETTER(str) returns the first character of a string, which expression creates the correct acronym?

Select an answer to continue

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.

How to use this quiz

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.

All questions

Question 1

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)FIRST_LETTER(str)FIRSTL​ETTER(str) returns the first character of a string, which expression creates the correct acronym?

  1. FIRST_LETTER(w1) + FIRST_LETTER(w2) + FIRST_LETTER(w3) (correct answer)
  2. w1 + w2 + w3
  3. FIRST_LETTER(w1 + w2 + w3)
  4. FIRST_LETTER(w1) + " " + FIRST_LETTER(w2) + " " + FIRST_LETTER(w3)

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.

Question 2

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?

  1. DISPLAY("Welcome, " + user + "!") (correct answer)
  2. DISPLAY("Welcome, user!")
  3. DISPLAY("Welcome, " + "user" + "!")
  4. DISPLAY(Welcome, + user + !)

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.

Question 3

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.

  1. "A!" (correct answer)
  2. "As"
  3. "A "
  4. "!A"

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!".

Question 4

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"?

  1. firstHalf + SUBSTRING(word, 4, 1) + secondHalf (correct answer)
  2. firstHalf + secondHalf
  3. secondHalf + SUBSTRING(word, 4, 1) + firstHalf
  4. firstHalf + SUBSTRING(word, 3, 1) + secondHalf

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.

Question 5

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.

  1. LENGTH(password) ≥ 8 (correct answer)
  2. LENGTH(password) > 8
  3. LENGTH(password) = 8
  4. password ≥ 8

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.

Question 6

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.

  1. "(" + SUBSTRING(pNum, 1, 3) + ") " + SUBSTRING(pNum, 4, 3) + "-" + SUBSTRING(pNum, 7, 4) (correct answer)
  2. SUBSTRING(pNum, 1, 3) + "-" + SUBSTRING(pNum, 4, 3) + "-" + SUBSTRING(pNum, 7, 4)
  3. "(" + SUBSTRING(pNum, 0, 3) + ") " + SUBSTRING(pNum, 3, 3) + "-" + SUBSTRING(pNum, 6, 4)
  4. "(" + SUBSTRING(pNum, 1, 3) + ")" + SUBSTRING(pNum, 4, 3) + "-" + SUBSTRING(pNum, 7, 4)

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.

Question 7

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"?

  1. prefix + suffix (correct answer)
  2. suffix + prefix
  3. prefix + " " + suffix
  4. "prefix" + "suffix"

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.

Question 8

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))?

  1. "nova" (correct answer)
  2. "nov"
  3. "nnov"
  4. "ovat"

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".

Question 9

What is a substring?

  1. A procedure that joins two or more strings together end-to-end to create a new, longer string.
  2. A part or portion of an existing string, which is itself a string. (correct answer)
  3. A special character within a string that indicates the end of the string.
  4. A string that contains only numerical characters instead of alphabetical ones.

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.

Question 10

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?

  1. word1 is "sauce", and word2 is "applesauce" (correct answer)
  2. word1 is "apple", and word2 is "sauce"
  3. word1 is "applesauce", and word2 is "sauce"
  4. 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".

Question 11

Which of the following best describes the process of string concatenation?

  1. Finding the number of characters in a single string.
  2. Extracting a smaller string from within a larger string.
  3. Joining two or more strings together to form a single, new string. (correct answer)
  4. Comparing two strings to see if they contain the same characters in the same order.

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.

Question 12

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?

  1. SUBSTRING(data, 10) (correct answer)
  2. SUBSTRING(data, 9)
  3. SUBSTRING(data, 1)
  4. SUBSTRING(data, 5)

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.

Question 13

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?

  1. "rockscissorspaper" (correct answer)
  2. "rockpaperscissors"
  3. "scissorspaperrock"
  4. "rockpaperscissorspaper"

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".

Question 14

A program uses a procedure REPLACE(original,toreplace,replacement)REPLACE(original, to_replace, replacement)REPLACE(original,tor​eplace,replacement) which finds the first occurrence of toreplaceto_replacetor​eplace in original and replaces it with replacement. What is the result of the following call?

REPLACE("happy hippo", "pp", "b")

  1. "haby hippo" (correct answer)
  2. "happy hibo"
  3. "haby hibo"
  4. "habby hibbo"

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".

Question 15

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?

  1. "start-middle-end"
  2. "start-middle-middle-middle-end" (correct answer)
  3. "start-middle-end-middle-end-middle-end"
  4. "start-end"

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.

Question 16

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.

  1. Check if SUBSTRING(fileName, LENGTH(fileName) - 3, 4) is equal to ".txt". (correct answer)
  2. Check if SUBSTRING(fileName, 1, 4) is equal to ".txt".
  3. Check if SUBSTRING(fileName, LENGTH(fileName) - 4, 4) is equal to ".txt".
  4. Check if LENGTH(fileName) is equal to 4.

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.

Question 17

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?

  1. The integer 168
  2. The integer 12345
  3. The string "168"
  4. The string "12345" (correct answer)

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".

Question 18

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"?

  1. airline + flightNum + city (correct answer)
  2. city + airline + flightNum
  3. airline + " " + flightNum + " " + city
  4. flightNum + city + airline

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.

Question 19

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?

  1. SUBSTRING(email, FIND(email, "@") + 1) (correct answer)
  2. SUBSTRING(email, FIND(email, "@"))
  3. SUBSTRING(email, 1, FIND(email, "@") - 1)
  4. SUBSTRING(email, FIND(email, "."))

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.