• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock 3 exam wrong answer (Java OCA 8 Programmer I Study Guide)

 
Vincent Botteman
Greenhorn
Posts: 9
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the questions is about what exception can fill in the blank line to make the code compile.



In the answer option D (LimpException) is said to be incorrect because the method split throws HurtException and no other exception. Therefore catching LimpException would be unreachable code because HurtException is already caugth.
But I can compile following code, so answer D is correct. I do receive a warning in my IDE about unreachable code but the source compiles and runs.

 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vince Botteman wrote:In the answer option D (LimpException) is said to be incorrect because the method split throws HurtException and no other exception. Therefore catching LimpException would be unreachable code because HurtException is already caugth.
But I can compile following code, so answer D is correct. I do receive a warning in my IDE about unreachable code but the source compiles and runs.

First of all, I completely agree with you about answer option D: it should be a correct answer because if LimpException fills in the blank, the code compiles successfully (although with a warning but that's not a compiler error) and you can run the program as well.

But you have to be very careful: there's a huge difference between "unreachable code" and "dead code"! The first one results in a compiler error, the latter one only in a warning. So on the exam, you'll have to know the difference between both, because with dead code the compiler will successully compile the code (and thus you can run the code); but when it detects unreachable, it gives a compiler error and you won't have a .class file (and thus you can't run the code).
To illustrate consider these 2 code snippetsThe distinction between "unreachable code" and "dead code" is sometimes very hard. So you'll find plenty of threads on this forum about this topic (most of them with lots of examples). E.g. here, here and here.

So the explanation in the book mentions that Java recognizes the LimpException as dead code (which is correct), but dead code is only a compiler warning not a compiler error. So answer D is correct! And finally (pun intended ), I must say I'm a little bit surprised with the compiler warning you'll get when you compile the Mock3ExamHurtException class:
Mock3ExamHurtException.java:10: warning: unreachable catch clause
catch (LimpException e) {System.out.println("Limp");}
^
thrown type HurtException has already been caught
1 warning

It mentions "unreachable" so that could be very confusing and misleading, because unreachable code is a compiler error (and here you'll get a compiler warning).

Hope it helps!
Kind regards,
Roel
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right! Added to the errata. I was so sure that LimpException wouldn't work that I didn't actually try it. Our tech editors must have been as well. Trying it now, I totally agree.

I gave you both a cow for this one. Because this wasn't a typo or slip of the mind. I really didn't think it would work.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A similar example with 2 classes from the Java API: FileNotFoundException extends IOException. Given this method declarationthis code snippet compiles with a warning

And what do you think will happen when you compile the previous code snippet after you have changed the method declaration to Will it still successfully compile? With or without warnings?
 
Vincent Botteman
Greenhorn
Posts: 9
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the code will not compile because FileNotFoundException is a checked exception and FileNotFoundException is not a superclass of the class that is declared to be thrown by the method (i.c. IOException). So the compiler will detect that FileNotFoundException or a subclass will never be thrown in the try block.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vince Botteman wrote:I think the code will not compile

Wrong! The code compiles successfully, even without any warning. So it's perfectly valid code.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel: That's evil!

Vince: The reason is that doRiskyThings() declares IOException. The actual implementation is free to throw IOException, any subclass of IOException, any runtime exception or no exception at all. Since it is possible for method to throw a FileNotFoundException, the code is legal.
 
Vincent Botteman
Greenhorn
Posts: 9
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for the clarification. It takes my knowledge of exceptions one step further.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Roel: That's evil!

That's my middle name!

Vince Botteman wrote:It takes my knowledge of exceptions one step further.

Let's see if we can increase this knowledge about exception a little further.

Given this class hierarchywhich of these methods will not compile and/or not run successfully? And why not?

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Given this class hierarchy
...
which of these methods will not compile and/or not run successfully? And why not?
...

It is a perfect example, Roel, thanks a lot. I learn new things which I don't know so far but I can't keep all of them in memory from the first time It is interesting that why don't you write study guide about certification?
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:It is interesting that why don't you write study guide about certification?

Writing a study guide from scratch is a very daunting task and that requires a massive amount of time and dedication
 
Vincent Botteman
Greenhorn
Posts: 9
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

The answers of the quiz:


It compiles successfully because the checked exception FoodException is caught. No problems at runtime.

At compile time the reference a is an Animal. And the exception FoodException is not caught or declared to be thrown. So compile error.

Similar to case 1: no compile or runtime errors

The reference is cast to Animal and the method Animal.eat throws FoodException which is not caught or declared to be thrown, so compile error.

The reference is cast to Tiger and the method Tiger.eat throws VeggieException which is a subclass of Exception thrown by method5, so the source compiles. But at runtime it will throw a ClassCastException because the object the reference a is pointing to is not a Tiger object.

The reference a is cast to Elephant and the method Elephant.eat throws no exception. It will compile and run successfully. A method is free to declare to throw an exception without actually throwing it.

This time it fails to compile because the catch block is unreachable.

 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vince Botteman wrote:The answers of the quiz:

A perfect score! Have a cow for having all the correct answers and providing each time a spot-on explanation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Similar Threads