Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaCaps - mock question

 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • 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
 
Vidya iyer
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Well s.equalsIgnoreCase(s1); would be true.
 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Konda Balabbigari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
 
Lori Battey
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Never mind...I now see that you changed the wording to the question...
My bad!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic