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);
}
}
Java
Java String Handling
15 Likes
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.
Answered By
7 Likes
Related Questions
Define a class to accept the gmail id and check for its validity.
A gmail id is valid only if it has:
→ @
→ .(dot)
→ gmail
→ com
Example:
icse2024@gmail.comis a valid gmail idThe following code to compare two strings is compiled, the following syntax error was displayed – incompatible types – int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate() { String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); System.out.println(x); }Write a Java program to enter any sentence and convert the sentence to uppercase. Print only those words of the sentence whose first and last letters are the same.