Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== question

 
Shiao Kung Chux
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • 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.
 
gaurav singhal
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • 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.
 
Vidhya Hari
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
Ene Crus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... And IF D is correct, E will also be correct... well that's not the case tho.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D is surely not correct...

I have checked that....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic