• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Missing Top Level Class

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following question was taken from a Whizlabs simulation exam topic
01 - Class Design

Given :


What is the result?

A. Bar
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.

Answer: A
Explanation:
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.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, they must.

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.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic