wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes OCP exams - Overriding question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "OCP exams - Overriding question" Watch "OCP exams - Overriding question" New topic
Author

OCP exams - Overriding question

matej spac
Greenhorn

Joined: Apr 21, 2011
Posts: 28
Hi,
I just found question in OCP Practise Exams book:

Exam 2, Q:29:

3. import java.io.*;
4. class Physicist {
5. void think() throws IOException {}
6. }
7. public class Feynman extends Physicist {
8. public static void main(String[] args) {
9. new Feynman().think();
10. }
11. // insert method here
12. }

Which compiles?

A. void think() throws Exception {}
B. void think() throws FileNotFoundException {}
C. public void think() {}
D. protected void think() throws IOException {}
E. private void think() throws IOException {}
F. void think() { int x = 7/0; }


I mark C and F as correct answers. But in book as correct answes is marked B, C, D and F.
How could be B and D correct? They throw an exception , but in main() method is not declared that it could throw exception, so I think that B and D could not be true.
Please somebody explain me what's going on here.

|OCPJP 6|
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

B. void think() throws FileNotFoundException {}
FileNotFoundException extends IOException, and hence is a narrower exception. You can throw it. But you cant throw broader exceptions like Exception.


Sorry, for the error. I complied the code and got an error. What you are saying is correct.
The code would compile if think() is not called in main OR if main declares or handles the Exception.


D.protected void think() throws IOException {}
This compiles when i comment think(); in main()
I am not so confident about this one. The overriding method may not have a more restrictive modifier. void think() throws IOException {} is public. Then how is "D" compiling ?

According to me - access modifiers in order of their decreasing restrictiveness :
Private > Default > Protected > Public

Is there something wrong in this approach ?


SCJP 6. Learning more now.
matej spac
Greenhorn

Joined: Apr 21, 2011
Posts: 28
In my opinion: B, C, D and F are all legal overriding.

But the question under the given class is exactly: "Which of the following methods, inserted independently at line 11, compiles? (Choose all that aply.)"
And the thruth is that only C and F compiles - because B and also D throws an exception, that is not declarad or handled in main().

So is there a bug in book or I do┬┤nt understand something?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
It could be a mistake in the book, but who in this thread actually ran the code and tried all six options?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
Ok, I tried to compiled all the options.

First the code:



Now, the errors:
For A:

For B:

For C:

For D:

For E:


For F, it compiled but when I ran it,

Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
In order for B and D to be correct:



Lines 9 and 12 need to be added. For F, it will run fine as what I think, is an overridden method. Not sure if it is, or is it overloaded...

For C to run, it has to be like this:

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
I haven't followed the details, but in the book, ALL the options include { }, including option C.

Where does that leave us?
matej spac
Greenhorn

Joined: Apr 21, 2011
Posts: 28
sorry, my mistake, off cource, option C is defined in book: "C. public void think() {}"

but it does not change my opinion, that only C and F are correct :-)
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

matej spac wrote:

but it does not change my opinion, that only C and F are correct :-)

Ikpefua wrote:

Hello all, @Matej I agree with you, (to the best of my knowledge) this code AS IT STANDS is testing your knowledge on two FUNDAMENTAL things;

1. The rules of overriding

2. The rules of Declare or Handle

In this case C and F are correct.

A. Is wrong because the overriding method CANNOT declare broader Exceptions (Even if it declares the adequate exception, it will still NOT compile because the main() method fails to comply with the obligation of Declare or Handle)

B. Is wrong because the main() method fails to comply with the obligation of Declare or Handle

D. Is wrong for reasons already mentioned in B.

E. is wrong because overriding method CANNOT use a more restrictive modifier (Even if it uses the adequate modifier, it will still NOT compile because the main() method fails to comply with the obligation of Declare or Handle)


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Wow, there IS an error! Thanks guys for the catch!

main() should throw Exception.

Bert
 
Don't get me started about those stupid light bulbs.
 
subject: OCP exams - Overriding question