Question
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]);
}
}
What is the output of this program?
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]);
}
}
Answer: Option D
Was this answer helpful ?
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:
$ javac output.java
$ java output
ac
Was this answer helpful ?
Submit Solution