QPredict the output:-
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()));
Question Info
Choose the Best Option
Click any option to instantly check if you're correct.
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 fromstr1starting 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 fromstr2starting 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 firstprintlnstatement prints "grinds". -
Second Line:
System.out.println("WH" + (str1.substring(2).toUpperCase()));
-str1.substring(2)extracts a substring fromstr1starting 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 secondprintlnstatement prints "WHEAT".
Conclusion:
The output of the code is:
- grinds
- WHEAT
Share This Question
Challenge a friend or share with your study group.