Python Loops Made Easy: 15 Quick Questions Quiz

  1. Counting with range

    When you run the code for num in range(1, 11): print(num), what does it print?

    1. Prints the numbers 1 through 10, each on its own line
    2. Prints the numbers 1 through 11, each on its own line
    3. Prints the numbers 0 through 10, each on its own line
    4. Prints only 1 and 10
    5. Prints nothing because range(1, 11) is empty
  2. Summing numbers 1 to 10

    Given sum_numbers = 0 followed by for num in range(1, 11): sum_numbers += num and then print(sum_numbers), what value is printed?

    1. 55
    2. 50
    3. 60
    4. 45
    5. 56
  3. Printing list elements

    Which loop correctly prints each element of the list my_list = [1, 2, 3] exactly once?

    1. for element in my_list: print(element)
    2. for element in my_list print(element)
    3. for element of my_list: print(element)
    4. for element in my_list: print(my_list)
    5. for i in range(len(my_list)): print(i)
  4. Product of list elements

    What value is printed by the code my_list = [2, 3, 4, 5]; product = 1; for num in my_list: product *= num; print(product)?

    1. 120
    2. 60
    3. 24
    4. 119
    5. 121
  5. Even numbers with range

    Which range call generates the even numbers from 2 to 10 (inclusive) when used as for n in ___: print(n)?

    1. range(2, 11, 2)
    2. range(2, 10, 2)
    3. range(2, 12, 2)
    4. range(1, 11, 2)
    5. range(0, 10, 2)
  6. Reverse counting with range

    Which range call will iterate over the numbers 10, 9, 8, ..., 1 when used in a for loop?

    1. range(10, 0, -1)
    2. range(10, 1, -1)
    3. range(10, -1, -1)
    4. range(1, 10, -1)
    5. rang(10, 0, -1)
  7. Iterating over a string

    Given s = 'Hello' and the loop for ch in s: print(ch), what sequence is printed?

    1. Prints H, e, l, l, o (each on its own line)
    2. Prints Hello (all on one line with no spaces)
    3. Prints o, l, l, e, H (each on its own line)
    4. Prints H e l l o (on one line separated by spaces)
    5. Prints H, e, l, l (stops early due to a typo)
  8. Finding the largest number

    What value is printed by the code my_list = [3, 9, 1, 6, 2, 8]; largest = my_list[0]; for num in my_list: if num u003E largest: largest = num; print(largest)?

    1. 9
    2. 8
    3. 6
    4. 3
    5. 1
  9. Average of a list

    Given my_list = [4, 7, 9, 2, 5], what value is printed by total = 0; for num in my_list: total += num; average = total / len(my_list); print(average)?

    1. 5.4
    2. 5
    3. 5.0
    4. 27
    5. 4.5
  10. Uppercase letters in a string

    For s = 'Hello World', what does the loop for ch in s: if ch.isupper(): print(ch) print?

    1. Prints H and W (each on its own line)
    2. Prints H only
    3. Prints W only
    4. Prints h and w (each on its own line)
    5. Prints nothing because isupper() checks digits
  11. Counting vowels

    What number is printed by the code s = 'Hello World'; vowels = 'AEIOUaeiou'; count = 0; for ch in s: if ch in vowels: count += 1; print(count)?

    1. 3
    2. 2
    3. 4
    4. 5
    5. 0
  12. Nested star pattern total

    How many total stars are printed by the code for i in range(5): for j in range(i + 1): print('*', end=''); print()?

    1. 15
    2. 10
    3. 14
    4. 20
    5. 5
  13. Factorial with a while loop

    What value is printed by the code num = 5; factorial = 1; while num u003E 0: factorial *= num; num -= 1; print(factorial)?

    1. 120
    2. 24
    3. 720
    4. 60
    5. 0
  14. Finding first occurrence index

    What value is printed by the code my_list = [3, 8, 2, 7, 4]; target = 7; index = 0; while index u003C len(my_list): if my_list[index] == target: break; index += 1; print(index)?

    1. 3
    2. 4
    3. 2
    4. 7
    5. -1
  15. Meaning of break in a loop

    In a loop that searches for a value, what does the break statement do when the value is found?

    1. It exits the loop immediately
    2. It skips to the next iteration but keeps looping
    3. It pauses the program for one second
    4. It resets the loop counter to zero
    5. It prints the word 'break' to the screen