OutputModerate2 marks
Write the output for the following: String s1 = "phoenix"; String s2 = "island"; System.out.println(s1.substring(0).concat(s2.substring(2))); System.out.println(s2.toUpperCase());
Topic: substring() and concat()
Previous-year question practice database
Answer
Exam answer
phoenixland ISLAND
Explanation
Line 1, taken one call at a time: s1.substring(0) starts at index 0, so it returns the WHOLE string, "phoenix". A substring from 0 is a useful thing to recognise — it changes nothing. s2.substring(2) on "island": i=0, s=1, l=2, so it returns "land". concat() joins them: "phoenix" + "land" = phoenixland. Line 2: toUpperCase() returns a NEW string in capitals, ISLAND. Note it does not alter s2 itself, because Strings are immutable — s2 is still "island" afterwards.
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