Home » JAVA Programming » Strings » Question
  1. What is the output of this program?

    public class Result
    {
    public static void main(String args[])
    {
    String ch[] = {"p", "q", "p", "s", "t"};
    for (int K = 0; K < ch.length; ++K)
    for (int L = K + 1; L < ch.length; ++L)
    if(ch[K].compareTo(ch[L]) == 0)
    System.out.print(ch[L]);
    }
    }
    1. p
    2. q
    3. r
    4. s
    5. t
Correct Option: A

compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.



Your comments will be displayed only after manual approval.