Computer Applications
The output of a program which extracts a part of the string "SUBMISSION" is as follows:
(a) "MISS"
(b) "MISSION"
If String str = "SUBMISSION"; write appropriate Java statements to get the above outputs.
Java String Handling
6 Likes
Answer
(a) str.substring(3, 7);
(b) str.substring(3);
Reason
For Output (a): "MISS"
substring(3, 7):
- Extracts the substring starting from index
3(inclusive) to index7(exclusive). - In "SUBMISSION":
- Index 3 →
'M' - Index 4 →
'I' - Index 5 →
'S' - Index 6 →
'S'
- Index 3 →
- Result:
"MISS"
For Output (b): "MISSION"
substring(3):
- Extracts the substring starting from index
3to the end of the string. - In "SUBMISSION":
- Starting at index 3 →
"MISSION" - Result:
"MISSION"
Answered By
1 Like
Related Questions
Write a program in Java to enter any sentence. Also ask the user to enter a word. Print the number of times the word entered is present in the sentence. If the word is not present in the sentence, then print an appropriate message.
The following code to compare two strings is compiled, the following syntax error was displayed – incompatible types – int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate() { String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); System.out.println(x); }