✏️ Explanatory Question
(x) Give the output of the following program segment and specify how many times the loop is executed.
String s = "JAVA"; for(i=0; i
(x) Give the output of the following program segment and specify how many times the loop is executed.
String s = "JAVA"; for(i=0; i
The given Java program prints substrings of the string "JAVA" based on the loop conditions.
String s = "JAVA";
for(i=0; i<s.length(); i+=2)
System.out.println(s.substring(i));
Initialization: i = 0
Loop Condition: i < s.length(), i.e., i < 4 (since "JAVA" has 4 characters).
Increment: i += 2 (i.e., i increases by 2 in each iteration).
Printing: System.out.println(s.substring(i)) prints the substring starting from index i.
First Iteration (i = 0): s.substring(0) → "JAVA"
Second Iteration (i = 2): s.substring(2) → "VA"
Loop Stops (i = 4 is out of bounds)
JAVA
VA
The loop runs 2 times (i = 0 and i = 2).
Output:
JAVA
VA
Number of times loop executes: 2