sample questions from the AP COMPSCI course description

Shahein
Sat, 30 Apr 2005 11:04:34 +0000

Because of polymorphism.

Base b = new Derived()

The actual class we are talking about is an instance of derived. Thus, calling b.methodOne() invokes methodOne of the derived class. The derived class calls the superclass’s methodOne which prints A. Then the superclass methodOne calls methodTwo. methodTwo is declared for class Derived, so it calls methodTwo in Derived, which calls the superclass methodTwo which prints B. Then D is printed since program control reverts to the proper method in Derived. Then C is printed because program control reverts back to Derived’s method one which prints C last.