Unit 1 Day 8: String Concatenation Practice

Unit 1, Section 1.8
Day 8 Practice • January 14, 2026
🎯 Focus: String Concatenation

Practice Question

Consider the following code segment:
int x = 5;
int y = 3;
System.out.println("Sum: " + x + y);
What is printed as a result of executing this code segment?
What This Tests: This question tests understanding of String concatenation and operator precedence. When a String is involved with the + operator, Java performs concatenation rather than addition—but order matters!

Key Concept: String Concatenation Order

Java evaluates expressions left to right. When a String appears first, subsequent + operations become concatenation:

"Sum: " + 5 + 3     // → "Sum: 5" + 3 → "Sum: 53"
5 + 3 + " is sum"   // → 8 + " is sum" → "8 is sum"
"Sum: " + (5 + 3)   // → "Sum: " + 8 → "Sum: 8"

Step-by-Step Trace

Step Expression Result
1 "Sum: " + x "Sum: 5" (String)
2 "Sum: 5" + y "Sum: 53" (String)

Because the String comes first, x is converted to "5" and concatenated, giving "Sum: 5". Then y is converted to "3" and concatenated, giving "Sum: 53".

Common Mistakes

Mistake: Answer A (Sum: 8)

This would require parentheses: "Sum: " + (x + y). Without parentheses, the numbers are concatenated as strings, not added.

How to Force Addition

💡 Use Parentheses!

To add numbers before concatenating with a String, wrap them in parentheses:

System.out.println("Sum: " + (x + y));  // "Sum: 8"

Related Topics

  • Section 1.3: Operator Precedence
  • Section 1.4: String Literals
  • Section 1.9: String Methods
Difficulty: Medium • Time: 1-2 minutes • AP Skill: 2.B - Determine output

Want More Practice?

Master AP CSA with guided practice and expert help

Schedule 1-on-1 Tutoring Practice FRQs
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.