Java Methods, Parameters & Overloading Quiz

Java
0 Passed
0% acceptance

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.

35 Questions
~70 minutes
1

Question 1

Why does Java use pass-by-value even when passing object references?

A
The reference itself is copied — the object is not duplicated, but the caller and callee have independent references
B
Objects are passed by reference for performance
C
Only primitives are passed by value
D
Pass-by-value vs pass-by-reference is configurable
2

Question 2

Given these two overloaded methods, which one is selected when calling process(null)?

java
void process(String s) { System.out.println("String"); }
void process(Object o) { System.out.println("Object"); }
// process(null);
A
The String version — more specific type wins
B
The Object version
C
Compilation error — ambiguous
D
Runtime exception
3

Question 3

Why does this seemingly harmless swap method fail to swap the two strings?

javascript
void swap(String a, String b) {
    String temp = a;
    a = b;
    b = temp;
}
// String x = "first"; String y = "second"; swap(x, y);
A
Parameters are passed by value — reassigning a and b only changes local copies
B
Strings are immutable so swapping is impossible
C
The method needs to return the swapped values
D
Varargs are required for swapping
4

Question 4

Which of these method signatures would cause a compilation error if declared together?

A
void run(int a, double b) and void run(double a, int b)
B
void run(int a, double b) and void run(int a, Double b)
C
void run(int a, double b) and int run(int a, double b)
D
void run(int... args) and void run(int[] args)
5

Question 5

What is actually created on the heap when a varargs method is called with three integers?

java
void log(int... values) { }
// log(1, 2, 3);
A
A new int[] array containing {1, 2, 3}
B
Three separate int parameters
C
Nothing — varargs are optimized away
D
An Integer[] after autoboxing
6

Question 6

Why does this overload resolution prefer the Integer version over Long?

javascript
void execute(Integer i) { System.out.println("Integer"); }
void execute(Long l) { System.out.println("Long"); }
// execute(100);
A
Widening primitive conversion (int → Integer) beats boxing + widening reference
B
Long is more specific
C
Both are equally applicable
D
Compilation error
7

Question 7

Which of these varargs declarations is legal and safe to call with zero arguments?

A
void process(String... args)
B
void process(String prefix, int... values)
C
void process(int... values, String suffix)
D
void process(int[]... arrays)
8

Question 8

Why does this method call result in a compiler error?

java
void merge(String... parts) { }
void merge(Object... objects) { }
// merge("a", "b");
A
Both overloads are applicable and equally specific — ambiguous call
B
String... is more specific than Object...
C
Varargs cannot be overloaded
D
String literals require explicit array
9

Question 9

What is the most surprising thing about this perfectly valid method declaration?

javascript
public <T> T[] toArray(T... elements) {
    return elements;
}
A
It compiles and works — varargs and generics can coexist safely here
B
Heap pollution warning
C
Cannot return varargs parameter
D
Generic array creation error
10

Question 10

Why does this attempt to modify a list inside a method not affect the original list?

javascript
void clear(List<String> list) {
    list = new ArrayList<>();
}
// List<String> data = Arrays.asList("a", "b"); clear(data);
A
The list parameter is passed by value — reassignment doesn’t propagate
B
Lists are immutable
C
Generics prevent modification
D
Varargs are involved
11

Question 11

Which return type is allowed when overriding a method that returns String?

A
CharSequence — covariant return type
B
Object — superclass of String
C
StringBuilder
D
void
12

Question 12

What happens when you pass an array to a varargs method expecting the element type?

javascript
void print(String... values) { }
String[] arr = {"x", "y"};
print(arr);
A
Compiles and treats the entire array as one varargs element
B
Automatically expands the array into individual elements
C
Compilation error
D
NullPointerException
13

Question 13

Why does this overload resolution choose the primitive version over the wrapper?

javascript
void accept(long value) { System.out.println("long"); }
void accept(Long value) { System.out.println("Long"); }
// accept(42);
A
Widening primitive conversion (int → long) is preferred over boxing (int → Long)
B
Long is more specific
C
Ambiguous call
D
Runtime decision
14

Question 14

What is the output when this varargs method is called with no arguments?

java
static void debug(Object... msg) {
    System.out.println(msg.length + " " + (msg[0] == null ? "null" : msg[0]));
}
// debug();
A
0 null
B
1 null
C
ArrayIndexOutOfBoundsException
D
Compilation error
15

Question 15

Why is this method declaration illegal?

java
public static void <T> merge(T... items) {}
A
Type parameters cannot appear before varargs in the same declaration
B
Static methods cannot be generic
C
Varargs must be the last parameter
D
No error
16

Question 16

Why does this perfectly reasonable method call fail to compile?

javascript
void log(int x, String... tags) { }
// log(1, null);
A
null is ambiguous — could be String[] or zero-length varargs
B
null cannot be passed to varargs
C
int cannot be followed by null
D
Runtime NullPointerException
17

Question 17

Which of these method pairs can coexist as valid overloads?

A
void run(Integer x) and void run(int... x)
B
void run(int x) and void run(Integer x)
C
void run(int x) and void run(long x)
D
void run(String x) and void run(StringBuilder x)
18

Question 18

What is the practical consequence of Java being pass-by-value for all parameters?

A
Methods cannot permanently modify primitive arguments or reassign object references
B
All objects are copied on method entry
C
Performance suffers greatly
D
Only final parameters are safe
19

Question 19

Why does this varargs method trigger a heap pollution warning?

javascript
@SafeVarargs
final void unsafe(T... data) {
    Object[] array = data; // unchecked cast
}
A
Assigning varargs parameter to a generic array loses type safety
B
Varargs are always unsafe
C
@SafeVarargs eliminates the warning
D
No warning occurs
20

Question 20

Which of these method calls would select the varargs version?

javascript
void execute(int a, String b) { }
void execute(int a, String... tags) { }
// execute(10, "debug", "perf");
A
The varargs version — more specific fixed parameters plus extra arguments
B
The fixed-parameter version
C
Compilation error — ambiguous
D
Runtime decision
21

Question 21

What is the most important rule to remember about method overloading in Java?

A
It is resolved at compile time based on the static type of arguments
B
It is resolved at runtime like overriding
C
Return type participates in resolution
D
Varargs always wins
22

Question 22

Why can you not overload a method solely by changing the parameter name?

A
Parameter names are not part of the method signature
B
The JVM ignores names
C
Only types matter for erasure
D
Names are only for generics
23

Question 23

What is the output when this tricky overload scenario is executed?

java
static void call(Number n) { System.out.println("Number"); }
static void call(Double d) { System.out.println("Double"); }
// call(10);
A
Compilation error — ambiguous
B
Number
C
Double
D
Runtime decision
24

Question 24

Why is this method perfectly legal even though it returns nothing in some paths?

javascript
int next() {
    if (hasNext()) return value++;
    throw new NoSuchElementException();
}
A
All code paths either return a value or throw an exception
B
Return type is optional
C
The compiler ignores missing returns
D
Only void methods allow missing returns
25

Question 25

What is the safest way to write a varargs method that accepts zero or more arguments without allocation overhead?

A
Provide an overloaded version that takes no varargs parameters
B
Use @SuppressWarnings("varargs")
C
Make the method generic
D
There is no way — allocation always occurs
26

Question 26

Why does this method call unexpectedly invoke the Object version?

javascript
void log(String s) { }
void log(Object o) { }
String s = null;
log(s);
A
The compile-time type of s is String — String version is chosen
B
null loses type information
C
Runtime type determines overload
D
Ambiguous
27

Question 27

What is the single most common mistake developers make with method parameters?

A
Believing Java passes objects by reference instead of by value
B
Forgetting to declare return types
C
Using varargs incorrectly
D
Overloading based on return type
28

Question 28

Why is this overload pair surprisingly allowed?

javascript
void process(int... nums) { }
void process(Integer... nums) { }
A
They have different erased parameter types after boxing
B
Varargs of primitive vs wrapper are considered different
C
One is primitive, one is reference — never ambiguous
D
Illegal — should not compile
29

Question 29

When should you prefer method overloading over default parameter values?

A
Java has no default parameters — overloading is the idiomatic way
B
Only for performance
C
Only when using varargs
D
Never — use builder pattern
30

Question 30

What is the output of this subtle overload resolution test?

java
static void test(long x) { System.out.println("long"); }
static void test(Integer x) { System.out.println("Integer"); }
// test(100);
A
long
B
Integer
C
Compilation error
D
Runtime choice
31

Question 31

Why does this varargs method never throw ArrayIndexOutOfBoundsException even when called with no arguments?

javascript
void safe(String first, int... rest) {
    System.out.println(rest.length);
}
A
When no extra arguments are passed, rest becomes a zero-length array
B
rest is null
C
The method cannot be called without arguments
D
Compiler prevents access
32

Question 32

Which of these is NOT a valid method modifier combination?

A
public static final void
B
private abstract void
C
protected synchronized void
D
public native void
33

Question 33

What is the key difference between method overloading and method overriding?

A
Overloading is compile-time polymorphism; overriding is runtime polymorphism
B
Overriding changes parameter types
C
Overloading requires inheritance
D
Only overriding allows covariant returns
34

Question 34

Why is this method signature considered excellent API design?

javascript
public static <T> List<T> of(T... elements)
A
Combines generics, varargs, and immutability for type-safe convenient collection creation
B
Uses varargs to avoid allocation
C
Generic varargs are always safe
D
It returns an array
35

Question 35

After mastering methods and parameters, what is the single most important principle you now fully understand?

A
Java is strictly pass-by-value, overload resolution is compile-time and rule-driven, and varargs are arrays in disguise
B
Objects are passed by reference
C
Overloading behaves like overriding
D
Varargs eliminate allocation

QUIZZES IN Java