Computer Applications
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));
Java String Handling
6 Likes
Answer
Output
16
mily
Explanation
String str = "I Love My Family";— In this statement, we first use thestr.length()method to determine the length of the stringstr, which is the number of characters in the string. The length of the string "I Love My Family" is 16 characters. Then, we useInteger.toString()to convert this length to a string before printing it. So, the output of this line will be "16" as a string.System.out.println(str.substring(12));— In this statement, we use thestr.substring(12)method to extract a substring from the original stringstr. The parameter12is the starting index from which the substring should begin. In Java, string indices are zero-based, so the character at index 12 is 'm'. Thesubstring(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.
Answered By
3 Likes
Related Questions
String is …………… in Java.
- interface
- mutable
- method
- immutable
What will be the output of the following statements?
int a = 3; System.out.println(" " + (1 + a)); System.out.println(" " + 1 + a);Write a Java statement for the following mathematical expression :
V = πr2h
Write the value of n and m after execution of the following code.
int m; int n; m = 5; n = (5 * ++m) % 3; System.out.println("n = " + n + "m =" + m);