• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
avi sinha
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??
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you compiled and ran the code?
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • 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.
 
Chaitanya Jadhav
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
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic