AP Computer Science A · Topic 2.6
String Objects: Concatenation, Literals, and More Practice
Part of Using Objects.
Practice questions
27
Sample questions
5 of 27 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 2/5
String s = "Score: "; int n = 5; System.out.println(s + 3 + n);What does this print?
- Acheck_circle
Score: 35
- B
Score: 8
- C
Score: 53
- D
Compile error
Why
Left-to-right + with a String produces String concatenation: "Score: " + 3 = "Score: 3", then + 5 yields "Score: 35".
- A
Sample 2difficulty 2/5
What does <code>"Hi " + 5 + 2</code> produce?
- A
7 Hi
- Bcheck_circle
Hi 52
- C
Hi 7
- D
Hi 5 2
Why
Left-to-right: "Hi " + 5 → "Hi 5"; + 2 → "Hi 52" (string concatenation, not addition).
- A
Sample 3difficulty 2/5
String a = "Hello"; String b = "World"; System.out.println(a + " " + b);What is printed?
- A
Hello+World
- B
HelloWorld
- Ccheck_circle
Hello World
- D
Hello World
Why
The + operator concatenates the strings with a space in between.
- A
Sample 4difficulty 3/5
String a = "Cat"; String b = "cat"; System.out.println(a.equals(b));What is printed?
- A
true
- Bcheck_circle
false
- C
1
- D
0
Why
equals is case-sensitive; "Cat" and "cat" differ in case, so it returns false.
- A
Sample 5difficulty 3/5
What does <code>"" + 1 + 2 + 3</code> produce?
- A
Compile error
- B
6
- Ccheck_circle
123
- D
1.2.3
Why
Empty string forces concatenation; result is "123".
- A