54.9k views
5 votes
String w1;

String w2 = "apple";
String w3 = "banana";


What is stored in w1 by the following?

w1 = w3.substring(1,3);

User Demonyowh
by
8.1k points

1 Answer

1 vote

Final answer:

w1 will store the substring "an" which is extracted from the string w3 "banana" using the substring method with indices 1 and 3.

Step-by-step explanation:

The question is about the Java programming language, particularly about the use of the substring method for String objects. The method is called on the String object w3 with the parameters (1,3), which means it will return a new String consisting of characters from index 1 to index 2 (as the end index is exclusive). Therefore, the String w1 will store the result of w3.substring(1,3), which is "an" because the characters at positions 1 and 2 of the String "banana" are 'a' and 'n' respectively.

User Joan Cardona
by
8.0k points