1Z0-808 · Question #69
Given: class Overloading { int x(double d) { System.out.println("one"); return 0; } String x(double d) { System.out.println("two"); return null; } double x(double d) { System.out.println("three")…
The correct answer is D. Compilation fails. Option D is correct because Java does not consider return type as part of a method's signature for overloading purposes. All three x methods have identical signatures - same name, same parameter type (double) - making them duplicates in the eyes of the compiler, which reports a…
Question
Options
- Aone
- Btwo
- Cthree
- DCompilation fails.
How the community answered
(32 responses)- A9% (3)
- B3% (1)
- D88% (28)
Explanation
Option D is correct because Java does not consider return type as part of a method's signature for overloading purposes. All three x methods have identical signatures - same name, same parameter type (double) - making them duplicates in the eyes of the compiler, which reports a compilation error.
Options A, B, and C are wrong because the program never reaches runtime; no output is ever printed when compilation fails. The JVM cannot execute code that doesn't compile, so none of the three println statements can run.
Memory tip: In Java, overloading requires the parameter list to differ (number, type, or order of parameters). Return type is irrelevant - the compiler can't infer which method you intended to call based on what you plan to do with the return value.
Topics
Community Discussion
No community discussion yet for this question.