What will be the output of the following Java code? [code] class output { public static void main(String args[])

Vy Bùi

New member
What will be the output of the following Java code?
Code:
    class output 
    {
        public static void main(String args[])
        {
             String chars[] = {"a", "b", "c", "a", "c"};
             for (int i = 0; i < chars.length; ++i)
                 for (int j = i + 1; j < chars.length; ++j)
                     if(chars[i].compareTo(chars[j]) == 0)
                         System.out.print(chars[j]); 
        }
   }
A. ab
B. bc
C. ca
D. ac
 
Answer: d
Explanation: 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.
output:
Code:
$ javac output.java
$ java output  
ac
This section of our 1000+ Java MCQs focuses on string handling in Java Programming Language.
 

Tìm kiếm

Back
Top