Discussion Forum : Searching And Modifying A String
Question -


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]);
}
}
Options:
A .  ab
B .  bc
C .  ca
D .  ac
Answer: Option D

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 ?
Next Question
Submit Your Solution hear:

Your email address will not be published. Required fields are marked *