Java Method Overloading with Binary Literals

1Z0-829 By islever Editorial Last updated

Short answer

C The binary literal resolves to int, calling the print(int) method which outputs hello.

Question

Given: What is the result? - Exhibit: Java Method Overloading with Binary Literals
  1. Compilation fails.
  2. there
  3. hello Correct Answer
  4. A NumberFormatException is thrown.

Key Takeaways

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.

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

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 →

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.

Discussion 4 contributions

Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.

From public community threads 4

  1. james2033 👍 4 Answered 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
  2. minhdev 👍 1
    Auto promote to int C is correct
  3. Uteman 👍 1
    C is the correct answer
  4. xplorerpj 👍 1 Answered C
    C is correct answer

Reader comments & corrections

No reader corrections 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.

← Back to 1Z0-829 Study Guide