Home / Questions / State the values stored in the variables str1 and str2: String s1 = "good"; String s2 = "world matters"; String str1 = s2.substring(5).replace('t', 'n'); String str2 = s1.concat(str1);
Explanatory Question

State the values stored in the variables str1 and str2:

String s1 = "good";
String s2 = "world matters";
String str1 = s2.substring(5).replace('t', 'n');
String str2 = s1.concat(str1);

👁 86 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

Value stored in str1 is " manners" and str2 is "good manners". (Note that str1 begins with a space.)

Explanation

s2.substring(5) gives a substring from index 5 till the end of the string which is " matters". The replace method replaces all 't' in " matters" with 'n' giving " manners". s1.concat(str1) joins together "good" and " manners" so "good manners" is stored in str2.