Computer Applications
State the output when the following program segment is executed:
String a ="Smartphone", b="Graphic Art";
String h=a.substring(2, 5);
String k=b.substring(8).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h));
Java
Java String Handling
ICSE 2015
23 Likes
Answer
The output of the above code is:
art
true
Working
a.substring(2, 5) extracts the characters of string a starting from index 2 till index 4. So h contains the string "art". b.substring(8) extracts characters of b from index 8 till the end so it returns "Art". toUpperCase() converts it into uppercase. Thus string "ART" gets assigned to k. Values of h and k are "art" and "ART", respectively. As both h and k differ in case only hence equalsIgnoreCase returns true.
Answered By
11 Likes
Related Questions
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.
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.
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); }
The output of the statement "talent".compareTo("genius") is:
- 11
- ā11
- 0
- 13