AmbifulWorksheets
Practice that never repeats
CBSE · Class 9 · Computational Thinking and AI

Conditions and Loops

Standard difficulty · 20 questions · 40 marks · Time: 42 minutes

Name: Date: Score: ____ / 40

Show your working for Sections B and C. Answers are on the last page — try every question first!

Section A — Choose the correct answer8 × 1 = 8 marks
1.
list(range(1, 5)) gives:
  1. [0, 1, 2, 3, 4]
  2. [2, 3, 4, 5]
  3. [1, 2, 3, 4, 5]
  4. [1, 2, 3, 4]
2.
What does this print?
marks = 86
if marks >= 85:
    print('A')
elif marks >= 75:
    print('B')
elif marks >= 60:
    print('C')
else:
    print('D')
  1. D
  2. C
  3. B
  4. A
3.
What does this print?
s = 0
for i in range(8, 13, 4):
    s = s + i
print(s)
  1. 21
  2. 33
  3. 20
  4. 8
4.
What does this print?
n = 148
count = 0
while n > 3:
    n = n // 2
    count = count + 1
print(count)
  1. 7
  2. 6
  3. 5
  4. 74
5.
What does this print?
print(list(range(5, 15, 4)))
  1. [5, 9, 13, 17]
  2. [9, 13]
  3. [5, 9, 13]
  4. [9, 13, 17]
6.
What does this print?
x = 23
if x > 19:
    if x % 2 == 0:
        print('big even')
    else:
        print('big odd')
else:
    print('small')
  1. big even
  2. small
  3. big odd
  4. nothing
7.
How many numbers does this print?
for i in range(1, 43):
    if i % 9 == 0:
        print(i)
  1. 4
  2. 5
  3. 42
  4. 3
8.
This program should print 1 + 2 + … + 18 = 171, but prints 172. Which change fixes it?
n = 18
s = 1
for i in range(1, n + 1):
    s = s + i
print(s)
  1. change s = s + i to s = s * i
  2. change n to n + 1 in the first line
  3. change s = 1 to s = 0
  4. change print(s) to print(i)
Section B — Short answer8 × 2 = 16 marks
9.
What is the difference between a for loop and a while loop?
10.
What does this print?
s = 0
for i in range(6, 12):
    s = s + i * i
print(s)
11.
What does this print?
x = 9
while x < 1861:
    x = x * 9
print(x)
12.
What does this print?
age = 23
if age >= 16:
    print('Can apply')
else:
    print('Not yet')
13.
How many times is Hello printed?
for i in range(2):
    for j in range(7):
        print('Hello')
14.
Write the values that i takes, separated by commas.
for i in range(7, 37, 5):
    print(i)
15.
What numbers does this print? Write them separated by commas.
for i in range(1, 25):
    if i % 4 == 3:
        print(i)
16.
What does this print?
nums = [20, 36, 52, 42, 33, 45]
total = 0
for x in nums:
    if x > 25:
        total = total + x
print(total)
Section C — Word problems4 × 4 = 16 marks
17.
Write a program to print the grade of a student: A for 90 and above, B for 75 to 89, C for 60 to 74 and D below 60.
18.
Trace this program.
n = 52784
s = 0
count = 0
while n > 0:
    s = s + n % 10
    n = n // 10
    count = count + 1
print(s, count)
(a) What is s at the end? (b) What is count? (c) What is s after the first pass?
19.
This program should print the first 7 even numbers starting from 2.
for i in range(2, 14, 2):
    print(i)
(a) Write the numbers it actually prints. (b) How many numbers is that? (c) What is the smallest stop value for range that prints all 7 numbers?
20.
What does this program print?
n = 12
for i in range(1, 10):
    print(n * i)
(a) What is the first number printed? (b) the last? (c) the sum of all the numbers printed?
AmbifulWorksheets
Practice that never repeats
CBSE · Class 9 · Computational Thinking and AI

Answer key

Conditions and Loops · AW-9CT-PYF-2026-V1 · For parents — fold back or tear off before handing the sheet to your child.

QAnswerWorking
1(d) [1, 2, 3, 4]—
2(d) Athe first true condition wins: 86
3(c) 20i = 8, 12 (13 is not included)
4(b) 6n ends at 2
5(c) [5, 9, 13]start at 5, add 4, stop before 15
6(c) big odd23 > 19 is true
7(a) 4multiples of 9 up to 42
8(c) change s = 1 to s = 0s must start at 0
9A for loop runs over a fixed sequence (like a range); a while loop repeats as long as a condition is True.—
10451i = 6, 7, 8, 9, 10, 11
1165619 → 81 → 729 → 6561
12Can apply23 >= 16 is true
13142 × 7
147, 12, 17, 22, 27, 32start 7, step 5, stop before 37
153, 7, 11, 15, 19, 23numbers up to 24 leaving remainder 3 on division by 4
16208numbers above 25: 36, 52, 42, 33, 45
17if m >= 90: print("A") elif m >= 75: print("B") elif m >= 60: print("C") else: print("D").—
18(a) 26; (b) 5; (c) 4last digits: 4, 8, 7, 2, 5
19(a) 2, 4, 6, 8, 10, 12; (b) 6; (c) 15range stops before its stop value, so it must be more than 14
20(a) 12; (b) 108; (c) 54012 × (1 + 2 + … + 9)