AP CSA Unit-Level MCQ
Return Values
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
What return values covers
A return statement produces a value and immediately exits the method, so any code after it in that path never runs. Every path through a value-returning method must return something of the declared type; a void method returns no value and calling it in an expression does not compile. Returning a value is not the same as printing it, and a returned value is lost unless the caller stores or uses it.
Where students lose points
Calling a method and discarding its result, then expecting the caller's variable to have changed, is the classic error, and it is the same misunderstanding that drives the String immutability trap. Students also write returns inside an if without a fall-through return, and they print inside a method that the question expected to return.
How it shows up on the AP exam
Unit 3 content that determines whether an FRQ method header is implemented correctly at all. A method that prints instead of returning generally scores zero on that part regardless of the logic inside.
For practice use only.
Return Values MCQ Practice
AP CSA return value practice: what return actually does, void versus value-returning methods, ignored results, and return type matching.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - Return Values: track the final printed value.
public class Main {
public static int gap(int x, int y) {
if (x > y) return x - y;
return y - x;
}
public static void main(String[] args) {
System.out.print(gap(18, 5));
}
}All 15 return values questions
Work through the interactive quiz above first. This is the full return values question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - Return Values: track the final printed value.
public class Main { public static int gap(int x, int y) { if (x > y) return x - y; return y - x; } public static void main(String[] args) { System.out.print(gap(18, 5)); } }- 23
- The code does not compile.
- 13
- 0
Show worked solution
Correct answer: 13
An early return exits the method immediately, so the later statement runs only when the condition was false.
2.Code Tracing 12 - Return Values: follow the variable updates.
public class Main { public static int addOne(int v) { v = v + 1; return v; } public static void main(String[] args) { int r = addOne(26); System.out.print(r); } }- 26
- 28
- 0
- 27
Show worked solution
Correct answer: 27
The parameter is a copy, so the caller never sees the change, but the returned value carries the result back out.
3.Code Tracing 28 - Return Values: trace the branch and loop path.
public class Main { public static int bump(int x) { return x + 10; } public static void main(String[] args) { int v = 38; bump(v); System.out.print(v); } }- 48
- 10
- 0
- 38
Show worked solution
Correct answer: 38
The returned value is discarded because it is never assigned, so the original variable is unchanged.
4.Code Tracing 39 - Return Values: watch the index changes.
public class Main { public static int combine(int x, int y) { return x / y + x % y; } public static void main(String[] args) { System.out.print(combine(46, 5)); } }- 10
- 9
- 1
- 51
Show worked solution
Correct answer: 10
Both operations use integer arithmetic, and the single return expression is evaluated before the value leaves the method.
5.Code Tracing 50 - Return Values: evaluate the state change step by step.
public class Main { public static boolean isEven(int v) { return v % 2 == 0; } public static void main(String[] args) { System.out.print(isEven(54) + ":" + isEven(55)); } }- false:false
- true:false
- false:true
- true:true
Show worked solution
Correct answer: true:false
A boolean-returning method hands back the comparison result directly, with no if statement needed.
6.What is printed by the following code segment?
public static int f(int n) { if (n > 5) { return 1; } if (n > 2) { return 2; } return 3; } public static void main(String[] args) { System.out.print(f(6) + f(3) + f(1)); }- 3
- 6
- 9
- 5
Show worked solution
Correct answer: 6
Each call returns at the first condition it satisfies, giving 1, 2, and 3.
7.What is printed by the following code segment?
public static boolean p(int n) { for (int d = 2; d < n; d++) { if (n % d == 0) { return false; } } return n > 1; } public static void main(String[] args) { System.out.print(p(1) + " " + p(2) + " " + p(9)); }- true true false
- false true true
- false true false
- false false false
Show worked solution
Correct answer: false true false
The early return rejects composites, and the final expression handles the value 1 correctly.
8.What is printed by the following code segment?
public static String f(int n) { String s = ""; while (n > 0) { s = n % 2 + s; n /= 2; } return s.equals("") ? "0" : s; } public static void main(String[] args) { System.out.print(f(5) + " " + f(0)); }- 101
- 5 0
- 110 0
- 101 0
Show worked solution
Correct answer: 101 0
The conditional at the end supplies a value for the case where the loop never runs.
9.What is printed by the following code segment?
public static int f() { int n = 0; for (int i = 0; i < 3; i++) { n++; if (n == 2) { return n; } } return -1; } public static void main(String[] args) { System.out.print(f()); }- 2
- 3
- -1
- 1
Show worked solution
Correct answer: 2
A return exits the method immediately, so the loop never completes.
10.What is printed by the following code segment?
public static int f(int n) { int r = 0; while (n > 0) { r += n % 10; n /= 10; } return r > 9 ? f(r) : r; } public static void main(String[] args) { System.out.print(f(9875)); }- 29
- 2
- 11
- 9
Show worked solution
Correct answer: 2
The digit sum is 29, which is still more than one digit, so the method calls itself and returns 11 then 2.
11.What is printed by the following code segment?
public static int max(int[] a) { if (a.length == 0) { return -1; } int m = a[0]; for (int v : a) { if (v > m) { m = v; } } return m; } public static void main(String[] args) { System.out.print(max(new int[] {3, 9, 2})); System.out.print(" " + max(new int[] {})); }- 9 0
- 3 -1
- 9 -1
- 9 3
Show worked solution
Correct answer: 9 -1
The guard supplies a sentinel for the empty case before any element is read.
12.What is printed by the following code segment?
public static int f(int n) { n++; return n++; } public static void main(String[] args) { System.out.print(f(1)); }- 3
- 1
- 4
- 2
Show worked solution
Correct answer: 2
The postfix increment returns the value before the increment, so the extra step is lost.
13.Why does the method below fail to compile?
public static int sign(int n) { if (n > 0) { return 1; } else if (n < 0) { return -1; } }- The path where n is 0 reaches the end without returning a value.
- A method cannot have more than one return statement.
- The return type should be void.
- An else clause is required after every else if.
Show worked solution
Correct answer: The path where n is 0 reaches the end without returning a value.
Every path through a value-returning method must return, and the compiler does not reason about which paths are reachable in practice.
14.What is printed by the following code segment?
public static int[] f(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = i * 2; } return a; } public static void main(String[] args) { System.out.print(f(4)[3]); }- 8
- 6
- 3
- 4
Show worked solution
Correct answer: 6
A method may return an array, and the result can be indexed directly.
15.What is printed by the following code segment?
public static int f(int a, int b) { return a > b ? a : f(b, a); } public static void main(String[] args) { System.out.print(f(2, 7)); }- 2
- 9
- 7
- 5
Show worked solution
Correct answer: 7
Swapping the arguments once puts the larger value first, so the second call returns it.
