About Question enthuware.ocajp.i.v7.2.839 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.839 :
The question asks to select 2 answers but the solution only has one choice.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
I see that options 1 and 4 are marked as correct.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.839 :
I have a different question from the above for this reference number. It asks me to select one answer and the code is:
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.
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.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Sun Dec 20, 2015 1:23 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
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?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
I am not sure I understand your question. The code will not compile without the throws clause. You should try it out.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Sun Dec 20, 2015 1:23 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
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.
-
- Posts: 16
- Joined: Thu Mar 19, 2020 2:27 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
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?
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?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
>>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.
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.
-
- Posts: 10
- Joined: Fri Nov 18, 2016 5:48 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
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 {}
}
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 {}
}
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
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.
-
- Posts: 11
- Joined: Thu Jun 04, 2020 3:19 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
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();
}
}
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();
}
}
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
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.
-
- Posts: 1
- Joined: Sun Jul 17, 2022 3:32 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
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.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.839 :
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.
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.
Who is online
Users browsing this forum: No registered users and 26 guests