Q.Find the output of the following program segments:
a = 110
while a > 100:
print(a)
a -= 2
for i in range(20,30,2):
print(i)
country = 'INDIA'
for i in country:
i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print (sum)
for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)
var = 7
while var > 0:
print ('Current variable value: ', var)
var = var -1
if var == 3:
break
else:
if var == 6:
var = var -1
continue
print ("Good bye!")
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Tracing each segment step by step: (i) 110→102 in steps of −2,
(ii) 20→28 in steps of 2,
(iii) I N D I A,
(iv) 12,
(v) 2 3 4 4 6 8 6 9,
(vi) prints values 7, 5, "Good bye!", 4 then breaks.
The reliable way to find a program's output is a dry run — track every variable, every test, every print, one step at a time.
(i) while a > 100, a starts at 110, a -= 2 each pass
| Pass | a printed | a after a -= 2 | a > 100? |
|---|---|---|---|
| 1 | 110 | 108 | yes |
| 2 | 108 | 106 | yes |
| 3 | 106 | 104 | yes |
| 4 | 104 | 102 | yes |
| 5 | 102 | 100 | no → stop |
110
108
106
104
102
(ii) for i in range(20, 30, 2) — start 20, stop 30 (excluded), step 2:
20
22
24
26
28
(iii) for i in country ("INDIA") — a string is a sequence of characters, so the loop prints each character:
I
N
D
I
A
(iv) while i < 9 with sum accumulating multiples of 4
| i (start of pass) | i % 4 == 0? | sum | i after i = i + 2 |
|---|---|---|---|
| 0 | yes | 0 | 2 |
| 2 | no | 0 | 4 |
| 4 | yes | 4 | 6 |
| 6 | no | 4 | 8 |
| 8 | yes | 12 | 10 → loop ends |
12
(v) nested loops with break when x * y > 10
| x | y | x*y | Action |
|---|---|---|---|
| 1 | 2 | 2 | print 2 |
| 1 | 3 | 3 | print 3 |
| 1 | 4 | 4 | print 4 |
| 2 | 2 | 4 | print 4 |
| 2 | 3 | 6 | print 6 |
| 2 | 4 | 8 | print 8 |
| 3 | 2 | 6 | print 6 |
| 3 | 3 | 9 | print 9 |
| 3 | 4 | 12 | 12 > 10 → break inner loop |
2
3
4
4
6
8
6
9
(vi) while var > 0 with break/continue interplay
| Pass | var printed | var after −1 | var == 3? | var == 6? | Ends with |
|---|---|---|---|---|---|
| 1 | 7 | 6 | no | yes → var = 5, continue | (no "Good bye!") |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.