Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/how-does-java-autoboxing-work
Question 126 / 297:  Given that Long and Integer extend Number, what is the result of compiling and running the following code?
public class Tester {

public static void main(String[] args) {

Number x = 12; // Line 5

Number y = (Long) x; // Line 6

System.out.print(x+""+y); // Line 7

}

}

A  1212
B  Compilation error at line 5
C  Compilation error at line 6
D  Compilation error at line 7
E  An exception is thrown at run time
<< First < Previous Next > Last >>
Explanation:

x will refer to an Integer object because 12 is by default of type int and will be wrapped to an Integer object. At compile time the casting works fine since Long and Number in the same type tree but at run time it will fail and a classCastException will be thrown because java.lang.Integer cannot be cast to java.lang.Long.

Exam Home Page
https://www.jobilize.com/java-certification-questions

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Donyea Sweets
Start Test
Rachel Carlisle
Start Quiz
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>