ICSE Class 10 Computer Applications Question 22 of 30

Solved Sample Paper 1 — Question 22

Back to all questions
22
Question

Question 2(ii)

Find the output of the following code.

String str = "I Love My Family";
System.out.println(Integer.toString(str.length()));
System.out.println(str.substring(12));
Answer
Output
16
mily
Explanation
  1. String str = "I Love My Family"; — In this statement, we first use the str.length() method to determine the length of the string str, which is the number of characters in the string. The length of the string "I Love My Family" is 16 characters. Then, we use Integer.toString() to convert this length to a string before printing it. So, the output of this line will be "16" as a string.
  2. System.out.println(str.substring(12)); — In this statement, we use the str.substring(12) method to extract a substring from the original string str. The parameter 12 is the starting index from which the substring should begin. In Java, string indices are zero-based, so the character at index 12 is 'm'. The substring(12) method will extract the portion of the string starting from index 12 till the end of the string. So, the output of this line will be "mily," as it is the substring that starts from the character 'm' and extends to the end of the original string.