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
प्रश्न
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));लघु उत्तरीय
Advertisements
उत्तर
JAVA
VA
Loop executes for 2 times.
Explanation:
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
