a. The program has a syntax error because Test1 does not have a main
method.
b. The program has a syntax error because o1 is an Object instance
and it does not have the compareTo method.
c. The program has a syntax error because you cannot cast an Object
instance o1 into Comparable.
d. The program would compile if ((Comparable)o1.compareTo(o2) >= 0)
is replaced by (((Comparable)o1).compareTo(o2) >= 0).
e. b and d are both correct.
#
7. Which of the following statements regarding abstract methods is not
true?
a. An abstract class can have instances created using the constructor
of the abstract class.
b. An abstract class can be extended.
c. A subclass of a non–abstract superclass can be abstract.
d. A subclass can override a concrete method in a superclass to declare
it abstract.
e. An abstract class can be used as a data type.
#
8. Which of the following possible modifications will fix the errors in
this code?
public class Test {
private double code;
public double getCode() {
return code;
}
protected abstract void setCode(double code);
}
a. Remove abstract in the setCode method declaration.
b. Change protected to public.
c. Add abstract in the class declaration.
d. b and c.
#
9. Analyze the following code.
class Test {
public static void main(String[] args) {
Object x = new Integer(2);
System.out.println(x.toString());
}
}
a. The program has syntax errors because an Integer object is
assigned to x.
b. When x.toString() is invoked, the toString() method in the Object
class is used.