• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about question 59 of Mala Gupta's mock exam

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

From Mala Gupta's study guide for OCAJP 8, page 624:

ME-Q59) Given the following code, which option, if used to replace //INSERT CODE
HERE, will enable a reference variable of type Roamable to refer to an object of the
Phone class? (Select 1 option.)




Possible answers:

a Roamable var = new Phone();
b Roamable var = (Roamable)Phone();
c Roamable var = (Roamable)new Phone();
d Because the interface Roamable and the class Phone are unrelated, a reference
variable of the type Roamable can’t refer to an object of the class Phone.



Answer: c

Explanation: Option (a) is incorrect. Without explicit casting, a reference variable of
type Roamable can’t refer to an object of the class Phone.
Option (b) is incorrect because this is an invalid line of code that will fail to compile.
Option (d) is incorrect because a reference variable of the type Roamable can refer
to an object of the class Phone with an explicit cast.
Note that although option (c) will compile, it will throw a ClassCastException if
it’s executed.



I don't see how the answer is c and not d. First of all, the explanation says d is incorrect because it is possible to create a reference variable that refers to an object of the class Phone without an explicit cast, but when I do so, I get the following error:

Tablet.java:4: error: incompatible types: Phone cannot be converted to Roamable
       Roamable var = new Phone();
                    ^
1 error



Also, how is c the correct answer? The question is what line will enable a reference variable of Roamable to refer to an object of Phone, but since this line throws a ClassCastException, how does that satisfy the condition stated in the question?

Thanks for your help!

Regards,
Shane
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Shane,

yeah, these kind of questions are hair-greying, aren't they?

The compiler takes into account that the Phone object might actually be an object of some subclass of Phone that does implement Roamable.

For instance:

This will compile and run fine.
That is why you will not get any compiler error with option c. But as Mala says, if you run the code, you will get a ClassCastException.

I can't help admiring the people who invent questions like this...
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shane Jensen wrote:First of all, the explanation says d is incorrect because it is possible to create a reference variable that refers to an object of the class Phone without an explicit cast, but when I do so, I get the following error


The explanation clearly states that option (d) is incorrect, because a reference variable of the type Roamable can refer to an object of the class Phone with an explicit cast. So without an explicit cast you'll get (as expected) a compiler error.

Shane Jensen wrote:Also, how is c the correct answer? The question is what line will enable a reference variable of Roamable to refer to an object of Phone, but since this line throws a ClassCastException, how does that satisfy the condition stated in the question?


Because options (a), (b) and (d) are obviously wrong The real exam will mention how many correct answers you must select, so you should be capable to pick the best fitting answer(s) when needed.
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, guys. Confusing wording was my main problem here.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic