Computer Applications
Consider the below function:
void strop(String s) {
char a = s.charAt(3);
int b = s.indexOf('M');
String t = s.substring(3,6);
boolean p = s.equals(t);
System.out.println(a + " " + b + " " + t + " " + p);
}
What will be the output for strop("COMPUTER")?
Java
Java String Handling
11 Likes
Answer
P 2 PUT false
Working
s.charAt(3)returns the letter at index 3 ofswhich is P. This gets assigned to variablea.- Index of letter M in
sis 2. s.substring(3,6)returns a substring ofsstarting at index 3 till index 5. So PUT gets assigned tot.- As PUT is not equal to the value of
s(which is COMPUTER) sopbecomes false.
Answered By
5 Likes
Related Questions
The 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); }Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024Write 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.
Write the output of the following String methods:
String x= "Galaxy", y= "Games";(a) System.out.println(x.charAt(0)==y.charAt(0));
(b) System.out.println(x.compareTo(y));