AP CSA Unit 3.5: Nested Method Calls Practice
Share
Unit 3, Section 3.5
Day 5 Practice • January 11, 2026
🎯 Focus: Writing Methods
Practice Question
Consider the following class definition and code segment:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
return a * b;
}
public int compute(int x) {
return add(x, multiply(x, x));
}
}
// Client code:
Calculator calc = new Calculator();
System.out.println(calc.compute(3));
What is printed as a result of executing the client code?
What This Tests: Section 3.5 covers writing methods. This question tests nested method calls—when methods call other methods, evaluate the inner call first, then use its result in the outer call.
Step-by-Step Trace
compute(3)
= add(3, multiply(3, 3)) // x = 3
= add(3, 9) // multiply(3,3) = 9
= 12 // add(3, 9) = 12
| Step | Method Call | Parameters | Returns |
|---|---|---|---|
| 1 | compute(3) | x = 3 | ? |
| 2 | multiply(3, 3) | a=3, b=3 | 9 |
| 3 | add(3, 9) | a=3, b=9 | 12 |
| 4 | compute returns | - | 12 |
Common Mistakes
Mistake: Answer B (9)
This only calculates multiply(3, 3) = 9 and forgets to add x (3) to the result.
Mistake: Answer A (6)
This might come from add(3, 3) = 6, ignoring the multiply call entirely.
Nested Calls Strategy
Inside-Out Evaluation
When methods call other methods:
- Find the innermost method call
- Evaluate it first
- Replace it with its return value
- Continue outward until done
Difficulty: Medium • Time: 3 minutes • AP Skill: 3.A - Call methods
Ready to Level Up Your AP CSA Skills?
Get personalized help or access our complete question bank
Premium Question Bank - Coming Soon Schedule 1-on-1 Tutoring