Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

"=" oprator question

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Given:
11. class ClassA {}
12. class ClassB extends ClassA {}
13. class ClassC extends ClassA {}
and:
21. ClassA p0 = new ClassA();
22. ClassB p1 = new ClassB();
23. ClassC p2 = new ClassC();
24. ClassA p3 = new ClassB();
25. ClassA p4 = new ClassC();
Which three are valid? (Choose three.)
A. p0 = p1;
B. p1 =p2;
C. p2 = p4;
D. p2 = (ClassC)p1;
E. p1 = (ClassB)p3;
F. p2 = (ClassC)p4;
Answer: AEF

this is a mock exam question. I am not convinced with the answer.
"=" oprator for object references connotes to equivalence of object the reference variable pointing to... if both are pointing to same object then they can be said equal. as per this understanding how p0=p1 is valid answer?
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shaily,

p1 is an instanceof ClassA and hence can be easily assigned to p0.

p3 and p4 are nothing but references of ClassA and can be assigned using respective casts.

and remember there is a difference between incompatable types and inconvertable types.

Thanks.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question isn't asking which references are equal. The question would use == (double equals) if it were asking about equality. In any case, none of those would be true because they all refer to different object instances.

What the question is testing is if you understand a legal assignment. If you have a Cat variable, you can't assign a Dog object to it. If you had an Animal reference, you could assign either a Cat or a Dog to it, assuming Cat and Dog both extend Animal (or implement Animal if Animal is an interface). In some cases, you know the type of object is a Cat even though you're currently using an Animal reference to point to it - so you could legally cast the variable to a Cat and then point to it using an actual Cat variable. I'm sure your book explains how all this works in more detail.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shaily, please quote your sources. Note that just saying "this is a mock exam question" is not enough. Say from which mock exam you got this question.
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic