Dear all, Today I took an online quiz from a Java platform product company I were confused by one of the questions. What is the output of the following code fragment:
public MyClass { int i; public boolean equals(G g) { return (this.i == g.i); } } G g1 = new G(1); G g2 = new G(1); System.out.println(g1 == g2 + ", " + g1.equals(g2));
A false, false B false, true C true, false D true, true I thought there is no correct answer for the question. Because you don't know what G is and what MyClass is. Am i right? What do you think? Thanx in advance.
[This message has been edited by Liu Zhensheng (edited June 28, 2001).]
Tony Alicea
Desperado
Sheriff
Joined: Jan 30, 2000
Posts: 3219
posted
0
I am moving this thread to the Java beginner forum.
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
prateek narang
Greenhorn
Joined: Apr 04, 2001
Posts: 19
posted
0
yes liu, i too belive that there is no right answer in the given option. because it is needed that class G should be there, which is not. regards prateek
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
posted
0
Liu You can at least say that the first part will be false. == checks to see if the two variables refer to the same memory location. In this case, since you just created two new objects, they dont. I agree with Prateek on the second part that because you have no idea what class G looks like you have no idea what is put into the i variable. I'm wondering if this is a typo, you show the equals method in class MyClass as comparing a MyClass object to a G object. Even if it is class G not MyClass it still doesnt show a constructor to show what gets put into i.
Dave [This message has been edited by Dave Vick (edited June 29, 2001).]
Dave
Ashwin Desai
Ranch Hand
Joined: Jul 17, 2000
Posts: 124
posted
0
Hi, The answer is false, false. g1 == g2 yields false because g1 and g2 are references to different objects G1 and G2 respectively. g1.equals(g2) yields false : The default behavior of the equals(..) method in class Object checks whether the two references point to the same object i.e. same memory location (that is same as g1 == g2). class MyClass is not instantiated at all. Thus, you can safely ignore MyClass. Hope this helps. Ashwin.
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
So how do you know that the G class doesn't override the equals() method to do something completely different? Lousy question.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Liu Zhensheng
Greenhorn
Joined: Nov 28, 2000
Posts: 5
posted
0
Thank all of you, prateek narang, Dave Vick, Ashwin Desai,Cindy Glass. I still think it is an incorrect question. There is no correct answer for it because of what Cindy said. Thanx again.
Bob Graffagnino
Ranch Hand
Joined: May 30, 2001
Posts: 81
posted
0
TTT [This message has been edited by Bob Graffagnino (edited June 29, 2001).]
jerry r
Greenhorn
Joined: Jul 05, 2001
Posts: 1
posted
0
Isn't there yet another problem with the code? The equals "override" shown is actually an overload; since Object.equals() requires an Object parameter, the code shown is not really an override, and although legal, can cause other problems and should not usually be used. As far as I can tell, this will not affect the answer, which is also ambiguous since you cannot tell if G overrides the equals( ) method.