About Question com.enthuware.ets.scjp.v6.2.649 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
chang_surrey
Posts: 7
Joined: Wed Dec 04, 2013 5:07 pm
Contact:

About Question com.enthuware.ets.scjp.v6.2.649 :

Post by chang_surrey »

May I ask why option 5 is wrong?

"Overriding method may choose not to throw any exception."

I chose option 3 because class A obviously doesn't compile, but I'm curious what makes option 5 a wrong answer.

Since the original method only throws an unchecked NPE, I assume its overriding version is free to throw nothing at all? Thanks!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Post by admin »

It is true that the overriding method may choose not to throw any exception. But that implies that the given code is fine, which it is not. That is why that option is incorrect.
Since you have to select one option, you need to select the best option.
If you like our products and services, please help us by posting your review here.

makemoneyy
Posts: 1
Joined: Mon Mar 02, 2015 8:56 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Post by makemoneyy »

This question is ambigious and should be avoided. Specially when multiple answer can be selected. I see the option 1, 3, and 5 are valid.

msalam4
Posts: 3
Joined: Sat Feb 27, 2016 1:55 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Post by msalam4 »

Can you please explain why option 1 is wrong? Thanks.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Post by admin »

Please read the discussion above.
If you like our products and services, please help us by posting your review here.

fortesp
Posts: 9
Joined: Mon Dec 14, 2020 8:53 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Post by fortesp »

I am confused...
Overriding method cannot throw IOException
The following snippet compiles:

Code: Select all

class A {
    byte getStatusCode(Object obj) throws NullPointerException {
        if (obj != null) return 127;
        else return -1;
    }
}

class B extends A {
    byte getStatusCode(Object obj) throws IOException {
        if (obj != null) return 127; // changed from 128 to 127.. for the example
        else throw new IOException();
    }
}
And then...
Since IOException is a checked exception and is not a subclass of NullPointerException, the overriding method cannot throw this exception.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Post by admin »

fortesp wrote:
Fri Feb 26, 2021 8:30 am
The following snippet compiles:
....
Are you sure?
If you like our products and services, please help us by posting your review here.

fortesp
Posts: 9
Joined: Mon Dec 14, 2020 8:53 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.649 :

Post by fortesp »

You are right! My IDE did not show any warning... :x

Its funny though. If i leave just the throws clause it compiles, but if i throw the exception it does not compile. So in terms of method override, i can use throws but not throw an actual exception.

Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests