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:
- Initialization:
x = 10 - Condition Check:
x > 20- Since
10 > 20is false, the loop never executes.
- Since
- Loop Execution:
- Since the condition fails in the first check, the loop does not run even once.
- After the Loop:
- The statement
System.out.println(x*2);is outside the loop, so it executes. - Since
xwas initialized to10and was never modified,x*2 = 10 * 2 = 20is printed.
- The statement
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.