nerdexam
Oracle

1Z0-809 · Question #57

Given: public class Msg { public static String doMsg(char x) { return "Good Day!"; } public static String doMsg(int y) { return "Good Luck!"; } public static void main(String[] args) { char x = '8'…

The correct answer is A. Good Day! Good Luck! Option A is correct because Java resolves overloaded methods at compile time based on the declared type of the argument, not its value. x is declared as char, so doMsg(x) binds to doMsg(char) → "Good Day!"; z is declared as int (Java widened '8''s Unicode value 56 into it), so…

Question

Given: public class Msg { public static String doMsg(char x) { return "Good Day!"; } public static String doMsg(int y) { return "Good Luck!"; } public static void main(String[] args) { char x = '8'; int z = '8'; System.out.println(doMsg(x)); System.out.println(doMsg(z)); } } What is the result?

Options

  • AGood Day! Good Luck!
  • BGood Day! Good Day!
  • CGood Luck! Good Day!
  • DGood Luck! Good Luck!
  • ECompilation fails

How the community answered

(62 responses)
  • A
    71% (44)
  • B
    5% (3)
  • C
    16% (10)
  • D
    6% (4)
  • E
    2% (1)

Explanation

Option A is correct because Java resolves overloaded methods at compile time based on the declared type of the argument, not its value. x is declared as char, so doMsg(x) binds to doMsg(char) → "Good Day!"; z is declared as int (Java widened '8''s Unicode value 56 into it), so doMsg(z) binds to doMsg(int) → "Good Luck!".

Why the distractors fail:

  • B & D assume both calls resolve to the same overload, ignoring that x and z have different declared types.
  • C reverses the logic - it would require char to resolve to the int overload and vice versa, which contradicts Java's type-matching rules.
  • E is wrong because the code is perfectly valid; both calls have an exact type match to an available overload.

Memory tip: Think "declared type drives dispatch." The line int z = '8' may look like a char, but once stored in an int, the compiler only sees int when choosing which overload to call.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice