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.
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"
Related Questions
The output of the statement "talent".compareTo("genius") is:
- 11
- –11
- 0
- 13
Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)
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.