Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OCP exams - Overriding question

 
matej spac
Greenhorn
Posts: 28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

 
matej spac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could be a mistake in the book, but who in this thread actually ran the code and tried all six options?
 
Francis Zabala
Ranch Hand
Posts: 32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, there IS an error! Thanks guys for the catch!

main() should throw Exception.

Bert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic