The string “JAVA” has a length of 4. Since the loop variable increases by 2 each time, the loop runs twice. The substring(i) method extracts the string from index i to the end. As i takes the values 0 and 2, the substrings obtained are “JAVA” and “VA”.
Advertisements
Advertisements
Question
Give the output of the following program segment and specify how many times the loop is executed.
String s = “JAVA”;
for(i=0;i<s.length();i+=2)
System.out.println(s.substring(i));Short Answer
Advertisements
Solution
JAVA
VA
Loop executes for 2 times.
Explanation:
shaalaa.com
Is there an error in this question or solution?
