• 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

JavaCaps - mock question

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
18. Which of the following statements would return false? if given the following statements.
String s = new String ("New year");
String s1 = new String("new Year");
A) s == s1
B) s.equals(s1);
C) s = s1;
D) None of the above

Answer given : D
According to me answer should be: A,B
Can someone confirm.
May be, I have misunderstood the qwestion.
Thanks.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer D is correct. == method copares the two references to make sure they are pointing at the same object, and equals method makes sure the value is the same. In both cases, they are false here.
Bill
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only correct answer would have been s.Ignorecase(s1).
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well s.equalsIgnoreCase(s1); would be true.
 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill,
Yes, I agree with you....And the question is what statements will return false.
I guess...A) s == s1 will return false as both strings are pointing to different objects.
B) s.equals(s1); will also return false as the values for both the strings are different.
Isn't that right.
Mukti
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's correct. One thing you will see on a lot of mock exams, is that if Strings are created without the new keyword, and they hold same String, then == and equals will both be true. But since the new keyword is used here, == would have been false.
Bill
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
Here is the corrected question with answers. Thanks for correcting the mistake.
http://www.javacaps.com/scjp_mockexams2.html#q18
Konda Balabbigari
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You "corrected" a question that had the correct answer to begin with...it should be changed back to "d) none of the above".
The only way these two strings could have returned "true" is if an answer of
s.equalsIgnoreCase(s1);
had been provided and this isn't one of the options so d) is the only possible answer.
 
Lori Battey
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind...I now see that you changed the wording to the question...
My bad!!
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic