Multiple Choice Questions 8

  1. Analyze the following code: class

    CircleM
    { private double radius;
    public Circle(double radius)
    { radius = radius; }
    }

  2. The program will not compile because it does not have a main method.
    The program will compile, but you cannot create an object of Circle with specified radius.
    The program has a compilation error because you cannot assign radius to radius.

  3. Suppose you create a class Cylinder to be a subclass of Circle.
    Analyze the following code:

    class Cylinder extends Circle
    { double length = 1;
    Cylinder(double radius)
    { Circle(radius);
    }
    }

  4. The program compiles fine, but you cannot create an instance of Cylinder because the constructor does not specify the length of the cylinder.
    The program has a syntax error because you attempted to invoke the Circle class's constructor illegally.
    The program compiles fine, but it has a runtime error because of invoking the Circle class's constructor illegally.

  5. Analyze the following code:

    Cylinder cy = new Cylinder(1,1);
    Circle c = cy;

  6. The code has a syntax error.
    The code has a runtime error.
    The code is fine.

  7. Analyze the following code:

    Circle c = new Circle (5);
    Cylinder c = cy;

  8. The code has a syntax error.
    The code has a runtime error.
    The code is fine.

  9. Which of the following class definitions defines a legal abstract class?

  10. class A
    { abstract void unfinished()
    { }
    }
    class A
    { abstract void unfinished();
    }
    abstract class A
    { abstract void unfinished();
    }
    public class abstract A
    { abstract void unfinished();

  11. Which of the following classes cannot be extended?

  12. class A
    { }
    class A
    { private A(); }
    final class A
    { }
    native class A
    { }

  13. Analyze the following code:

    class Test extends A
    { public static void main(String[] args)
    { Test t = new Test();
    t.print();
    }
    }
    class A
    { String s;
    A(String s)
    { this.s = s; }
    public void print()
    { System.out.println(s);
    }
    }

  14. The program does not compile because Test does not have a constructor Test().
    The program would compile if the constructor in the class A were removed.
    The program compiles, but it has a runtime error due to the conflict on the method name print.
    The program runs just fine.

  15. Given the following classes and their objects,
    class C1 {};
    class C2 extends C1 {};
    class C3 extends C1 {};
    C2 c2 = new C2();
    C3 c3 = new C3();
    Analyze the following statement:
    c2 = (C2)((C1)c3);

  16. c3 is cast into c2 successfully.
    You will get a compilation error because you cannot cast objects from sibling classes.
    You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form.
    d. The statement is correct.
    Given the following code,
    class C1 {}
    class C2 extends C1 { }
    class C3 extends C2 { }
    class C4 extends C1 {}
    C1 c1 = new C1();
    C2 c2 = new C2();
    C3 c3 = new C3();
    C4 c4 = new C4();

  17. Which of the following expressions evaluates to false?

  18. c1 instanceof C1
    c2 instanceof C1
    c3 instanceof C1
    c4 instanceof C2

  19. Which of the following statements will not convert a string s into an integer?

  20. i = Integer.parseInt(s);
    i = (new Integer(s)).intValue();
    i = Integer.valueOf(s).intValue();
    i = Integer.valueOf(s);

Visited times since 7 March 1999.