1Z0-811 · Question #16
Given public class App { int num; public int add(int x) { num = num + x; return num; } public void add(int x) { num = num + x; } public static void main (String[] args) { App obj = new App()…
The correct answer is D. A compilation error occurs. D is correct because Java does not allow method overloading based solely on return type - both add(int x) methods have identical parameter lists, so the compiler cannot distinguish between them and throws a compilation error before the program ever runs. The distractors (A, B…
Question
Options
- A300
- B100
- C200
- DA compilation error occurs.
How the community answered
(49 responses)- A10% (5)
- B6% (3)
- C4% (2)
- D80% (39)
Explanation
D is correct because Java does not allow method overloading based solely on return type - both add(int x) methods have identical parameter lists, so the compiler cannot distinguish between them and throws a compilation error before the program ever runs.
The distractors (A, B, C) all assume the program executes, but since it never compiles, no output is produced: 200 would require both calls to succeed, 100 would suggest only one call ran, and 300 would imply some triple accumulation - none of which can happen.
Memory tip: Think of Java's method signature as a name tag that includes only the method name and parameter types (not the return type). Two methods with the same name tag are always illegal, regardless of what they return.
Topics
Community Discussion
No community discussion yet for this question.