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
3
to the end of the string. - In "SUBMISSION":
- Starting at index 3 →
"MISSION"
- Result:
"MISSION"
Related Questions
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); }
A shopping website offers a special discount if the order ID has the sequence 555 anywhere in it. For example, 158545553031, 198555267140, …. .
Fill in the blanks (a) and (b) in the given Java Method to convert the order ID (a long integer) into a string and check if the sequence 555 is present in it.
void checkOrder(long oid) { String str = _______(a)_________; if(______(b)_______) { System.out.println("Special Discount Eligible: " + oid); } }
Consider the following program segment in which the statements are jumbled, choose the correct order of statements to check if a given word is Palindrome or not.
boolean palin(String w) { boolean isPalin; w = w.toUpperCase(); int l = w.length(); isPalin = false; // Stmt (1) for (int i = 0; i < l / 2; i++) { char c1 = w.charAt(i), c2 = w.charAt(l - 1 - i); // Stmt (2) if (c1 != c2) { break; // Stmt (3) isPalin = true; // Stmt (4) } } return isPalin; }
Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024