Java Method Overloading with Binary Literals

Answer Correct answer: C — The binary literal resolves to int, calling the print(int) method which outputs hello.

Given: What is the result? - image

  1. Compilation fails.
  2. there
  3. hello Correct Answer
  4. A NumberFormatException is thrown.

Community Votes

C
100%

100% of anonymous learners picked answer C. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

The exam tests whether you know that binary literals default to int unless suffixed with 'L', which traps candidates expecting long promotion.

This question tests Java method overloading resolution when passing a binary literal to overloaded integer types. It establishes that the literal resolves to int, invoking the int-based method.

Candidates often choose B because they assume the large binary value forces promotion to long, overlooking Java's literal type rules.

Community Discussion (4 comments)

Uteman 👍 1
C is the correct answer
xplorerpj 👍 1 Selected: C
C is correct answer
minhdev 👍 1
Auto promote to int C is correct
james2033 👍 4 Selected: C
package q10; public class Main { void print(int i) { System.out.println("hello"); } void print(long j) { System.out.println("there"); } public static void main(String[] args) { new Main().print(0b1101_1010); } } // Result: // hello

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

The correct answer is C because the binary literal 0b1101_1010 is treated as an int by default in Java. The print(int i) method is invoked, printing "hello".

Why the Other Options Are Wrong

Option B is incorrect because the literal does not automatically become a long; it only becomes a long if suffixed with L. Option A is wrong as the code compiles successfully. Option D is incorrect because the value fits within an int range.

Community Comment Notes

The community overwhelmingly agrees with C. As user james2033 noted, the result is "hello" because the binary literal defaults to int. Others confirmed that auto-promotion to int logic applies here for standard binary literals.

Exam Strategy

Always check the suffix of numeric literals. If there is no 'L', binary, hex, and octal literals are int type, regardless of their magnitude (as long as they fit in int).

Frequently Asked Questions

Does a binary literal ever default to long?

Only if it is explicitly suffixed with an uppercase L (e.g., 0b1101_1010L). Otherwise, it is always an int.

Why doesn't the large value cause overflow or exception?

The value 0b1101_1010 (218 decimal) fits comfortably within the positive range of a 32-bit signed int.

More 1Z0-829 FAQ →

Related Analysis

← Back to 1Z0-829 Study Guide