Java Methods, Parameters & Overloading Quiz
35 challenging and insight-revealing questions that dive deep into how Java really handles method invocation, parameter passing, overloading resolution, varargs behavior, return type rules, and the subtle edge cases that surprise even experienced developers.
Question 1
Why does Java use pass-by-value even when passing object references?
Question 2
Given these two overloaded methods, which one is selected when calling process(null)?
void process(String s) { System.out.println("String"); }
void process(Object o) { System.out.println("Object"); }
// process(null);Question 3
Why does this seemingly harmless swap method fail to swap the two strings?
void swap(String a, String b) {
String temp = a;
a = b;
b = temp;
}
// String x = "first"; String y = "second"; swap(x, y);Question 4
Which of these method signatures would cause a compilation error if declared together?
Question 5
What is actually created on the heap when a varargs method is called with three integers?
void log(int... values) { }
// log(1, 2, 3);Question 6
Why does this overload resolution prefer the Integer version over Long?
void execute(Integer i) { System.out.println("Integer"); }
void execute(Long l) { System.out.println("Long"); }
// execute(100);Question 7
Which of these varargs declarations is legal and safe to call with zero arguments?
Question 8
Why does this method call result in a compiler error?
void merge(String... parts) { }
void merge(Object... objects) { }
// merge("a", "b");Question 9
What is the most surprising thing about this perfectly valid method declaration?
public <T> T[] toArray(T... elements) {
return elements;
}Question 10
Why does this attempt to modify a list inside a method not affect the original list?
void clear(List<String> list) {
list = new ArrayList<>();
}
// List<String> data = Arrays.asList("a", "b"); clear(data);Question 11
Which return type is allowed when overriding a method that returns String?
Question 12
What happens when you pass an array to a varargs method expecting the element type?
void print(String... values) { }
String[] arr = {"x", "y"};
print(arr);Question 13
Why does this overload resolution choose the primitive version over the wrapper?
void accept(long value) { System.out.println("long"); }
void accept(Long value) { System.out.println("Long"); }
// accept(42);Question 14
What is the output when this varargs method is called with no arguments?
static void debug(Object... msg) {
System.out.println(msg.length + " " + (msg[0] == null ? "null" : msg[0]));
}
// debug();Question 15
Why is this method declaration illegal?
public static void <T> merge(T... items) {}Question 16
Why does this perfectly reasonable method call fail to compile?
void log(int x, String... tags) { }
// log(1, null);Question 17
Which of these method pairs can coexist as valid overloads?
Question 18
What is the practical consequence of Java being pass-by-value for all parameters?
Question 19
Why does this varargs method trigger a heap pollution warning?
@SafeVarargs
final void unsafe(T... data) {
Object[] array = data; // unchecked cast
}Question 20
Which of these method calls would select the varargs version?
void execute(int a, String b) { }
void execute(int a, String... tags) { }
// execute(10, "debug", "perf");Question 21
What is the most important rule to remember about method overloading in Java?
Question 22
Why can you not overload a method solely by changing the parameter name?
Question 23
What is the output when this tricky overload scenario is executed?
static void call(Number n) { System.out.println("Number"); }
static void call(Double d) { System.out.println("Double"); }
// call(10);Question 24
Why is this method perfectly legal even though it returns nothing in some paths?
int next() {
if (hasNext()) return value++;
throw new NoSuchElementException();
}Question 25
What is the safest way to write a varargs method that accepts zero or more arguments without allocation overhead?
Question 26
Why does this method call unexpectedly invoke the Object version?
void log(String s) { }
void log(Object o) { }
String s = null;
log(s);Question 27
What is the single most common mistake developers make with method parameters?
Question 28
Why is this overload pair surprisingly allowed?
void process(int... nums) { }
void process(Integer... nums) { }Question 29
When should you prefer method overloading over default parameter values?
Question 30
What is the output of this subtle overload resolution test?
static void test(long x) { System.out.println("long"); }
static void test(Integer x) { System.out.println("Integer"); }
// test(100);Question 31
Why does this varargs method never throw ArrayIndexOutOfBoundsException even when called with no arguments?
void safe(String first, int... rest) {
System.out.println(rest.length);
}Question 32
Which of these is NOT a valid method modifier combination?
Question 33
What is the key difference between method overloading and method overriding?
Question 34
Why is this method signature considered excellent API design?
public static <T> List<T> of(T... elements)Question 35
After mastering methods and parameters, what is the single most important principle you now fully understand?
