Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Question 6 val's mock

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can someone explain why the correct answer is E?
import java.io.*;
public class Question05 {
public static void main(String[] args) {
Question05Sub myref = new Question05Sub();
try{
myref.test();
}
catch(IOException ioe){}
}
void test() throws IOException{
System.out.println("In Question05");
throw new IOException();
}
}
class Question05Sub extends Question05 {
void test() {
System.out.println("In Question05Sub");
}
}


A. Prints:
In Question05Sub

B. Prints:
In Question05

C. Prints:
In Question05
In Question05Sub

D. Prints:
In Question05Sub
In Question05

E. The code does not compile.
Any help would be much appreciated,
Rowan.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually this is question 5 but whatever
Explanations:
The code does not compile because no IOException is thrown when invoking myref.test().
Note that myref's declared and runtime types are Question05Sub and thus no dynamic lookup will be performed.
However, if you change the declared type to Question05, the code will compile and the correct answer would be A because method test() is overridden in Question05Sub.
Compilation error:

[ March 15, 2002: Message edited by: Valentin Crettaz ]
 
Rowan Chattaway
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read this explanation before, sorry but I still don't understand!.
I thought because myref's declared and runtime types are Question05Sub then it would simply call method test() in Question05Sub, and so no exception would be thrown and the correct answer would be:
A. Prints:
In Question05Sub
what does "The code does not compile because no IOException is thrown when invoking myref.test()." mean??
Why would this stop compilation? can you not put code in a try block that doesn't have the potential to cause exceptions?
Can you please try and explain this to my simple greenhorn mind?
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rowan
What you thought is almost correct. However, the java compiler will protect you from adding constructs to your code that will never be needed or executed.
In this case, the test method defined for Question05Sub is not declared to throw an IOException. Subsequently, it is not required to be caught when a call is made to Question05Sub's test() method.
Keep in mind that myref is a Question05Sub and not a Question05, therefore the subclasses test method is called and not the superclass method. I hope this helps.
Ultimately, you only have to catch an exception under a few circumstances. A couple of which are if the method declares that it can throw the exception or if the code that you are executing has the potential to actually throw the exception. These are just a couple of the reasons you catch an exception.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rowan
The reason this does not compile is because IOException is a checked exception. This means that any method that can throw it must say that it does, on the same note any method that calls a method that can throw it must either catch it or say that it throws it too.
The compiler is smart enough to know that none of the methods in the chain can throw that exception (because none of them say that they do) so it tells you that the code in the catch is unreacheable.
Try these two experiments:
#1 Change the exception to a 'RuntimeException' - now the code compiles fine because a RuntimeException is unchecked and can be thrown at anytime.
#2 Leave the exception as an IOException but add 'throws IOException' to the test method in the subclass. Now it will also compile becasue that method says it can throw that exception so now the calling method has to catch it or throw it itself.
For a detailed explaination of unreachable code check out the JLS section 14.20 it says:


A catch block C is reachable iff both of the following are true:
Some expression or throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause C. (An expression is considered reachable iff the innermost statement containing it is reachable.)


hope that helps clear it up for you
[ March 15, 2002: Message edited by: Dave Vick ]
 
Rowan Chattaway
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers guys, I get that now, but whether I'd recognise that problem in similar questions is another matter! I know that Val's mock is supposed to be harder than the actual exam, but some of these questions are fiendishly sneaky! hopefully, the actual exam will be significantly easier or I'm in trouble!.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please try and explain this to my simple greenhorn mind?
What greenhorn? You are a ranch hand now
hopefully, the actual exam will be significantly easier or I'm in trouble!.
Don't worry, it will be much easier
 
Rowan Chattaway
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, I'm a ranch hand now!!
Glad to hear the actual exam will be easier, your mock is serious eye-opener to how much I don't know!
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your mock is serious eye-opener to how much I don't know!
And it can be much harder. (The second one will be )
The purpose is not to let you know how much you don't know, but to make you aware of important issues that are usually set aside and taken for granted. It is my opinion that if you want to master a language then you have to go under the hood and try to tweak things in every possible ways. With enough practice, you will actually be able to read Java code as you would read a novel.
Being able to see Java code the way a compiler does is a big advantage when productivity is of paramount importance.
Keep it up !
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin,
Continuing with the question05, why it is not compiling even if i say super.test() in the subclass test() method.
My understanding from the discussion is that the method should throw IOException and i think if i call the super class method which throws the IOException should work fine and it should compile.
Please correct me.
Thanks in advance
kareem
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you have to add a throw clause in the subclass method declaration otherwise the code still doesn't compile.
And once you have done that, the code compiles and prints "In Question05" when run.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Rowan Chattaway:
cheers guys, I get that now, but whether I'd recognise that problem in similar questions is another matter! I know that Val's mock is supposed to be harder than the actual exam, but some of these questions are fiendishly sneaky! hopefully, the actual exam will be significantly easier or I'm in trouble!.


Ditto Rowan... My exam is scheduled for this Monday 9:45 a.m. and im hunkered down this weekend trying to squeeze a little bit more information into my pea brain. I took Val's mock thinking i was doing pretty good til i got the result back that i failed it. arrrrrgh gotta do more hunkering.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still stuck up with this. This explanation : "because no statement in the try block throws the IOException" it wont compilewas good. Now what happens if I do this:
import java.io.*;
public class Question05 {
public static void main(String[] args) {
Question05Sub myref = new Question05Sub();
try{
int i=5;
if (i==4) {
myref.test();
}
else {
System.out.println("Nothing to do");
} // end of else
}
catch(IOException ioe){}
}
void test() throws IOException{
System.out.println("In Question05");
throw new IOException();
}
}
class Question05Sub extends Question05 {
void test() throws IOException{
System.out.println("In Question05Sub");
}
}

Here also, at compiler time only JVM will know that I am only going to execute the else part, and no statement throwing an exception would be executed.
Could someone please give a little detail??
Thanks!
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here also, at compiler time only JVM will know that I am only going to execute the else part, and no statement throwing an exception would be executed.
Nothing is known at compile time since i is not final. The compiler does not know that the else branch will be executed instead of the if branch. Remember that the compiler and the interpreter and two different things. It's the interpreter's job to figure out that the else branch will be executed. If you want to delegate this job to the compiler, you'll have to declare i final.
 
You can't expect to wield supreme executive power just because
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic