Computer Applications
Write the output for the following:
String s1 = "Life is Beautiful";
System.out.println("Earth" + s1.substring(4));
System.out.println(s1.endsWith("L"));
Answer
Earth is Beautiful
false
Working
s1.substring(4) will return a substring starting at index 4 of s1 till its end so its output will be " is Beautiful". System.out.println("Earth" + s1.substring(4)) will concatenate "Earth" and " is Beautiful" to given the output as "Earth is Beautiful".
As the last character of s1 is a small l not a capital L so s1.endsWith("L") returns false.
Related Questions
Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)
The output of the statement "talent".compareTo("genius") is:
- 11
- –11
- 0
- 13
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.