• 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

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public class Foo {
2. private int val;
3. public Foo(int v){val = v;}
4.public static void main (String [] args){
5. Foo a = new Foo (10);
6. Foo b = new Foo (10);
7. Foo c = a;
8. int d = 10;
9. double e = 10.0;
10. }
11.}

Which three logical expressions evaluate to true? (Choose Three)
A.(a ==c)
B.(d ==e)
C.(b ==d)
D.(a ==b)
E.(b ==c)
F.(d ==10.0)

Ths answer is ABF.
i don't know why F is true. i think it should be D.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not D because you are declaring two different instances of the Foo class, meaning two different memory references.
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d is correct because before performing operation all the operands will be converted to data type T(which is wider than int) if it is present else it will be converted to int.so here 10 will becomes float ,so both the values are compared and are equal.

for other question you asked, we should not compare the state of the objects,we have to compare the references that reference variables hold for == operator.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you say (B) is right then why not (F) both are same.....
The two reference object can be same only when the pointing to same object in heap memory but in the given question 'a'and 'b' point to different object in heap memory.Thus (D) cannot be true.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
D is incorrect for the simple reason that it refers to two different objects on the heap,although the two objects are meaninfully equivalent,i.e although the two objects hold the same values,they still are two different objects..So references a and b refer to different objects that happen to hold the same value..== compares the references to the objects & not the data inside the object..think this way,if u have the reference b instead of a ,will it take u to the object tht 'a' refers to...the answer is NO...cos b has a reference to some other object...
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bnkiran kumar:
d is correct because before performing operation all the operands will be converted to data type T(which is wider than int) if it is present else it will be converted to int.so here 10 will becomes float ,so both the values are compared and are equal...


I think you mean F is correct (not D).

Also, the widening conversion will be to type double, because a literal floating-point value (like 10.0) is interpreted as a double -- not float.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... And IF D is correct, E will also be correct... well that's not the case tho.
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
D is surely not correct...

I have checked that....
reply
    Bookmark Topic Watch Topic
  • New Topic