MULTIPLE CHOICE QUESTIONS
- b. It executes code only if the condition is True
- c. for
- a. [2, 4, 6, 8]
- b. break
- c. while loop
FILL IN THE BLANKS
- continue
- Boolean (True/False)
- 0
- backwards / in reverse
- single-pass (or one-time) loop
DESCRIPTIVE QUESTIONS
- Break vs Continue
break→ exits the loop completely.continue→ skips the current iteration and moves to the next.
for i in range(1, 6): if i == 3: continue # skip 3 if i == 5: break # stop at 5 print(i) # Output: 1 2 4 - Entry-controlled vs Exit-controlled
- Entry-controlled: condition checked first, then body executes (e.g., Python’s
while,for). - Exit-controlled: body executes at least once, then condition is checked (e.g., do-while in other languages).
- Python’s
while→ entry-controlled loop.
- Entry-controlled: condition checked first, then body executes (e.g., Python’s
- If loop variable not updated
- The condition may never change, causing an infinite loop.
i = 1 while i < 5: # infinite loop because i never increments print(i) - Difference in range()
range(stop)→ counts from 0 to stop-1.range(start, stop, step)→ more control with start, stop, and step.
Example:
range(5) → [0,1,2,3,4] range(2,10,2) → [2,4,6,8] - Indentation importance
- Python uses indentation to define blocks. Incorrect indentation → error.
if True: print("Hello") # ❌ Error: indentation required
PROGRAMMING QUESTIONS
- Even numbers 1–50
- for i in range(2, 51, 2):
- print(i)
- Sum until 0 entered
- total = 0
- while True:
- num = int(input(“Enter a number (0 to stop): “))
- if num == 0:
- break
- total += num
- print(“Sum is:”, total)
- Check prime number
- num = int(input(“Enter a number: “))
- if num > 1:
- for i in range(2, num):
- if num % i == 0:
- print(“Not Prime”)
- break
- else:
- print(“Prime”)
- else:
- print(“Not Prime”)
- Count vowels in string
- text = input(“Enter a string: “)
- vowels = “aeiouAEIOU”
- count = 0
- for ch in text:
- if ch in vowels:
- count += 1
- print(“Number of vowels:”, count)
- Multiplication table
- num = int(input(“Enter a number: “))
- for i in range(1, 11):
- print(num, “x”, i, “=”, num * i)
