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
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)- A71% (44)
- B5% (3)
- C16% (10)
- D6% (4)
- E2% (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
xandzhave different declared types. - C reverses the logic - it would require
charto resolve to theintoverload 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.