AP Computer Science A • Unit 1.15

String Manipulation

Detailed answer explanations for the Unit 1.15 multiple-choice practice questions.

Q1B
Q2A
Q3A
Q4B
Q5A
Q6A
Q7C
Q8C
Q9B
Q10B
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the following code segment. What is printed?

String word = "notebook";
String part = word.substring(1, 4);
int pos = word.indexOf("book");

System.out.println(part + " " + pos);
Anot 4
Bote 4
Cote 3
Dote -1
Correct answer: B. The call substring(1, 4) yields ote, and "book" begins at index 4, so the line prints ote 4.
Explanation

Indexes start at 0, and the two-argument substring stops just before its second index, so the characters at 1, 2, and 3 are returned. indexOf reports where the first occurrence of the target begins.

  • A is not correct: the returned characters begin at index 1 rather than index 0, so the first three characters not are not what substring(1, 4) produces.
  • C is not correct: the search counts from zero, so "book" begins at index 4; index 3 holds the character just before it.
  • D is not correct: -1 is returned only when the target string is not found, and "book" does appear inside "notebook".
Q2

Consider the following code segment. What is printed?

String course = "APCSA";
System.out.println(course.substring(2));
System.out.println(course.length());
ACSA followed by 5
BPCSA followed by 5
CCSA followed by 4
DAP followed by 5
Correct answer: A. The one-argument substring(2) keeps everything from index 2 onward, and length() counts all 5 characters.
Explanation

The one-argument version of substring starts at the given index and continues to the end of the string. length() returns the number of characters, and the valid indexes run from 0 to length() - 1.

  • B is not correct: the one-argument call starts at index 2, not at index 1, so the character P is left out of the returned string.
  • C is not correct: length() gives the character count, which is 5 here; 4 is the last valid index rather than the number of characters.
  • D is not correct: the one-argument version keeps the characters from the given index onward, so it returns the ending CSA rather than the leading AP.
Q3

Consider the following code segment. What is printed?

String first = "java";
String second = "java";

System.out.println(first.equals(second));
Atrue
Bfalse
Cjavajava
DThe code does not compile because equals cannot be used with strings.
Correct answer: A. equals checks whether two strings have the same contents, and both variables hold the same characters, so the result is true.
Explanation

The method equals compares the contents of two strings and reports whether they hold the same characters. The comparison is case-sensitive, but here the two strings are written identically, so they are equal.

  • B is not correct: false is reported when the contents differ, as with "Hello!" and "hello!"; both variables here hold the same characters.
  • C is not correct: equals compares contents and reports true or false; joining two strings end to end is what the + operator does.
  • D is not correct: equals is a String method that checks whether two strings have the same contents, so this code compiles and runs.
Q4

Consider the following code segment. What is printed?

String code = "AP";
System.out.println(code + 2 + 3);
System.out.println(code + (2 + 3));
AAP5 followed by AP5
BAP23 followed by AP5
CAP23 followed by AP23
D5AP followed by AP5
Correct answer: B. Concatenation runs left to right, so the digits join as text, while parentheses force the addition to happen first.
Explanation

Concatenation is evaluated from left to right. Once a String has appeared, later + operations join text instead of adding numbers. Parentheses change that by forcing the addition inside them to happen before any joining.

  • A is not correct: without parentheses the leftmost + already has a String operand, so 2 is joined as text before 3 is reached.
  • C is not correct: parentheses in the second statement force 2 + 3 to be added as numbers first, so 5 is joined instead of the digits separately.
  • D is not correct: the String operand sits on the left of each +, so the converted numbers are appended after it and never placed in front.
Q5

Consider the following code segment. What is printed?

String text = "papaya";
System.out.println(text.indexOf("pa"));
System.out.println(text.indexOf("x"));
A0 followed by -1
B2 followed by -1
C0 followed by 2
D-1 followed by 0
Correct answer: A. indexOf returns 0 for the first occurrence of "pa" and -1 because "x" does not appear.
Explanation

The method indexOf searches for one string inside another and returns the index of the first occurrence, counting from 0. When the target string is not found anywhere, the method returns -1 instead.

  • B is not correct: 2 is where a later occurrence of "pa" begins, but indexOf reports the first occurrence, which starts at index 0.
  • C is not correct: "x" never appears in "papaya", and a target that is not found produces -1 rather than an index.
  • D is not correct: the two results are exchanged: the target that is found gives its first index 0, and the missing target gives -1.
Q6

Consider the following code segment. What is printed?

String word = "coding";
word.substring(1);
System.out.println(word);
Acoding
Boding
Cc
DThe code does not compile.
Correct answer: A. The returned string is ignored and never assigned, so word still refers to its original contents when it is printed.
Explanation

A String cannot be changed. A method such as substring builds a new string and hands that back, leaving the original exactly as it was. Here the returned value is ignored, so nothing about the variable changes.

  • B is not correct: oding is the string that substring(1) returns, but nothing assigns that result back, so the variable is unaffected.
  • C is not correct: substring(1) starts at index 1 and continues to the end, so it never produces the single character that comes before that index.
  • D is not correct: ignoring the value that a method returns is legal, so the code compiles and prints the unchanged string.
Q7

Consider the following code segment. What is printed?

String label = "Room";
label += 4;
System.out.println(label);
ARoom
B4
CRoom4
DThe code does not compile because 4 is an int.
Correct answer: C. += builds the combined string and assigns it back, and the int value is converted to text.
Explanation

The += operator creates the combined string and assigns it back to the variable on its left. A primitive value concatenated with a String is converted to text as Java builds the new string.

  • A is not correct: += assigns the combined string back to the variable, so the value stored afterwards is no longer only the original characters.
  • B is not correct: the original characters are kept and the converted number is joined onto the end, so the number does not replace what was there.
  • D is not correct: an int may be concatenated with a String, because Java converts the primitive value to text while building the new string.
Q8

Consider the following code segment. What is printed?

String s = "garden";
String a = s.substring(0, 3);
String b = s.substring(3);
System.out.println(a + b);
Agaren
Bgardden
Cgarden
Dgar den
Correct answer: C. The two pieces meet exactly at index 3, so joining them rebuilds the original characters with nothing dropped or repeated.
Explanation

The two-argument call returns the characters starting at from and ending just before to, while the one-argument call runs from its index to the end of the string. The two results fit together without a gap.

  • A is not correct: no character is skipped, because the two-argument call stops just before index 3 and the one-argument call starts at index 3.
  • B is not correct: the character at index 3 is not included by substring(0, 3), so it appears once rather than twice in the joined result.
  • D is not correct: + joins the two strings directly, and neither call produces a space, so nothing separates the two pieces.
Q9

Consider the following code segment. What is printed?

String tag = "AP-CSA";
int pos = tag.indexOf("-");
String part = tag.substring(pos + 1);
System.out.println(part + " " + pos);
A-CSA 2
BCSA 2
CCSA 3
DThe code does not compile.
Correct answer: B. indexOf("-") gives 2, and starting the substring at pos + 1 keeps the characters after the target.
Explanation

The method indexOf returns the index of the first occurrence, counting from 0. Adding 1 gives the position just past that target, and the one-argument substring keeps everything from there to the end.

  • A is not correct: the substring begins at pos + 1, one position past the target, so the character that was searched for is left out.
  • C is not correct: pos holds the index where the target begins, which is 2; 3 is the starting index used by the substring call.
  • D is not correct: indexOf and substring are String methods used exactly as shown here, so this code compiles and runs.
Q10

Which statement about "apple".compareTo("banana") is correct?

AIt returns 0 because both are strings.
BIt returns a negative value because "apple" comes before "banana".
CIt returns a positive value because "apple" is shorter.
DIt returns true.
Correct answer: B. A negative result from compareTo places the receiver before the argument in alphabetical order.
Explanation

The method compareTo compares two strings in alphabetical order. The exact nonzero values are not important: 0 means the strings are equal, a negative result places the receiver before the argument, and a positive result places it after.

  • A is not correct: 0 is returned when the two strings are equal, and these two strings do not hold the same contents.
  • C is not correct: a positive result would place the receiver after the argument, and the sign reflects alphabetical order rather than which string is shorter.
  • D is not correct: compareTo reports order as a number whose sign matters; true and false are what equals reports.