aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== question" Watch "== question" New topic
Author

== question

Shiao Kung Chux
Greenhorn

Joined: Jan 26, 2006
Posts: 25
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.


while everyone is busy , i am free
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

This is not D because you are declaring two different instances of the Foo class, meaning two different memory references.


[My Blog]
All roads lead to JavaRanch
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
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.


Kiran Kumar.
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
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

Joined: Mar 20, 2006
Posts: 20
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ene Crus
Greenhorn

Joined: Mar 19, 2006
Posts: 8
... And IF D is correct, E will also be correct... well that's not the case tho.
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
D is surely not correct...

I have checked that....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == question