Chapter 5: Nested for a while in a loop
Multiple Choice Questions (MCQs)
1. Observe the below program:
for I in range(5, 31):
if (152 - 0):
print(1)
Which number will be displayed?
• The condition if (152 - 0): is always True, so 1 is printed 30 - 5 = 26 times.
• Answer: (D) 31 (since range(5,31) includes 5 to 30, the loop runs 26 times)
2. Observe the below program:
num = 3
while (num < 9):
print("Hello")
num = num + 2
How many times will it display "Hello"?
• The loop increments by 2 each time: 3, 5, 7.
• Stops at 9, so it prints 3 times.
• Answer: (B) 3
3. Observe the below program:
for I in range(1,6):
for j in range(i):
print("Hi!", end="")
print("\n")
How many times will it display "Hi!"?
• The pattern will be:
• Hi!
• Hi!Hi!
• Hi!Hi!Hi!
• Hi!Hi!Hi!Hi!
• Hi!Hi!Hi!Hi!Hi!
• The total occurrences = 1 + 2 + 3 + 4 + 5 = 15.
• Answer: (C) 15
4. The following program was run:
z = 10
while (z >= 0):
print("Yahoo!!!")
z -= 2
else:
print("Eureka!!!")
What was displayed in the output?
• Yahoo!!! is printed for z = 10, 8, 6, 4, 2, 0 (6 times).
• Then Eureka!!! is printed once.
• Answer: (A) 5 "Yahoo!!!" and 1 "Eureka!!!"
5. Which of the following is a nested loop?
• Answer: (A) A loop inside another loop
Fill in the Blanks
1. The if and else produce two different outputs. Conditional statements can have
only one execution path.
2. Two or more conditions can be combined or modified using logical (AND, OR, NOT)
operators.
3. If a program is used to check 5 conditions and has 6 different outputs, then the if-
elif-else conditional statement is used.
4. The infinite loop never stops producing output.
5. In a for loop, 0 is the default start value. 1 is the default step value.
Descriptive Questions
1. Create programs that draw concentric circles using:
(A) For loop
import turtle
t = turtle.Turtle()
for i in range(5):
t.circle(20 * (i+1)) # Increasing radius
t.penup()
t.goto(0, -20 * (i+1))
t.pendown()
(B) While loop
import turtle
t = turtle.Turtle()
radius = 20
count = 0
while count < 5:
t.circle(radius)
radius += 20
t.penup()
t.goto(0, -radius)
t.pendown()
count += 1
2. Create a program to draw infinite squares at random positions using turtle
import turtle
import random
t = turtle.Turtle()
t.speed(0)
while True:
x = random.randint(-200, 200)
y = random.randint(-200, 200)
t.penup()
t.goto(x, y)
t.pendown()
for _ in range(4):
t.forward(50)
t.right(90)
3. Write syntax for conditional statements:
(A) If statement
if condition:
statement
(B) If-else statement
if condition:
statement
else:
statement
(C) If-elif-else statement
if condition1:
statement1
elif condition2:
statement2
else:
statement3
4. Write syntax for loops:
(A) For loop
for variable in range(start, stop, step):
statement
(B) While loop
while condition:
statement
(C) Nested loop
for i in range(n):
for j in range(m):
statement
5. Write a program to check if a student is eligible for a scholarship test
age = int(input("Enter age: "))
percentage = float(input("Enter percentage: "))
science_marks = float(input("Enter science marks: "))
attendance = float(input("Enter attendance percentage: "))
if age >= 15 and percentage >= 75 and science_marks >= 80 and attendance >=
60:
print("You are eligible for the scholarship test.")
else:
print("You are not eligible for the scholarship test.")