English

Given that: String xa = "active" ; String xb = "passive"; int ct = xa.compareTo(xb); What will be the value of ct?

Advertisements
Advertisements

Question

Given that:

String xa = "active" ; String xb = "passive";

int ct = xa.compareTo(xb);

What will be the value of ct?

Options

  • 15

  • 14

  • −14

  • −15

MCQ
Advertisements

Solution

−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
  Is there an error in this question or solution?
Chapter 10: String Handling - Exercises [Page 245]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 10 String Handling
Exercises | Q 1. (ii) | Page 245
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×