AMP

Chapter-4 Iterative Statements in Python — Online MCQ Test

INFORMATION TECHNOLOGY · Grade 8 · CBSE(NCERT)
Practice Chapter-4 Iterative Statements in Python with a free chapter-wise online MCQ test. This chapter covers: control flow - iteration - for loop - while loop - infinite loop - loop variable - range function - membership operators. AI-generated questions from basic to board-exam level, with instant results and explanations.

Start Exam on Full Site →

Chapter-4 Iterative Statements in Python — Important Questions & Answers

Which keyword is used to create a loop in Python that repeats a block of code?
  • A. repeat
  • B. loop
  • C. for
  • D. iterate
Answer: C. for
The 'for' keyword is used to create a for loop in Python, which is one of the primary iterative statements.
What is the purpose of the range() function in Python?
  • A. To find the maximum value in a list
  • B. To generate a sequence of numbers
  • C. To check if a value exists in a list
  • D. To convert a string to a number
Answer: B. To generate a sequence of numbers
The range() function generates a sequence of numbers, commonly used with for loops to repeat code a specific number of times.
Which of the following is a membership operator in Python?
  • A. ==
  • B. in
  • C. and
  • D. not
Answer: B. in
The 'in' operator is a membership operator used to check if a value exists in a sequence like lists, strings, or tuples.
What does a while loop do?
  • A. Repeats code a fixed number of times
  • B. Repeats code as long as a condition is true
  • C. Executes code only once
  • D. Skips code execution
Answer: B. Repeats code as long as a condition is true
A while loop continues to execute a block of code as long as its condition remains true.
What is a loop variable?
  • A. A variable that stores the total count of loops
  • B. A variable that changes with each iteration of the loop
  • C. A variable that must be outside the loop
  • D. A variable that cannot be modified
Answer: B. A variable that changes with each iteration of the loop
A loop variable is a variable that gets updated with each iteration of the loop, typically used to track the current iteration.
What is an infinite loop?
  • A. A loop that runs exactly 100 times
  • B. A loop that never terminates because its condition is always true
  • C. A loop that has no loop variable
  • D. A loop that skips iterations
Answer: B. A loop that never terminates because its condition is always true
An infinite loop occurs when the loop condition never becomes false, causing the loop to run indefinitely.
What will be the output of this code? for i in range(3): print(i)
  • A. 1 2 3
  • B. 0 1 2
  • C. 0 1 2 3
  • D. 1 2
Answer: B. 0 1 2
The loop prints values from range(3), which generates 0, 1, and 2. Each value is printed on a new line.
Which of the following will create an infinite loop?
  • A. for i in range(10): print(i)
  • B. while True: break
  • C. while x > 0: x = x - 1
  • D. while True: print('hello')
Answer: D. while True: print('hello')
while True: print('hello') creates an infinite loop because the condition 'True' never becomes false, and there's no break statement.
What will be the output? for i in range(1, 4): for j in range(1, 3): print(i, j)
  • A. 1 1, 1 2, 2 1, 2 2, 3 1, 3 2
  • B. 1 1, 2 1, 3 1
  • C. 1 2, 2 2, 3 2
  • D. 1 1, 1 2, 1 3
Answer: A. 1 1, 1 2, 2 1, 2 2, 3 1, 3 2
This is a nested loop. The outer loop runs for i = 1, 2, 3, and for each, the inner loop runs for j = 1, 2, producing all combinations.
Consider this code: for i in range(5): if i == 3: break print(i) What is the output?
  • A. 0 1 2 3 4
  • B. 0 1 2
  • C. 0 1 2 3
  • D. 1 2 3
Answer: B. 0 1 2
The loop prints 0, 1, 2, and when i becomes 3, the break statement exits the loop without printing 3.