Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

[Java Programmer Certification Mock Exam (Dan Chisholm) wrong solution..

 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i think the answer should be e . line no 2 marked in the question will never throw exception because we are not trying to use the reference variable "sub". but the answer given in the book is "d".[Java Programmer Certification Mock Exam
(Dan Chisholm)]
is it correct sir??
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you compiled and ran the code?
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Exception in thread "main" java.lang.ClassCastException: Base cannot be cast to Sub
at Orange.main(Orange.java:10)



It is a compile time error as the object of the super type cannot to cast to its subtype. Yes you are correct as far as I think.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sudipto shekhar wrote:


Exception in thread "main" java.lang.ClassCastException: Base cannot be cast to Sub


It is a compile time error as the object of the super type cannot to cast to its subtype. Yes you are correct as far as I think.


What you posted is a runtime error, not a compile time error. But either way, "e" -which the first post suggested- is not correct.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

NOT wrong solution.

the option d is right.(RuntimeException at line number 2)

 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot everyone..i got the point..i was a bit wrong.
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry
    Bookmark Topic Watch Topic
  • New Topic