What will be the result of trying to compile the following class? : Questions « Type Casting « SCJP






import java.io.Serializable;

public class MainClass {
  public static void main(String args[]) {
    String[] names = new String[10];
    if (names instanceof Object[])
      System.out.println("Obj[] true");
    if (names instanceof Object)
      System.out.println("Obj true ");

    double d = Math.sqrt(-1.0);

    int x = (int) d;
    System.out.println("d= " + d + " cast to int Value = " + x);
    long lx = Long.MAX_VALUE;
    float f = lx;
    int[] nnn = new int[1000];
    Object obj = nnn;
    Cloneable clobj = nnn;
    Serializable sb = nnn;
    int[] xxx = (int[]) nnn.clone();
  }
}








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