I chose D but the correct answer is F. I guess I don't understand how you can tell from just looking at the code that it will generate a compilation error, or that it will generate a runtime exception.
I guess I could have compiled the code and tried to run it - but I don't think you'll be able to do that on the actual exam.
This is another case where I understood the basic cause of the error related to casting and trying to use an incompatable type in the go2 method. But again I'm not sure how you can tell just from looking at the code why a runtime exception is thrown and not a compilation at line 9.
So is it better for me to be typing these questions into an IDE and trying to compile them? I'm assuming on the actual exam I'm won't have the ability to use javac, so I'm trying to judge the outcome of code in the sample questions just by reading them.
A ClassCastException is a Runtime Exception,which means it won't be caught at compile time. You can cast a Tree object to a Redwood object, the compiler won't recognize it. It'll recognize if you try to cast a JockStrap object to a Redwood object, because a JockStrap can never be cast to a Redwood, but not a Tree, or an Object.
The Dogshow thing is slightly more tricky. Because it is cast up to a Dog reference, the compiler will only register the methods of Dog as possible methods used on that reference.
I understand why there are problems with the code related to casting in both questions. When I was doing the questions I knew that there was a casting problem - my problem is in the choices available for the answers - I'm having trouble judging from just looking at the code when a runtime exception will be thrown and when a compilation error will be thrown.
I know that if I type the code out and try to compile and run I will get the correct answer, but I am trying to figure out what the result is without compiling and running the code - as you won't have javac available to you in the actual exam.