Strings
Help Questions
AP Computer Science Principles › Strings
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 $$FIRST_LETTER(str)$$ returns the first character of a string, which expression creates the correct acronym?
FIRST_LETTER(w1) + " " + FIRST_LETTER(w2) + " " + FIRST_LETTER(w3)
w1 + w2 + w3
FIRST_LETTER(w1 + w2 + w3)
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.
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?
DISPLAY("Welcome, " + user + "!")
DISPLAY("Welcome, user!")
DISPLAY(Welcome, + user + !)
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.
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.
"!A"
"As"
"A!"
"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!".
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"?
firstHalf + SUBSTRING(word, 3, 1) + secondHalf
firstHalf + SUBSTRING(word, 4, 1) + secondHalf
firstHalf + secondHalf
secondHalf + SUBSTRING(word, 4, 1) + firstHalf
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.
LENGTH(password) ≥ 8
LENGTH(password) = 8
LENGTH(password) > 8
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.
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.
"(" + SUBSTRING(pNum, 1, 3) + ") " + SUBSTRING(pNum, 4, 3) + "-" + SUBSTRING(pNum, 7, 4)
"(" + SUBSTRING(pNum, 0, 3) + ") " + SUBSTRING(pNum, 3, 3) + "-" + SUBSTRING(pNum, 6, 4)
SUBSTRING(pNum, 1, 3) + "-" + SUBSTRING(pNum, 4, 3) + "-" + SUBSTRING(pNum, 7, 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.
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"?
"prefix" + "suffix"
prefix + " " + suffix
prefix + suffix
suffix + prefix
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))?
"nov"
"nova"
"nnov"
"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".
What is a substring?
A procedure that joins two or more strings together end-to-end to create a new, longer string.
A special character within a string that indicates the end of the string.
A string that contains only numerical characters instead of alphabetical ones.
A part or portion of an existing string, which is itself a string.
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.
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 "apple", and word2 is "sauce"
word1 is "applesauce", and word2 is "sauce"
word1 is "sauce", and word2 is "sauce"
word1 is "sauce", and word2 is "applesauce"
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".