Multiple Choice Questions 8
Analyze the following code: class
CircleM
{ private double radius;
public Circle(double radius)
{ radius = radius; }
}
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.
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);
}
}
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.
Analyze the following code:
Cylinder cy = new Cylinder(1,1);
Circle c = cy;
The code has a syntax error.
The code has a runtime error.
The code is fine.
Analyze the following code:
Circle c = new Circle (5);
Cylinder c = cy;
The code has a syntax error.
The code has a runtime error.
The code is fine.
Which of the following class definitions defines a legal abstract class?
class A
{ abstract void unfinished()
{ }
}
class A
{ abstract void unfinished();
}
abstract class A
{ abstract void unfinished();
}
public class abstract A
{ abstract void unfinished();
Which of the following classes cannot be extended?
class A
{ }
class A
{ private A(); }
final class A
{ }
native class A
{ }
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);
}
}
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.
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);
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();
Which of the following expressions evaluates to false?
c1 instanceof C1
c2 instanceof C1
c3 instanceof C1
c4 instanceof C2
Which of the following statements will not convert a string s into an integer?
i = Integer.parseInt(s);
i = (new Integer(s)).intValue();
i = Integer.valueOf(s).intValue();
i = Integer.valueOf(s);
Visited
times since 7 March 1999.