मराठी

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)); - Computer Applications

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:

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”.

shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2024-2025 (March) Official Board
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×