43.2k views
1 vote
String sentence;

String str1, str2, str3, str4;

int length1;

sentence = " First exam is on Monday. " ;

str1 = sentence.substring(6, 12);

str2 = str1.substring(0, 4);

str3 = sentence.replace( ' i ' , ' # ' );

str4 = sentence.indexOf( "on" );

length1 = sentence.length();

1. Based on the code above, what is the value of str2?

User The
by
6.0k points

1 Answer

2 votes

Answer:

" exa"

Step-by-step explanation:

str1 will create a substring of sentence from index 6 - 12

str1 = " exam "

Index 6 is the spacing between 'first' and 'exam' while index 12 is the letter 'i' of 'is'

str2 will create a substring of str1 from index 0 - 4

str2 = " exa"

Index 0 is the spacing before the 'exam' and index 4 is the letter 'm' of the ' exam'

The last position in the index is not considered when creating a substring. The substring stop a letter before the last index given as argument

User Azzamsa
by
5.5k points