AP CSA Unit-Level MCQ
Methods and Parameters
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
What methods and parameters covers
Java passes everything by value. For a primitive, the method receives a copy of the number, so changing the parameter has no effect on the caller. For an object or array, the method receives a copy of the reference, which still points at the same underlying object. That means the method can mutate the object through it, but reassigning the parameter to a new object only changes the local copy.
Where students lose points
The single most consequential misconception on the exam is expecting a method to change a caller's int or a caller's String. Students also assume that because arrays can be mutated by a method, reassigning the array parameter will be visible outside, which it is not.
How it shows up on the AP exam
Unit 3 class creation, and the concept behind a large family of trace-the-output questions. It also explains why String swap methods cannot work, a perennial MCQ answer choice.
For practice use only.
Methods and Parameters MCQ Practice
AP CSA methods and parameters practice: pass by value, why primitive changes do not escape, and how object references let a method mutate shared state.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - Methods and Parameters: track the final printed value.
public class Main {
public static int combine(int a, int b, int c) {
return a + b * c;
}
public static void main(String[] args) {
System.out.print(combine(18, 3, 5));
}
}All 15 methods and parameters questions
Work through the interactive quiz above first. This is the full methods and parameters question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - Methods and Parameters: track the final printed value.
public class Main { public static int combine(int a, int b, int c) { return a + b * c; } public static void main(String[] args) { System.out.print(combine(18, 3, 5)); } }- 30
- 33
- 105
- 26
Show worked solution
Correct answer: 33
Arguments are copied into parameters. The method returns a + b * c using normal precedence.
2.Code Tracing 12 - Methods and Parameters: follow the variable updates.
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(26, 7)); } }- 33
- The code does not compile.
- 19
- -19
Show worked solution
Correct answer: 19
The first return executes only when x > y; otherwise the second return executes.
3.Code Tracing 28 - Methods and Parameters: trace the branch and loop path.
import java.util.Arrays; public class Main { public static void change(int[] nums, int add) { nums[1] += add; } public static void main(String[] args) { int[] values = {9, 7, 2, 8}; change(values, 3); System.out.print(Arrays.toString(values)); } }- [8, 2, 10, 9]
- 10
- [9, 10, 2, 8]
- [9, 7, 2, 8]
Show worked solution
Correct answer: [9, 10, 2, 8]
The array reference is passed to the method, so changing nums changes the original array.
4.Code Tracing 39 - Methods and Parameters: watch the index changes.
public class Main { public static int firstAbove(int[] nums, int limit) { for (int value : nums) { if (value > limit) return value; } return -1; } public static void main(String[] args) { int[] data = {11, 8, 4, 10, 6}; System.out.print(firstAbove(data, 8)); } }- -1
- 8
- The code does not compile.
- 11
Show worked solution
Correct answer: 11
The method returns immediately when it finds the first value above the limit.
5.Code Tracing 50 - Methods and Parameters: evaluate the state change step by step.
public class Main { public static int average(int a, int b, int c) { int total = a + b + c; return total / 3; } public static void main(String[] args) { System.out.print(average(54, 7, 7)); } }- 22
- 22.666667
- 68
- 23
Show worked solution
Correct answer: 22
The method returns an int, so total / 3 uses integer division.
6.What is printed by the following code segment?
public static int f(int n) { n = n * 2; return n; } public static void main(String[] args) { int a = 3; int b = f(f(a)); System.out.print(a + " " + b); }- 12 12
- 3 12
- 3 6
- 6 12
Show worked solution
Correct answer: 3 12
The nested calls double twice, and the caller variable is untouched because the parameter is a copy.
7.What is printed by the following code segment?
public static void g(int[] a) { a[0]++; a = new int[2]; a[0] = 99; } public static void main(String[] args) { int[] x = {5}; g(x); System.out.print(x[0]); }- 99
- 5
- 6
- 0
Show worked solution
Correct answer: 6
The increment reaches the shared array, but reassigning the parameter afterward affects only the local copy.
8.What is printed by the following code segment?
public static String h(String s) { s = s + "!"; return s; } public static void main(String[] args) { String a = "hi"; h(a); String b = h(a); System.out.print(a + b); }- hi!hi!
- hihi
- hi!hi
- hihi!
Show worked solution
Correct answer: hihi!
The discarded call has no effect because strings are immutable and the parameter is a copy of the reference.
9.What is printed by the following code segment?
public static int add(int a, int b) { return a + b; } public static double add(double a, double b) { return a + b; } public static void main(String[] args) { System.out.print(add(1, 2) + " " + add(1.0, 2)); }- 3 3.0
- 3.0 3.0
- 3 3
- 3.0 3
Show worked solution
Correct answer: 3 3.0
Overload resolution picks the int version for two ints and the double version once either argument is a double.
10.What is printed by the following code segment?
public static void swap(int[] a, int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; } public static void main(String[] args) { int[] x = {1, 2, 3}; swap(x, 0, 2); System.out.print(x[0] + "" + x[2]); }- 13
- 31
- 11
- 33
Show worked solution
Correct answer: 31
Swapping through an array parameter works because the caller and the method share one array.
11.What is printed by the following code segment?
public static int count(String s, char c) { int n = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == c) { n++; } } return n; } public static void main(String[] args) { int a = count("banana", 'a'); int n = count("banana", 'n'); System.out.print(a + n); }- 3
- 2
- 5
- 6
Show worked solution
Correct answer: 5
Three a characters and two n characters give a total of five.
12.What is printed by the following code segment?
public static int f(int n) { if (n <= 1) { return 1; } return n * f(n - 1); } public static void main(String[] args) { System.out.print(f(4)); }- 10
- 4
- 120
- 24
Show worked solution
Correct answer: 24
The method multiplies down to the base case, computing four factorial.
13.What is printed by the following code segment?
public static void addAll(int[] a, int n) { for (int i = 0; i < a.length; i++) { a[i] += n; } n = 0; } public static void main(String[] args) { int[] x = {1, 2}; int k = 5; addAll(x, k); System.out.print(x[0] + " " + k); }- 6 5
- 6 0
- 1 5
- 1 0
Show worked solution
Correct answer: 6 5
The array contents change for the caller while the int parameter does not.
14.Which call to the method below causes a compile-time error?
public static double half(int n) { return n / 2.0; }- double r = half(4);
- int r = half(4);
- System.out.print(half(4));
- double r = half(4) + 1;
Show worked solution
Correct answer: int r = half(4);
The method returns a double, and assigning a double to an int requires an explicit cast.
15.What is printed by the following code segment?
public static int f(int a, int b) { if (a > b) { return a - b; } return b - a; } public static void main(String[] args) { System.out.print(f(3, 8) + f(8, 3)); }- 0
- 5
- 10
- 11
Show worked solution
Correct answer: 10
The method returns the absolute difference either way, so both calls return 5.
