Advertisements
Advertisements
Question
Given that:
String gk = "James Clerk Maxwell";
nk = gk.compareTolgnoreCase("k");
What will be the value of nk?
Options
1
2
−1
−2
MCQ
Advertisements
Solution
−1
Explanation:
The compareToIgnoreCase() method compares the very first characters of both strings alphabetically after converting them to lowercase. Since 'j' (from "James") comes exactly one position before 'k' in the alphabet, the method performs the subtraction ('j' − 'k'), which equals −1.
shaalaa.com
Is there an error in this question or solution?
Chapter 10: String Handling - Exercises [Page 245]
