Name : Comp 240 Quiz Written 4
ID # : May 26, 2005
3/4
State clearly all of your assumptions.
Good luck.
Q1. (51 pt)
In the following questions, check all that apply.
1.a. (6 pt)
Which of the following keywords allows a subclass to access a superclass method even when the subclass has overridden the superclass method?
o protected.
o this.
o public.
þ super.
1.b. (6 pt)
What happens if the superclass’s constructor does not assign a value to an instance variable?
o A syntax error occurs.
o A compile-time error occurs.
o A run-time error occurs.
þ The program compiles and runs correctly.
1.c. (6 pt)
Which of the following statements is/are true?
þ We can use inheritance to customize existing software.
þ A superclass specifies commonality.
þ A superclass can be modified without modifying subclasses
þ A subclass can be modified without modifying its superclass.
1.d. (6 pt)
For which of the following would polymorphism not provide a clean solution?
o A billing program where there is a variety of clients who are billed with different fee structures.
o A maintenance log program where a variety of machine data is collected and maintenance schedules are produced for each machine based on the data collected.
þ A program to compute a 5% savings account interest for a variety of clients.
Because there is only one kind of calculation, there is no need for polymorphism.
o A program that maintains information on a variety of taxpayers and determines who to audit based on criteria for classes of taxpayers.
1.e. (6 pt)
Non-abstract classes are called:
o real classes.
o instance classes.
o insatiable classes.
þ concrete classes.
1.f. (7pt)
Consider the abstract superclass below:
public abstract class Foo
{
private int a;
public int b;
public Foo( int aVal, int bVal )
{
a = aVal;
b = bVal;
} // end Foo constructor
public abstract int calculate();
} // end class Foo
Any concrete subclass that extends class Foo:
þ Must implement a method called calculate.
þ Will not be able to access the instance variable a.
þ Will not be able to instantiate an object of class Foo.
1.g. (7 pt)
Assigning a subclass reference to a superclass variable is safe:
o because the subclass has an object of its superclass.
þ because the subclass is an object of its superclass.
o only when the superclass is abstract.
o only when the superclass is concrete.
1.h. (7 pt)
If “class A extends B”, then:
o Class A is the base class.
o Class A is the superclass.
þ Class A inherits from class B.
o Class B inherits from class A.
o Class B is the derived class.
Q2. (49 pt)
Consider the following class declarations A, B, and C. Does the following code segments compile or run correctly? If no, why? If yes, what is the output? (Each 7 pt)
public abstract class A {
public abstract void m();
public void n() {
System.out.println("n at A");
}
}
public class B extends A {
public void m() {
System.out.println("m at B");
}
}
public class C extends A {
public void m() {
System.out.println("m at C");
}
public void n() {
System.out.println("n at C");
}
public void p() {
System.out.println("p at C");
}
}
Code Segment / ResultA a = new A();
a.m();
a.n(); / Compilation error.
Cannot instantiate the type A
B b = new B();
b.m();
b.n(); / m at B
n at A
C c = new C();
c.m();
c.n(); / m at C
n at C
A a = new B();
a.m();
a.n(); / m at B
n at A
A a = new C();
a.m();
a.n(); / m at C
n at C
A a = new C();
a.p(); / Compilation error.
The method p() is undefined for the type A
C c = new C();
c.p(); / p at C