Give the output of the following program segment. How many times is the loop executed?

for(x=10; x>20; x++)
System.out.println(x);
System.out.println(x*2);

Single Choice
Views 53

Answer:

Step-by-Step Execution:

  1. Initialization: x = 10
  2. Condition Check: x > 20
    • Since 10 > 20 is false, the loop never executes.
  3. Loop Execution:
    • Since the condition fails in the first check, the loop does not run even once.
  4. After the Loop:
    • The statement System.out.println(x*2); is outside the loop, so it executes.
    • Since x was initialized to 10 and was never modified, x*2 = 10 * 2 = 20 is printed.

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of ICSE Computer Applications Class 10 – Previous Year Question Papers & Solutions, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.