Win a copy of Pipeline as Code this week in the Cloud/Virtualization 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Which code compiles, but throws an exception...confused about the answer

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This code/question is from the SAI sample e-practice exam:



The question is:

Which code, inserted at line 11, will compile, but cause an exception to be thrown at runtime?

A. a2.m2();
B. ((Beta)a2).m2();
C. ((Alpha)a2).m2();
D. ((Gamma)a2).m2();

The answer and explanation are:
Option D is correct. Options A and C will NOT compile, option B will compile and run. Option D throws an exception because type Alpha has no m2 method.

But when I try option D, the code compiles and Runs fine. No exception is being thrown. The above answer and explanation doesn't make sense to me. In fact none of the answer choices seem to be correct. I am confused. What is the right answer and why?

Thanks.
 
Sheriff
Posts: 9679
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is right. It does throw an exception on my system. Can you create a new GreekTest.java file in an empty folder with the code you posted, recompile it and then run it to see the output. Sometimes existing .class files can result in unpredictable behavior...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea... It gives a RuntimeException to me also!~

Short circuit operator(||) check its first boolean expression, if that is false only, it will check the second boolean expression.... Here you can't cast a Beta object to a Gamma object.. so, ClassCastException will be thrown.....
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, option D generates an exception as shown below.

Exception in thread "main" java.lang.ClassCastException: Beta cannot be cast to Gamma
at GreekTest.main(GreekTest.java:11)
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I understand what is going on

Below is the code and corresponding explanations



a2.m2(); : Reference variable is of type Alpha, so the compiler sees that Alpha doesn't have m2 method
((Beta)a2).m2(); : Will run and compile because you are casting an Alpha reference variable as Beta. It is as if the reference variable is Beta, which has method m2()
((Alpha)a2).m2(); : You are casting reference variable Alpha as Alpha so it's just as if you would have written a2.m2();
((Gamma)a2).m2(); : Beta cannot be cast to Gamma. This is a downward cast and there is no guarentee that whatever Gamma can do, Beta can do



Please feel free to clarify these points.
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic