Python Interview Questions

Python Interview Questions on Loops

Top 10 Python Interview Questions on Loops for 2023

1. what is the difference between the continue and break statements in Python?

The break statement terminates the loop that contains it. The program immediately moves to the code section that is in the outer scope of the loop.
The continue statement skips the rest of the code of the current iteration and move to the next iteration.
Question - 1
Which of the following type of loop(s) is/are not supported by Python?
for
while
Both for and while
do while
Question - 2
What would be the result of executing the following code?
n = sum(range(2,4))
while n:
    print(n, end = ' ')
    n = n - 1
    break
else:
    print('End', end = ' ')
5 4 3 2 1 End
5 4 3 2 1
5
TypeError
Question - 3
Which statement is used inside a loop to skip the rest of the code for the current iteration, so execution can continue with the next iteration?
abort
break
next
continue
Question - 4
Which loop in Python is a counter-controlled loop?
for
while
do while
repeat
Question - 5
Which Python statement is used to bring control out of the current loop?
abort
break
exit
quit
Question - 6
What would be the result of executing the following code?
sum = 0
for i in range(5, 0, -2):
    sum += i > sum
print(sum)
0
1
2
3
Question - 7
Which of the following code will result in an infinite loop?


while(-1):
    print ('OK')


while(None):
    print ('OK')


while(0):
    print ('OK')


while(range(-1)):
    print ('OK')
Python Interview Questions
  • Python - Loops
    1. What keyword is used for looping?
    A) while
    B) for
    C) loop

    Answer: for
    2. What function can generate a list of numbers?
    A) for
    B) list
    C) range

    Answer: range
    3. Name the 2 keywords used for looping?
    A) while loop
    B) for loop
    C) for while

    Answer: for while
    4. What module is used for generating random values?
    A) math
    B) random
    C) randrange

    Answer: random
    5. What keyword is used to skip back to the beginning of a loop?
    A) break
    B) while
    C) continue

    Answer: continue
    6. What keyword is used to end looping completely?
    A) end
    B) break
    C) continue

    Answer: break
    7. Use range to generate a list from 1 through 5?
    A) range(1, 5)
    B) range(5)
    C) range(1,6)

    Answer: range(1,6)
    8. Select the code needed to generate a random number between 1 through 50 and assign it to rand_num?
    A) random.randrange(1, 51)
    B) random.randrange(1, 50)
    C) random(1, 50)

    Answer: random.randrange(1, 51)
    9. What numbers does the following Range generate: range(5,10)?
    A) 5,6,7,8,9,10
    B) 6,7,8,9,10
    C) 5,6,7,8,9

    Answer: 5,6,7,8,9

    Explanation: Range starts at 5 and goes up to (but not including) 10
    10. What numbers does the following range generate range(3)?
    A) 3
    B) 1,2,3
    C) 0,1,2,3
    D) 0,1,2

    Answer: 0,1,2

    Explanation: Range will start at 0 and go up to (but not including) 3
    11. What is printed out after the following code nums = range(1,5) print(nums)?
    A) 1,2,3,4
    B) range(1,5)

    Answer: range(1,5)

    Explanation: Printing range does not print out all the numbers in the range. We have to iterate over the range with a loop to print the numbers.
    12. What numbers does the following range generate range(8,0,-2):
    A) 0,2,4,6
    B) 8,6,4,2
    C) 8,6,4,2,0

    Answer: 8,6,4,2

    Explanation: The 3rd argument(step) indicates that range should work backwards from 8, moving 2 numbers at a time.
    13. What does the following loop do?

    i = 1
    while i < 5:
    i + i
    print(i)

    A) It prints 1,2,3,4,5 and then exits
    B) It prints 1,2,4 then exits
    C) It prints 1 forever

    Answer: It prints 1 forever

    Explanation: Don't forget to add the + before the = sign when incrementing!
    14. What does the following loop do?

    # print 1 to 5
    i = 0
    while i <= 5:
    i =+ 1
    print(i)

    A) It prints 1 through 5 because it increments i each time
    B) It prints 0 through 5 because i starts at zero
    C) It prints 1 forever because there is a typo with the increment which should be += instead of =+.
    D) It prints zero through four because i starts at zero and goes up until 5.

    Answer: It prints 1 forever because there is a typo with the increment which should be += instead of =+.
    15. What can we do to get out of the infinite loop below?

    #this code runs forever...
    x = 0
    while x != 11:
    x += 2
    print(x)

    A) Change the condition to x ! = 10
    B) Change the condition to x < 11
    C) Add logic that says if x == 10: break
    D) Press control + C while your program is running to kill it
    E) All of the above

    Answer: All of the above
    16. For loops are used to loop over:
    A) Loopy objects
    B) Numbers
    C) Iterable objects
    D) Lines of code

    Answer: Iterable objects
    17. What is the tradeoff when using while loops for looping?
    A) While loops are more flexible since you explicitly set the start and end conditions, but they require more setup than for loops
    B) While loops are faster than for loops but require more setup
    C) While loops can loop infinitely and give you more control, but they cannot be used to iterate over iterable objects

    Answer: While loops are more flexible since you explicitly set the start and end conditions, but they require more setup than for loops
    18. What does the break keyword do?
    A) Exits the loop at the beginning of the next iteration
    B) Breaks your code
    C) Exits the loop at the end of this iteration
    D) Exits the loop immediately

    Answer: Exits the loop immediately

    Explanation: break is the fastest way to get out of a loop -- it won't run any code after the break
    19. What does the following code print?

    for x in range(5):
    print(x)

    A) 1 2 3 4 5
    B) 1 2 3 4
    C) 0 1 2 3 4

    Answer: 0 1 2 3 4

    Explanation: Python ranges start at zero by default and count up to, but not including, the end number.
  • GKNXT Encyclopedia
    0 A B C D E
    F G H I J K
    L M N O P Q
    R S T U V W
    X Y Z #
    colors list
    Collective Nouns List
    Top 10 Lists
    World Flags