PrepGo

AP Computer Science A Practice Quiz: String Manipulation

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 14 questions to check your progress.

Question 1 of 14

Consider the following code segment: String s1 = "AP"; String s2 = " Computer Science"; String result = s1 + s2; What is the value of `result` after this code executes?

All Questions (14)

Consider the following code segment: String s1 = "AP"; String s2 = " Computer Science"; String result = s1 + s2; What is the value of `result` after this code executes?

A) "AP Computer Science"

B) "APComputer Science"

C) "AP"

D) " Computer Science"

Correct Answer: A

The `+` operator concatenates the two String objects `s1` and `s2`. The content of `s1` ("AP") is combined with the content of `s2` (" Computer Science") to create a new String object with the value "AP Computer Science".

Consider the following code segment: String greeting = "hello"; greeting.toUpperCase(); // Assume this method attempts to make the string uppercase What is the value of the `greeting` variable after this code has executed?

A) "HELLO"

B) "hello"

C) null

D) The code results in a compile-time error.

Correct Answer: B

String objects are immutable. This means that once a String object is created, its content cannot be changed. The method call `greeting.toUpperCase()` does not modify the original String object referenced by `greeting`. Instead, it creates and returns a new String object ("HELLO"), but this new object is not assigned to any variable.

Consider the following code segment: String label = "Score: "; int score = 95; String report = label + score; What is the value of `report` after this code executes?

A) The code will cause a compile-time error.

B) "Score: "

C) "Score: 95"

D) "95"

Correct Answer: C

A primitive value, like the integer `score`, can be concatenated with a String object. The integer `95` is converted to its String representation ("95") and then combined with the `label` string, resulting in the new String object "Score: 95".

A String object is defined as `String word = "Java";`. What is the index value of the last character in this string?

A) 0

B) 3

C) 4

D) 5

Correct Answer: B

String objects have index values from 0 to one less than the length of the string. The string "Java" has a length of 4. Therefore, its indices are 0, 1, 2, and 3. The last character, 'a', is at index 3.

Consider the String object `String str = "APCS";`. Which of the following lines of code, if `charAt` is a method that accesses a character at a given index, would result in a `StringIndexOutOfBoundsException`?

A) char c = str.charAt(0);

B) char c = str.charAt(3);

C) char c = str.charAt(4);

D) char c = str.charAt(1);

Correct Answer: C

The string "APCS" has a length of 4. The valid index values are from 0 to length - 1, which is 0 to 3. Attempting to access an index outside this range, such as index 4, will result in a `StringIndexOutOfBoundsException`.

Consider the following code segment: String message = "Good"; message += " morning"; What is the final value of the `message` variable?

A) "Good"

B) " morning"

C) "Good morning"

D) The code results in an error because Strings are immutable.

Correct Answer: C

The `+=` operator is used for concatenation. It creates a new String object by combining the original string ("Good") with the new string (" morning"). The reference variable `message` is then updated to point to this new String object, "Good morning". The original "Good" object is unchanged but no longer referenced by `message`.

Consider the following code segment: String s1 = "test"; String s2 = s1; s1 = s1 + "ing"; What are the final values of `s1` and `s2`?

A) s1 is "testing", s2 is "testing"

B) s1 is "testing", s2 is "test"

C) s1 is "test", s2 is "testing"

D) s1 is "test", s2 is "test"

Correct Answer: B

Initially, both `s1` and `s2` reference the same String object "test". Because Strings are immutable, the operation `s1 + "ing"` creates a completely new String object, "testing". The variable `s1` is then reassigned to reference this new object. The variable `s2` is not affected and continues to reference the original, unchanged String object "test".

If a String is defined as `String title = "Programming";` and an integer `len` stores the result of calling a method that returns the string's length, what expression represents the index of the last character?

A) len

B) len + 1

C) len - 1

D) 0

Correct Answer: C

A String's indices range from 0 to one less than its length. If `len` is the length of the string, the first character is at index 0 and the last character is at index `len - 1`.

What is printed by the following code segment? System.out.println("Result: " + 10 + 5);

A) Result: 15

B) Result: 105

C) 15: Result

D) The code produces a syntax error.

Correct Answer: B

Concatenation operations involving a String are evaluated from left to right. First, "Result: " is concatenated with the primitive `10`, producing a new String "Result: 10". Then, this new string is concatenated with the primitive `5`, producing the final String "Result: 105".

What is printed by the following code segment? System.out.println(10 + 5 + " is the result");

A) 105 is the result

B) 15 is the result

C) The code produces a syntax error.

D) is the result 15

Correct Answer: B

Operations are evaluated from left to right. Since the first two operands (`10` and `5`) are integers, the `+` operator performs arithmetic addition, resulting in `15`. Then, the integer `15` is concatenated with the String " is the result", producing the final String "15 is the result".

Which of the following statements best describes the consequence of a String object being immutable?

A) The value of a String variable cannot be changed after it is declared.

B) Methods called on a String object create and return a new String object rather than modifying the original.

C) String objects cannot be combined using the `+` or `+=` operators.

D) Accessing an index of a String object is not permitted.

Correct Answer: B

Immutability means that the state (the character sequence) of a String object cannot be changed after it is created. Therefore, any method that appears to modify the string, such as concatenation or changing case, must create and return a new String object with the modified content. The original object remains untouched.

Consider the code: `String str = "Go"; str += " Team";`. How many distinct String objects have been created and referenced by the `str` variable throughout the execution of this code?

A) 1

B) 2

C) 3

D) 4

Correct Answer: B

Initially, the String literal "Go" is created, and `str` references it. This is the first object. When `str += " Team"` is executed, a new String object, "Go Team", is created by concatenating the content of `str` and the literal " Team". The variable `str` is then updated to reference this new object. Therefore, `str` has referenced a total of two distinct objects: "Go" and "Go Team". (Note: The literal " Team" is also created, but the question asks what `str` has referenced).

When the code `String result = "one" + "two";` is executed, which of the following is true?

A) The String object "one" is modified to become "onetwo".

B) The String object "two" is modified to become "onetwo".

C) A new String object, "onetwo", is created and assigned to `result`.

D) No new String objects are created; existing ones are just linked together.

Correct Answer: C

The `+` operator, when used with strings, results in a new String object. The original String objects ("one" and "two") are immutable and are not changed. A third String object containing the combined content ("onetwo") is created, and the `result` variable is set to reference it.

A String is created with the statement `String phrase = "Final Exam";`. What is the valid range of index values for this String object?

A) 1 to 10

B) 0 to 9

C) 1 to 9

D) 0 to 10

Correct Answer: B

The length of the string "Final Exam" is 10 characters (including the space). A String object has index values from 0 to one less than its length. Therefore, the valid indices for this string are from 0 to (10 - 1), which is 0 to 9.