Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/casting-objects-in-java
Question 62 / 297:  Would the following code compile and if yes , what is the output?
interface Colorable {}

class Vehicle {}

class Car extends Vehicle implements Colorable {}

public class Tester {

public static void main(String[] args) {

Vehicle a = new Car();

Colorable i = (Colorable) a;

Vehicle b = new Vehicle();

Colorable j = (Colorable) b;

}

}

Oracle Certified Professional Java Programmer Yasser Ibrahim casting objects in java
A  The code compiles fine and produces no output
B  The code compiles fine but throws a RuntimeException because of the second casting
C  There is a compilation error because of the second casting
<< First < Previous Next > Last >>
Explanation:

The compiler doesn't object as long as the casting type is compatible with the declaration type

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

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Hannah Sheth
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>