Advertisements
Advertisements
प्रश्न
Given that:
String xa = "active" ; String xb = "passive";
int ct = xa.compareTo(xb);
What will be the value of ct?
विकल्प
15
14
−14
−15
MCQ
Advertisements
उत्तर
−15
Explanation:
- The Method: The compareTo() method compares two strings lexicographically (based on the ASCII/Unicode value of each character) starting from the very first character.
- The Calculation: It finds the first position where the two strings differ. Here, they differ at the very first character: 'a' in "active" and 'p' in "passive". The method subtracts the Unicode value of 'p' (112) from the Unicode value of 'a' (97), which results in 97 - 112 = -15.
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 10: String Handling - Exercises [पृष्ठ २४५]
