Menu
Topics Index
...
`

Captains2213


If you need explanation Read this topic

If you need Answer Take test on this topic

Captains2213
What will be the output of the following program?
public class Captains {
    public static void main(String[] args) {
        String person = "Dhoni";
        String person1 = "Kapildev";
        int personResult = person.compareTo("dhoni");
        int person1Result = person1.compareToIgnoreCase("kaPiLdEv");
        System.out.println(personResult + " " + person1Result);
    }
}

A. 0 0
B. -32 0
C. -32 -32
D. 0 -32
E. Compilation Error or Runtime Error
Topic: Java compareTo() method

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is B.
Because compareTo() method Compares two strings lexicographically, considering case differences. And compareToIgnoreCase() method Compares two strings lexicographically, ignoring case differences.

Posted by Gopinath Manchikanti    2014-06-11 03:50:40


anwer is b. compareTo method compares the ascii values of the string which is passed to that method with the object with which the compareTo method is called. here in this case as 'D' ascii code is 100 and 'd' is 68 it will return -32. and compareToIgnorecase method will do the same job unlike case insensitive.

Posted by Vijay Kumar    2014-06-11 04:36:09


Congratulations Gopinath Manchikanti. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-06-12 03:58:06


© meritcampus 2019

All Rights Reserved.

Open In App