Python for loop quiz

    Last Updated :
    Discuss
    Comments

    Question 1

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

    • To create a list of numbers

    • To define the start and end of the loop

    • To generate a sequence of numbers

    • To specify the step size of the loop

    Question 2

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

    • To create an enumerated list

    • To generate a sequence of numbers

    • To iterate over two lists simultaneously

    • 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?

    • for char in reversed(string):

    • for char in string[::-1]:

    • for char in string.reverse():

    • 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=' ')
     

    • 3 7 2 8 5

    • 3 7

    • 3 7 2

    • 7 2 8

    Question 5

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

    • To execute an alternative block of code

    • To handle exceptions

    • To specify the step size of the loop

    • 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=' ')
     

    •  h e

    • h e l

    •  h e l l

    •  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=' ')
     

    • 1 3 5

    • 2 4

    • 1 2 3 4 5

    • 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=' ')
     

    • 5 4 3 2 1

    •  1 2 3 4 5

    • 5 5 5 5 5

    • 1 3 5

    Question 9

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

    • for i, value in enumerate(list):

    • for index, value in list:

    • for index in list.index():

    • for value in list.values():

    Question 10

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

    • for element in tuple:

    • for in in range(len(tuple)):

    • for i, element in enumerate(tuple):

    • for i in tuple:

    There are 24 questions to complete.

    Take a part in the ongoing discussion