Author
Strings
Anju sethi
Ranch Hand
Joined: Dec 26, 2005
Posts: 91
posted Feb 08, 2006 03:59:00
0
Question: What is the output of following if the return value is "the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument" (Assuming written inside main) String s5 = "AMIT"; String s6 = "amit"; System.out.println(s5.compareTo(s6)); System.out.println(s6.compareTo(s5)); System.out.println(s6.compareTo(s6)); Ans a) -32 32 0 b) 32 32 0 c) 32 -32 0 d) 0 0 0 As per me answer should be c. But mock exam Question says it is a. Please explain how???
thanks,<br />Anju Sethi
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35220
posted Feb 08, 2006 04:14:00
0
What output did it generate when you ran the code?
Android apps – ImageJ plugins – Java web charts
Anju sethi
Ranch Hand
Joined: Dec 26, 2005
Posts: 91
posted Feb 08, 2006 04:19:00
0
I am unable to sun this code as i am in a cyber cafe right now . and found this problem just now in one of the mock exams.
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
Here the answer is A) because s5.compareTo(s6) It starts comparing from the left side each character.. Here the first character is different and the difference is -32 A is 65 and a is 97 s6.compareTo(s5) Same as above ..but now it is 97-65 =32 s6.compareTo(s6) Here all the characters in both the objects are same and so difference is not there ....So 0. Hope you got it...
Anju sethi
Ranch Hand
Joined: Dec 26, 2005
Posts: 91
posted Feb 08, 2006 05:42:00
0
thanx Sir. I got it.
subject: Strings