MULTIPLE CHOICE QUESTIONS
- What will be the output of the following code?
for i in range(1, 6, 2): print(i, end=" ")a. 1 2 3 4 5
b. 1 3 5
c. 2 4
d. 1 5 - Which of the following can create an infinite loop if used incorrectly?
a. for with range()
b. while with condition always True
c. for with step size 2
d. range(0) - In nested loops, if the outer loop runs 4 times and the inner loop runs 3 times, the total iterations are:
a. 7
b. 12
c. 81
d. 64 - Which statement is correct about the
elseclause in loops?
a. It executes only when break is used
b. It executes only when loop ends normally
c. It executes only in while loop
d. It never executes - Which of the following programs will print numbers 1, 2, 3 repeatedly forever?
a.for i in range(1,4): print(i)
b.while True: for i in range(1,4): print(i)
c.for i in range(1,4): while True: print(i)
d. Both b and c
FILL IN THE BLANKS
- In Python, loops inside other loops are called ______ loops.
- A loop with multiple conditions joined by
and/oroperators is called a ______ condition loop. - The statement that immediately stops execution of the current loop is ______.
- If the step value is negative, the range function generates numbers in ______ order.
- The output of
range(10, 2, -2)is ______.
DESCRIPTIVE QUESTIONS
- Explain how nested loops can be used to display patterns with an example.
- Differentiate between
break,continue, andpasswith small code snippets. - Why is it dangerous to write a while loop without updating the loop variable? Give an example.
- Describe how logical operators (and/or) can be combined with if statements in loops.
- Write the dry run (step-by-step execution) of the following code:
for i in range(2, 5): for j in range(1, 3): print(i * j)
PROGRAMMING QUESTIONS
- Write a program to print the following pattern using nested loops:
* * * * * * * * * * - Write a program to calculate the sum of all prime numbers between 1 and 50.
- Write a program that asks the user for a number and checks if it is an Armstrong number (e.g., 153 = 1³+5³+3³).
- Write a program that prints all numbers between 1 and 100 which are divisible by both 3 and 5.
- Write a program using a while loop that keeps asking for input until the user enters “exit”.
Answer Key
MULTIPLE CHOICE QUESTIONS
- b. 1 3 5
- b. while with condition always True
- b. 12
- b. It executes only when loop ends normally
- b. while True: for i in range(1,4): print(i)
FILL IN THE BLANKS
- nested
- compound (or multiple)
- break
- reverse / descending
- [10, 8, 6, 4]
DESCRIPTIVE QUESTIONS
- Nested loop for patterns
Example – printing a right triangle pattern:for i in range(1, 5): for j in range(i): print("*", end=" ") print()Output:* * * * * * * * * * - Break, Continue, Pass
break: exits the loop completely.continue: skips current iteration.pass: does nothing, acts as placeholder.
for i in range(1,6): if i == 2: continue # skips 2 if i == 4: break # exits loop if i == 3: pass # does nothing print(i) # Output: 1, 3 - Danger of not updating loop variable
If loop variable never changes, condition remains True → infinite loop.i = 1 while i < 5: # Infinite loop print(i) # i not updated - Using logical operators with if in loops
for i in range(1, 11): if i % 2 == 0 and i % 3 == 0: print(i, "divisible by 2 and 3") - Dry run of given code
for i in range(2, 5): # i = 2, 3, 4 for j in range(1, 3): # j = 1, 2 print(i * j)Steps:- i=2 → j=1 → 2, j=2 → 4
- i=3 → j=1 → 3, j=2 → 6
- i=4 → j=1 → 4, j=2 → 8
Output: 2, 4, 3, 6, 4, 8
PROGRAMMING QUESTIONS
Loop until “exit” entered while True: text = input("Enter text (type 'exit' to stop): ") if text.lower() == "exit": break print("You typed:", text)
Pattern printing for i in range(1, 5): for j in range(i): print("*", end=" ") print()
Sum of prime numbers (1–50) total = 0 for num in range(2, 51): for i in range(2, num): if num % i == 0: break else: total += num print("Sum of primes from 1 to 50:", total)
Armstrong number check num = int(input("Enter a number: ")) temp = num power = len(str(num)) total = 0 while temp > 0: digit = temp % 10 total += digit ** power temp //= 10 if total == num: print("Armstrong number") else: print("Not Armstrong")
Numbers divisible by 3 and 5 (1–100) for i in range(1, 101): if i % 3 == 0 and i % 5 == 0: print(i)
