The following question was taken from a Whizlabs simulation exam topic
01 - Class Design
What is the result?
B. The code runs with no output.
C. Compilation fails because of an error in line 12.
D. Compilation fails because of an error in line 15
E. Compilation fails because of an error in line 18.
Correct answer is choice A.
When overriding a method in the parent class, the method in the sub class can only throw an exception which is defined in the parent class method signature.
It can also throw exceptions which are more specific (subclasses of the exception being thrown in the parent class) than defined in the super class method. In the given problem,
class bar is extending class foo and also overrides the method process(). Since the method process() in class foo throws IOexception,
the overriding method can throw IOException or no exception. On execution, the given code will run successfully and will invoke the overridden process method which will print Bar.
My Observations: In order for this code to compile, the "main" method has to belong to a
top level class that encloses the 2 static classes.
The author of the question must be assuming that
the reader understands that in order for the 2 static classes to compile,
they must be enclosed by a non-static public class.
Answer A is correct if the static classes are enclosed by a non-static top level class as shown below.
Please confirm that the reader must make the assumption that both static classes have to be
enclosed by a top level class.
Have a look at the line numbers. One of the conventions in the exams is that if the line numbers don't start at 1 then there is additional code before the code shown, and you can assume that code is correct, and won't be very relevant to the answer. In this case that means you can assume the code you see is embedded in a class.