nerdexam
Oracle

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…

Working with Methods and Encapsulation

Question

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"); return 0.0; } public static void main(String[] args) { new Overloading().x(4.0); } } What is the result?

Options

  • Aone
  • Btwo
  • Cthree
  • DCompilation fails.

How the community answered

(32 responses)
  • A
    9% (3)
  • B
    3% (1)
  • D
    88% (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

#method overloading#method signature#return type rules#compilation error

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice