• Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassCastException - Am I wrong, or is the book?  RSS feed

 
Lisa Couprie
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EDIT: Sweet formatting Batman, I don't know what I was thinking! I typed the code directly from the mock exam and was so focused on getting every line right (it wouldn't let me copy-paste) that I somehow missed the atrocious formatting of said lines. Adding insult to injury, some typos still made it through. Don't be like me kids; never proofread when sleep deprived.

Also, I found the answer: the offending method is named "print" and in my frustrated, overly tired stated, I somehow read it as "System.out.print". Ooooops!

Thank you so much, Campbell Ritchie, for pointing in the right direction! I'm not sure if I would have found it otherwise!

Original post:

Hello everyone,

This is my first question here and I would really appreciate your help, because I've been banging my head against the wall for hours now.

The issue I'm having is with the infamous "when does it give a compiler error and when does it throw a ClassCastException" issue. I spend a lot of time reading older topics on this forum and I really believed I grasped it.

And then this mock exam question happened:



and the following fragments:



Here are the possible answers:
A: All fragments compile successfully
B: Two of them will not compile
C: Three of them will not compile
D: Two of them will throw a ClassCastException at runtime
E: Three of them will throw a ClassCastException at runtime
F: One of them will be executed successfully
G: Two of them will be executed successfully

According to the explanation, answers C, E and G are correct. I feel like I'm about to make a fool out of myself, but I dare to differ.

I understand why F4, F7 and F8 will not compile (missing methods). I also understand, thanks to this forum, that F1, F2 and F3 will throw a ClassCastException, because Car car does not implement Breakable and Car car IS not A SuperFancyCar (in this case, anyway). Okay. But what about F4 and F5? Car IS not A FancyCar either, so shouldn't I get the same exception?

G also puzzles me for the same reason. According to the explanation, F5 and F6 will execute. F6, sure, but F5? Really? Again, a Car IS not A FancyCar! It would compile (I think), but no way I would execute....  would it?

This has been driving be absolutely bonkers. Am I wrong? Is the mock exam wrong? Am I missing something completely obvious?

I got this mock exam from an old CD that was included with the book: "OCA/OCP Java SE 7 Programmer I and II Study Guide" from McGraw Hill Education. I don't know the print as I no longer have the book. The CD copyright is from 2009, if that helps. I also hope the title of this topic is good enough. I really tried to think of a good one, so I'm sorry if it isn't clear enough.
 
Lisa Couprie
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the life of me, I can not figure out how to edit my post.

F4, F7 and F8 will not compile. Sorry for the typo.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You can't edit old posts. I would start by indenting the code correctly. Have you copied it exactly from the question? Thank you for telling us where those questions come from.
Now let's have a look at the parameter (line 11) and see what sort it is. Can you pass the argument in line 10? Does that argument have all the methods used? If you cast it, will the compiler allow the cast? Will the actual argument match the cast?
And, maybe the most useful: have you tried compiling that code or executing it?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!