About Question enthuware.ocajp.i.v7.2.839 :

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

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.839 :

Post by ETS User »

The question asks to select 2 answers but the solution only has one choice.

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

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by admin »

I see that options 1 and 4 are marked as correct.
q2.839.gif
q2.839.gif (47.79 KiB) Viewed 9642 times
If you like our products and services, please help us by posting your review here.

Guest

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by Guest »

I have a different question from the above for this reference number. It asks me to select one answer and the code is:

Code: Select all

class Great {
    public void doStuff() throws FileNotFoundException{
    }    
}

class Amazing extends Great { 
  public void doStuff() throws IOException, IllegalArgumentException{
  }    
}

public class TestClass {
    public static void main(String[] args) throws IOException{
        Great g = new Amazing();
        g.doStuff();
    }
}

The question asks "How can you fix the following code to make it compile?" and the supposed solution is "Change doStuff in Great to throw only IOException instead of FileNotFoundException".

As far as I can see, this only makes work if java.io.IOException is imported before the code. It will not compile if nothing is imported, like in the actual code given, and will compile without problems if java.io.* is imported.

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

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by admin »

You are right, this statement has now been added to the code. For the purpose of the exam, though, please keep in mind that you may have have to make certain assumptions based on given options. For example, in this case, the question doesn't tell you that the code exists in TestClass.java file. If the file name is something else, it will not compile. But "Remove public from TestClass" is not an option. Therefore, you may assume that the file name is correct. Similarly, import java.io.* is not an option, so you can assume that appropriate imports are present.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

iamslrr
Posts: 6
Joined: Sun Dec 20, 2015 1:23 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by iamslrr »

What impact does the throw in the TestClass main method have on the question? It doesn't seem like it will impact the exception handling of other methods at all. Is it there as an attempt to confuse the issue?

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

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by admin »

I am not sure I understand your question. The code will not compile without the throws clause. You should try it out.
-Paul.
If you like our products and services, please help us by posting your review here.

iamslrr
Posts: 6
Joined: Sun Dec 20, 2015 1:23 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by iamslrr »

When I answered this question I did not think the the exception throw by the main method has an impact on the exceptions thrown by the Amazing and Great classes. I will try it out as you have instructed. Thanks.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by swarna pakeer »

explanation says "The overriding method in the subclass is free to not throw any checked exception at all even if the overridden method throws a checked exception. No exception is a valid subset of exceptions thrown by the overridden method".
this sentence is little confusing as it says subclass overriding method cannot throw any checked exception at all when it can throw subclasses of Exception class of overridden method. and i know it cannot throw new or broader checked exceptions .am i missing something?

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

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by admin »

>>this sentence is little confusing as it says subclass overriding method cannot throw any checked exception at all
No, that is not what that sentence says! Please read carefully.
If you like our products and services, please help us by posting your review here.

alfredo.zuloaga
Posts: 10
Joined: Fri Nov 18, 2016 5:48 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by alfredo.zuloaga »

If you do
Change doStuff in Amazing to throw only IOException.
also works
lass Great {
public void doStuff() throws IOException {}
}

class Amazing extends Great {
public void doStuff() throws IOException, IllegalArgumentException {}
}

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

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by admin »

No, it doesn't work. Check your code for class Great. It is not the same as what is given in the problem statement.
If you like our products and services, please help us by posting your review here.

af1981
Posts: 11
Joined: Thu Jun 04, 2020 3:19 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by af1981 »

Good Morning,

this question has got 2 right answers. The added one is:

1. Change doStuff in Amazing to throw only IOException;
2 Change doStuff in Great to throw only IOException instead of FileNotFoundException.

Making the code below to compile too. Thanks for your support. Have a good day and a good work.

import java.io.*;
class Great {
public void doStuff() throws IOException{
}
}

class Amazing extends Great {
public void doStuff() throws IOException{
}
}

public class TestClass {
public static void main(String[] args) throws IOException{
Great g = new Amazing();
g.doStuff();
}
}

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

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by admin »

Not sure I understand what you are saying. Are you saying the answers given in the question are incorrect?
If you like our products and services, please help us by posting your review here.

klaashielke
Posts: 1
Joined: Sun Jul 17, 2022 3:32 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by klaashielke »

I came here for the same reason as user "af1981". Also to me it seems that multiple answer combinations are correct for this question. Answer C: "change doStuff() in Amazing to only throw IOException" and answer D "change doStuff() in Great to only throw IOException" will result in the super class, sub class, and main method all only throwing an IOException, which compiles.

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

Re: About Question enthuware.ocajp.i.v7.2.839 :

Post by admin »

The problem statement explicitly says, "Assume that changes suggested in a option are to be applied independently of changes suggested in other options."
So you have to consider each option individually and not in combination of any other option.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 37 guests