Python Loops Quiz - MCQ Questions and Answers

Welcome to the Python Loops Quiz! This quiz is designed for learners who are familiarizing themselves with the concept of loops in Python. Loops are a fundamental aspect of programming in Python, allowing the execution of a block of code multiple times. This quiz will test your knowledge of both 'for' and 'while' loops, their syntax, and common patterns. Let's begin and see how well you understand Python loops!

1. What is the purpose of a loop in Python?

a) To execute a code block once
b) To stop a program
c) To execute a code block multiple times
d) To check the validity of an expression

Answer:

c) To execute a code block multiple times

Explanation:

Loops are used in Python to repeatedly execute a block of code as long as a condition is met.

2. Which keyword is used to exit a loop in Python?

a) exit
b) stop
c) break
d) return

Answer:

c) break

Explanation:

The 'break' keyword is used to exit a loop prematurely.

3. What type of loop will execute at least once regardless of the condition?

a) For loop
b) While loop
c) Do-while loop
d) Repeat-until loop

Answer:

c) Do-while loop

Explanation:

The do-while loop will execute its block at least once before checking its condition. However, it's important to note that Python does not have a built-in do-while loop, but this concept exists in other programming languages.

4. How do you iterate over a list named 'myList' using a for loop in Python?

a) for item in myList: ...
b) for item = 0 to len(myList): ...
c) for each item in myList: ...
d) for item <= len(myList): ...

Answer:

a) for item in myList: ...

Explanation:

The syntax for iterating over a list in Python is: for item in myList.

5. What does the 'continue' keyword do in a loop?

a) Pauses the loop
b) Stops the loop
c) Skips the rest of the code inside the loop for the current iteration
d) Exits the program

Answer:

c) Skips the rest of the code inside the loop for the current iteration

Explanation:

The 'continue' keyword is used to skip the current iteration of the loop and continue with the next one.

6. How do you create an infinite loop in Python?

a) while True: ...
b) for i in range(infinity): ...
c) while 1: ...
d) Both a) and c)

Answer:

d) Both a) and c)

Explanation:

Infinite loops can be created using 'while True:' or 'while 1:' as these conditions are always true.

7. What is the output of the following code?

   for i in range(3):
       print(i)
a) 0 1 2
b) 1 2 3
c) 0 1 2 3
d) Error

Answer:

a) 0 1 2

Explanation:

The range(3) function generates numbers from 0 up to, but not including, 3.

8. Which loop is typically used when the number of iterations is known?

a) For loop
b) While loop
c) Do-while loop
d) Infinite loop

Answer:

a) For loop

Explanation:

A for loop is used when the number of iterations is known beforehand.

9. How do you loop through both the index and elements of a list named 'myList'?

a) for index, item in enumerate(myList): ...
b) for index in range(len(myList)): ...
c) for index, item in myList: ...
d) for item in myList: ...

Answer:

a) for index, item in enumerate(myList): ...

Explanation:

The enumerate() function is used to get both the index and the value of each element in the list.

10. What is the correct way to write a while loop in Python?

a) while (condition) { ... }
b) while condition: ...
c) while (condition): ...
d) while: (condition) ...

Answer:

b) while condition: ...

Explanation:

The correct syntax for a while loop in Python is: while condition: ...

11. What will be the output of the following code?

   count = 0
   while count < 5:
       print(count)
       count += 1
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3 4 5
d) Infinite loop

Answer:

a) 0 1 2 3 4

Explanation:

The loop prints numbers from 0 to 4. It stops when count becomes 5.

12. What is the purpose of the 'range' function in loops?

a) To repeat a string several times
b) To create a list of numbers
c) To set the range of a list's indices
d) To generate a sequence of numbers

Answer:

d) To generate a sequence of numbers

Explanation:

The range function is used to generate a sequence of numbers, often used for looping a specific number of times.

13. How do you write a for loop that repeats an action 5 times in Python?

a) for i in range(5): ...
b) for i in 5: ...
c) for i to 5: ...
d) for i = 0; i < 5; i++: ...

Answer:

a) for i in range(5): ...

Explanation:

The range(5) function generates numbers from 0 to 4, which is used to repeat the loop 5 times.

14. What will the following code output?

   i = 5
   while i > 0:
       i -= 1
       if i == 2:
           break
       print(i)
a) 4 3 2
b) 4 3
c) 3 2 1
d) 4 3 1

Answer:

b) 4 3

Explanation:

The loop breaks when i becomes 2, so it only prints 4 and 3.

15. How do you loop through a dictionary named 'myDict' to print its keys?

a) for key in myDict.keys(): ...
b) for key in myDict: ...
c) for key, value in myDict: ...
d) for key in myDict.values(): ...

Answer:

b) for key in myDict: ...

Explanation:

When looping through a dictionary, it defaults to iterating over its keys.

16. What will be the output of the following code?

   for x in 'Python':
       if x == 'h':
           continue
       print(x)
a) Pyton
b) Pythn
c) Python
d) Pyton

Answer:

d) Pyton

Explanation:

The loop skips the iteration when x is 'h', so 'h' is not printed.

17. How do you ensure a loop runs at least once in Python?

a) By using a do-while loop
b) By setting the initial condition to True
c) By writing the loop inside a function
d) This is not possible in Python

Answer:

d) This is not possible in Python

Explanation:

Python doesn't have a do-while loop construct that guarantees the execution of the loop body at least once.

18. What is an infinite loop?

a) A loop that runs a finite number of times
b) A loop that never ends
c) A loop that runs twice
d) A loop without a body

Answer:

b) A loop that never ends

Explanation:

An infinite loop is a loop that has no end condition, or the condition never becomes false, so it runs indefinitely.

19. How can you iterate over a range of numbers in reverse order in a for loop?

a) for i in reversed(range(5)): ...
b) for i in range(5, 0): ...
c) for i in range(5, 0, -1): ...
d) for i in range(-5, 0): ...

Answer:

c) for i in range(5, 0, -1): ...

Explanation:

The range function can take a third argument for step size, and a negative step size iterates in reverse.

20. What is the best loop to use when you need to perform an action a specific number of times?

a) While loop
b) For loop
c) Do-while loop
d) No loop

Answer:

b) For loop

Explanation:

A for loop is most suitable when the number of iterations is known beforehand, as it can iterate over a sequence of numbers generated by the range function.

Comments