Name : ..................................................................... Comp 240 Quiz Written 4 ID # : ..................................................................... May 26, 2005 1/3 State clearly all of your assumptions. Good luck. Q1 (51) Q2 (49) Sum 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? protected. this. public. super. 1.b. (6 pt) What happens if the superclass’s constructor does not assign a value to an instance variable? A syntax error occurs. A compile-time error occurs. 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? A billing program where there is a variety of clients who are billed with different fee structures. 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. 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: real classes. instance classes. insatiable classes. concrete classes. Name : ..................................................................... Comp 240 Quiz Written 4 ID # : ..................................................................... May 26, 2005 2/3 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: because the subclass has an object of its superclass. because the subclass is an object of its superclass. only when the superclass is abstract. only when the superclass is concrete. 1.h. (7 pt) If “class A extends B”, then: Class A is the base class. Class A is the superclass. Class A inherits from class B. Class B inherits from class A. Class B is the derived class. Name : ..................................................................... Comp 240 Quiz Written 4 ID # : ..................................................................... May 26, 2005 3/3 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 A a = new A(); a.m(); a.n(); B b = new B(); b.m(); b.n(); C c = new C(); c.m(); c.n(); A a = new B(); a.m(); a.n(); A a = new C(); a.m(); a.n(); A a = new C(); a.p(); C c = new C(); c.p(); Result