• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

One confused Java online quiz before interview

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am moving this thread to the Java beginner forum.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how do you know that the G class doesn't override the equals() method to do something completely different?
Lousy question.
 
Liu Zhensheng
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TTT
[This message has been edited by Bob Graffagnino (edited June 29, 2001).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic