1z0-829 Exam QuestionsBrowse all questions from this exam

1z0-829 Exam - Question 10


Given:

What is the result?

Show Answer
Correct Answer: BC

The method new Main().print(0b1101_1010) calls the print method with the binary value 0b1101_1010, which is 218 in decimal. In Java, a binary literal prefixed with 0b is considered an integer. However, 218 exceeds the range of a byte type (which is -128 to 127) and therefore fits within the range of an int. But since both int and long can accommodate 218, Java will prefer the more specific method when possible. Thus, it will choose print(long j) because method resolution in Java chooses the narrowest possible type when an exact match isn't found, leading to the result 'there'.

Discussion

4 comments
Sign in to comment
james2033Option: C
Feb 15, 2024

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

minhdevOption: C
May 5, 2024

Auto promote to int C is correct

xplorerpjOption: C
Jun 26, 2024

C is correct answer

UtemanOption: C
Jul 2, 2024

C is the correct answer