What will happen when you try to compile and run this code? : Questions « Type Casting « SCJP






class ApBase implements Runnable {
  public void run() {
  }

}

public class MainClass {
  public static void main(String[] argv) {
    ApBase aBase = new ApBase();
    Runnable aR = aBase;
    Object obj = aR;
    ApBase x = (ApBase) obj;

  }
}
    
A.      The compiler objects to line 2.
B.      The compiler objects to line 3.
C.      The code compiles but when run throws a ClassCastException in line 4.
D.      The code compiles and runs without a problem.








4.6.Questions
4.6.1.Object references can be converted in both method calls and assignments, and the rules are identical(True/False)
4.6.2.Answer: reference method call and assignment
4.6.3.What is the output(object and primitive type)?
4.6.4.Answer: object and primitive type
4.6.5.What is the output(object type conversion)?
4.6.6.Answer: object type conversion
4.6.7.What is the output(interface type)?
4.6.8.Answer: interface type
4.6.9.What is the output(boolean operator and short type)?
4.6.10.Answer: boolean operator and short type
4.6.11.What are the possible values of b in the following code?
4.6.12.Answer: casting between long and byte
4.6.13.What are the possible values of the result l in the following code?
4.6.14.Answer: assignment between byte and long
4.6.15.For the following hierarchy and code, Which of the following saveEvent calls would run without an exception?
4.6.16.Answers: exception hierarchy
4.6.17.Which of the following Java statements will compile and execute without error?
4.6.18.Answer: implement Runnable interface
4.6.19.What will happen when you try to compile and run this code?
4.6.20.Answer: interface casting
4.6.21.What will happen when you call this method with int primitives as in the following fragment?
4.6.22.Answer: method overloading
4.6.23.Which of the following Java code fragments compiles and executes without error?
4.6.24.Answer: interface type casting
4.6.25.What will be the result of trying to compile the following class?
4.6.26.Answer: instanceof object