OutputHard2 marks
Write the output for the following: String s = "Today is Test"; System.out.println(s.indexOf('T')); System.out.println(s.substring(0, 7) + " " + "Holiday");
Topic: indexOf() and substring()
Previous-year question practice database
Answer
Exam answer
0 Today i Holiday
Explanation
Index the string first: T=0, o=1, d=2, a=3, y=4, space=5, i=6, s=7, space=8, T=9 ... Line 1: indexOf('T') returns the FIRST occurrence, index 0. The later T at index 9 is ignored — lastIndexOf would find that one. Line 2: substring(0, 7) takes indices 0 to 6 (stopping just before 7), giving "Today i" — note it cuts mid-word and INCLUDES the space at index 5. Joined with " " and "Holiday", the output is Today i Holiday. Counting the space as a character is exactly what this question tests.
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