ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
System.out.print("apple".compareTo("apricot") + " "
    + "app".compareTo("apple") + " "
    + "b".compareTo("a"));
  1. A. -1 -2 1
  2. B. -1 -1 1
  3. C. -2 -1 1
  4. D. -2 -2 1

Correct answer: D. -2 -2 1

compareTo returns the difference of the first differing characters: 'p' - 'r' = -2. If one string is a prefix of the other, it returns the length difference: 3 - 5 = -2. 'b' - 'a' = 1. It is not limited to -1, 0, 1.

More OOP with C++ and Java questions

Exams that ask this topic