OutputModerate2 marks
Give the output of the following program segment: String S = "GRACIOUS".substring(4); System.out.println(S); System.out.println("GLAMOROUS".endsWith(S));
Topic: substring() and endsWith()
Previous-year question practice database
Answer
Exam answer
IOUS false
Explanation
Line by line. Index "GRACIOUS": G=0, R=1, A=2, C=3, I=4, O=5, U=6, S=7. substring(4) with a single argument runs from index 4 to the end, so S becomes "IOUS". "GLAMOROUS".endsWith("IOUS") asks whether the word finishes with those four letters. Its last four characters are R, O, U, S — "ROUS", not "IOUS" — so the answer is false. Note that endsWith() returns a boolean, which prints as true or false in lower case, and that the comparison is case-sensitive.
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.2026Name the following:
(a) The return data type of the method equals().
(b) The String method which has no parameter and returns a String.2026Write a program to accept a word and print the Symbolic of the accepted word.
Symbolic word is formed by extracting the characters from the first consonant, then add characters before the first consonant of the accepted word and end with "TR".
Example: AIRWAYS - Symbolic word is RWAYSAITR
BEAUTY - Symbolic word is BEAUTYTR2026