Python for loop quiz

Last Updated :
Discuss
Comments

Question 1

What is the purpose of the range function in a for loop?

  • A

    To create a list of numbers

  • B

    To define the start and end of the loop

  • C

    To generate a sequence of numbers

  • D

    To specify the step size of the loop

Question 2

What is the purpose of the enumerate function in a for loop?

  • A

    To create an enumerated list

  • B

    To generate a sequence of numbers

  • C

    To iterate over two lists simultaneously

  • D

    To access both the index and the value of elements in an iterable

Question 3

How can you iterate over a string in reverse order using a for loop?

  • A

    for char in reversed(string):

  • B

    for char in string[::1]:

  • C

    for char in string.reverse():

  • D

    Reverse iteration is not possible on strings

Question 4

What will the following code output?

numbers = [3, 7, 2, 8, 5]
for i, num in enumerate(numbers):
   if i == num:
       break
   print(num, end=' ')
 

  • A

    3 7 2 8 5

  • B

    3 7

  • C

    3 7 2

  • D

    7 2 8

Question 5

What is the purpose of the else clause in a for loop?

  • A

    To execute an alternative block of code

  • B

    To handle exceptions

  • C

    To specify the step size of the loop

  • D

    To execute code when the loop is terminated normally

Question 6

What will the following code output?

for char in 'hello':
   if char == 'l':
       break
   print(char, end=' ')
 

  • A

     h e

  • B

    h e l

  • C

     h e l l

  • D

     h e l l o

Question 7

What will the following code output?

numbers = [1, 2, 3, 4, 5]
for i, num in enumerate(numbers):
   if i % 2 == 0:
       continue
   print(num, end=' ')
 

  • A

    1 3 5

  • B

    2 4

  • C

    1 2 3 4 5

  • D

    0 1 2 3 4

Question 8

What will the following code output?

numbers = [1, 2, 3, 4, 5]
for i in range(len(numbers) - 1, -1, -1):
   print(numbers[i], end=' ')
 

  • A

    5 4 3 2 1

  • B

     1 2 3 4 5

  • C

    5 5 5 5 5

  • D

    1 3 5

Question 9

How can you iterate over the indices and values of a list using a for loop?

  • A

    for i, value in enumerate(list):

  • B

    for index, value in list:

  • C

    for index in list.index():

  • D

    for value in list.values():

Question 10

How can you iterate over a tuple using a for loop?

  • A

    for element in tuple:

  • B

    for in in range(len(tuple)):

  • C

    for i, element in enumerate(tuple):

  • D

    for i in tuple:

There are 24 questions to complete.

Take a part in the ongoing discussion