This is a closed notes, closed book exam. All of the problems on this exam, refer to the additional classes that are displayed on the projection screen. You should assume that all of the classes are in a single directory. There are no syntax errors in the classes displayed on the screen. For each problem there are three possible answers. First, the class in the problem may contain a syntax error, if so, identify the error and correct it. Second, executing the class may result in a runtime error (i.e. throw an exception), if so, identify the error. Third, there may be no errors, if so, show what is printed by the program. All errors are related to inheritance and dynamic method dispatch.
Hint: Only two problems contain syntax errors. In the first such problem the error can be corrected by inserting characters, no deletions are required. In the second such problem one identifier needs to be changed. There is only one problem that will generate a runtime error.
class Quiz3Prob1 {
public static void main(String[] args)
{
ClassOne one = new ClassOne();
ClassTwo two = new ClassTwo();
one.print();
two.print();
one = two;
one.print();
}
}
No errors. The output is:
inside ClassOne inside ClassTwo inside ClassTwo
class Quiz3Prob2 {
public static void main(String[] args)
{
ClassOne one = new ClassTwo();
ClassTwo two;
one.print();
// two = one;
two = (ClassTwo)one; // need to explicitly cast to subtype
two.print();
}
}
This cast works because one is actually referring to an object of
type ClassTwo. Once the error is fixed, the output is:
inside ClassTwo inside ClassTwo
class Quiz3Prob3 {
public static void main(String[] args)
{
ClassOne one = new ClassOne();
ClassTwo two = new ClassTwo();
two.bar();
two = (ClassTwo)one; //Runtime error here
two.bar(); //not here
}
}
There are no syntax errors, but the cast, (ClassTwo)one, will fail
at runtime because one is not actually referring to a ClassTwo object
in this particular case. Although a compiler should have been able
to figure it out in this simple example, it cannot be determined in
general. The error occurs at the point of the cast, not at the last line, where
bar() is called for the second time.
class Quiz3Prob4
{
public static void main(String[] args)
{
ClassThree three = new ClassFour();
three.print(); // dynamic dispatch will take us to print in ClassFour
three.foo();
}
}
No errors. The output is:
In print from ClassFour In foo from ClassThree
class Quiz3Prob5
{
public static void main(String[] args)
{
ClassFour four = new ClassFour();
four.print();
four.foo(); // Here we call the inherited foo from ClassThree
}
}
No errors. The output is:
In print from ClassFour In foo from ClassThree
class Quiz3Prob6
{
public static void main(String[] args)
{
ClassThree three = new ClassThree(); // Can't instantiate an abstract class
three.print(); // the problem is not here as the earlier examples showed
three.foo();
}
}
If you change new ClassThree to new ClassFour, then of course we
get the same output as the previous two problems.