Computer Applications
Give the output of the following program:
public class StringCheck {
boolean foo(String str) {
int x = str.compareTo("INCOMMUTABLE");
System.out.println("x=" + x);
boolean ret = x == 0 ? true : false;
return ret;
}
public static void main(String[] args) {
StringCheck obj = new StringCheck();
boolean a = obj.foo("INCOMPUTABLE");
System.out.println(a);
}
}
Answer
x=3
false
Working
Output of "INCOMPUTABLE".compareTo("INCOMMUTABLE") will be:
ASCII of P - ASCII of M
⇒ 80 - 77
⇒ 3
Value of x becomes 3. As x is not equal to 0 so ternary operator assigns false to ret. foo method returns back this false that gets assigned to a in main method and then printed as the output.
Related Questions
Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)
Write a program in Java to enter any sentence. Also ask the user to enter a word. Print the number of times the word entered is present in the sentence. If the word is not present in the sentence, then print an appropriate message.
The output of the statement "CONCENTRATION".indexOf('T') is:
- 9
- 7
- 6
- (-1)