• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

john hunts que no13

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q. 13
Which of the following return true?
A."john" == "john"
B."john".equals("john")
C."john" = "john"
D."john".equals(new Button("john"))
Select all correct answers.
answer:A
i answered A, B,D.Can anyone verify me
Thanks
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel it is a,b
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anu,
The best way to test the results is to write some code and try it

And the answer is:

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------------
The anwsers are A,B for the following reason.
1. The string literal "john" is interned and a single instance is maintained so the expression
"john" == "john" both refer to the same object and hence it returns true.
2. I will skip this since it is obvious
3. The third statement won't compile. You can't assign a value to a String literal
4. In the fourth case you are comparing a String object with a Button object and hence will return false.
Actually the reason it returns false is the way the equals method is usually coded. (This is a recommended practice if you are provide your own equals method for a class)
Ex:
... class String {
public boolean equals(Object o) {
if ( o == null ) {
return false;
} else if ( !(o instanceof String)) {
return false;
} else {
// do the actual value comparison
}
}
Take a look at java.lang.String to get a better idea...

 
anu k
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks leena,jane and prakash..it is clear now
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anu k,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic