File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes One confused Java online quiz before interview Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "One confused Java online quiz before interview" Watch "One confused Java online quiz before interview" New topic

One confused Java online quiz before interview

Liu Zhensheng

Joined: Nov 28, 2000
Posts: 5
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

Joined: Jan 30, 2000
Posts: 3226
I am moving this thread to the Java beginner forum.

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
prateek narang

Joined: Apr 04, 2001
Posts: 19
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.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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.

[This message has been edited by Dave Vick (edited June 29, 2001).]

Ashwin Desai
Ranch Hand

Joined: Jul 17, 2000
Posts: 124
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.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Nov 28, 2000
Posts: 5
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
[This message has been edited by Bob Graffagnino (edited June 29, 2001).]
jerry r

Joined: Jul 05, 2001
Posts: 1
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.
I agree. Here's the link:
subject: One confused Java online quiz before interview
It's not a secret anymore!