-
A
grinds
WHEAT -
B
gre
WHEAT -
C
grind
WHEA -
D
none of these
- Share this MCQ
Explanation:
Let's analyze the given Java code step by step to predict its output:
Code:
String str1 = "great";
String str2 = "minds";
System.out.println(str1.substring(0, 2).concat(str2.substring(1)));
System.out.println("WH" + (str1.substring(2).toUpperCase()));
Explanation:
-
First Line:
System.out.println(str1.substring(0, 2).concat(str2.substring(1)));\
-str1.substring(0, 2)
extracts a substring fromstr1
starting at index 0 up to, but not including, index 2.
Forstr1 = "great"
,str1.substring(0, 2)
results in "gr".
-str2.substring(1)
extracts a substring fromstr2
starting at index 1 to the end of the string.
Forstr2 = "minds"
,str2.substring(1)
results in "inds".
- Concatenating these results:"gr".concat("inds")
results in "grinds".
- Therefore, the firstprintln
statement prints "grinds". -
Second Line:
System.out.println("WH" + (str1.substring(2).toUpperCase()));
-str1.substring(2)
extracts a substring fromstr1
starting at index 2 to the end of the string.
Forstr1 = "great"
,str1.substring(2)
results in "eat".
-str1.substring(2).toUpperCase()
converts this substring to uppercase."eat".toUpperCase()
results in "EAT".
- Concatenating"WH"
with the result:"WH" + "EAT"
results in "WHEAT".
- Therefore, the secondprintln
statement prints "WHEAT".
Conclusion:
The output of the code is:
- grinds
- WHEAT
Related Topic: