OutputModerate2 marks
Write the output for the following: String s1 = "Life is Beautiful"; System.out.println("Earth" + s1.substring(4)); System.out.println(s1.endsWith("L"));
Topic: substring() and endsWith()
Previous-year question practice database
Answer
Exam answer
Earth is Beautiful false
Explanation
Line 1: index the string — L=0, i=1, f=2, e=3, space=4. substring(4) runs from index 4 to the end, so it returns " is Beautiful" INCLUDING the leading space. Joined to "Earth" this gives Earth is Beautiful. Line 2: endsWith() is case-sensitive. The string ends with a lowercase l, not a capital L, so the answer is false — printed in lower case, as Java prints all booleans. Both halves of this question test attention rather than knowledge: the invisible space, and the difference between l and L.
More from String Manipulation
To extract the word NOW from the word "ACKNOWLEDGEMENT", the Java statement "ACKNOWLEDGEMENT".substring(3, ____) is used. Choose the correct number to fill in the blank.2026String a[] = {"Atasi", "Aditi", "Anant", "Amit", "Ahana"};
System.out.println(a[1].charAt(1) + "*" + a[2].charAt(2));
The output of the above statement is:2026Which of the following String methods returns a negative value?2026Assertion (A): The substring() method modifies the original String.
Reason (R): The substring() method can extract part of a String starting from a specific index.2026Give the output of the following program segment:
String S = "GRACIOUS".substring(4);
System.out.println(S);
System.out.println("GLAMOROUS".endsWith(S));2026Name the following:
(a) The return data type of the method equals().
(b) The String method which has no parameter and returns a String.2026