Python Unit 7: Loops Practice Questions
Practice questions for Python Unit 7 on loops (while and for loops, iteration, and loop tracing), with explanations for every answer.
Questions
What does this program print?\n\nn = 1\nwhile n < 20:\n n = n * 2\nprint(n)
- 32
- 16
- 20
- 64
What does this program print?\n\nn = 1\nwhile n < 20:\n print(n)\n n = n * 2
- 1\n2\n4\n8\n16
- 2\n4\n8\n16\n32
- 1\n2\n4\n8\n16\n32
- 2\n4\n8\n16
What does this program print?\n\ncount = 5\nwhile count > 0:\n count = count - 2\nprint(count)
- -1
- 0
- 1
- -2
How many times does the loop body run? The program prints the answer.\n\nk = 0\ntimes = 0\nwhile k < 17:\n k = k + 4\n times = times + 1\nprint(times)
- 5
- 4
- 6
- 17
What does this program print?\n\nx = 50\nwhile x < 10:\n print(x)\n x = x + 1\nprint('end')
- end
- 50\nend
- Nothing is printed
- 50
Which loop never stops?
- x = 1\nwhile x != 10:\n x = x + 2
- x = 1\nwhile x < 10:\n x = x + 2
- x = 1\nwhile x != 9:\n x = x + 2
- x = 20\nwhile x < 10:\n x = x + 2
The user types hi, yo, stop. What is printed?\n\nword = input('Word: ')\nwhile word != 'stop':\n print(f'got {word}')\n word = input('Word: ')
- Word: hi\ngot hi\nWord: yo\ngot yo\nWord: stop
- Word: hi\nWord: yo\nWord: stop
- Word: hi\ngot hi\nWord: yo\ngot yo\nWord: stop\ngot stop
- got hi\ngot yo
The loop body never asks for a new word. The user types hi. What happens?\n\nword = input('Word: ')\nwhile word != 'stop':\n print(f'got {word}')
- got hi prints forever
- SyntaxError
- got hi prints once
- It asks again
The user types 12, 123456, 4821. What is printed?\n\npin = int(input('PIN (4 digits): '))\nwhile pin < 1000 or pin > 9999:\n print('Try again')\n pin = int(input('PIN (4 digits): '))\nprint('Saved')
- PIN (4 digits): 12\nTry again\nPIN (4 digits): 123456\nTry again\nPIN (4 digits): 4821\nSaved
- Try again\nTry again\nSaved
- PIN (4 digits): 12\nTry again\nPIN (4 digits): 123456\nSaved
- PIN (4 digits): 12\nTry again\nPIN (4 digits): 123456\nTry again\nPIN (4 digits): 4821\nTry again\nSaved
A loop must keep asking while the rating is outside 1-5. Which condition goes after while?
- rating < 1 or rating > 5
- rating < 1 and rating > 5
- rating >= 1 and rating <= 5
- rating > 1 or rating < 5
What does this program print?\n\ntotal = 0\nn = 1\nwhile n <= 4:\n total = total + n\n n = n + 1\nprint(total)
- 10
- 6
- 4
- 15
What does this program print?\n\nx = 100\nsteps = 0\nwhile x > 1:\n x = x // 2\n steps = steps + 1\nprint(steps)
- 6
- 50
- 7
- 5
What does this program print?\n\nx = 2\nwhile x < 30:\n x = x * x\nprint(x)
- 256
- 30
- 64
- 16
Savings grow 10% a year. How many years until the balance reaches 150?\n\nbalance = 100\nyears = 0\nwhile balance < 150:\n balance = balance * 1.1\n years = years + 1\nprint(years)
- 5
- 6
- 4
- 50
What happens when this runs?\n\nsecret = 7\nguess = 0\ntries = 0\nwhile guess != secret:\n guess = guess + 3\n tries = tries + 1\nprint(tries)
- It never ends: guess skips 7
- It prints 3, then stops
- It prints 7, then stops
- It prints 2, then stops
What does this program print?\n\nn = 10\nwhile n > 0:\n print(n)\n n = n - 4
- 10\n6\n2
- 6\n2
- 10\n6
- 10\n6\n2\n-2
What does this program print?\n\nn = 3\nwhile n > 0:\n n = n - 1\n print(n)
- 2\n1\n0
- 3\n2\n1\n0
- 3\n2\n1
- 2\n1
What does this program print?\n\nn = 3\nwhile n > 0:\n print(n)\n n = n - 1\nprint('after', n)
- 3\n2\n1\nafter 0
- 2\n1\n0\nafter 0
- 3\n2\n1\nafter 1
- 3\n2\n1\n0\nafter 0
Which task needs a while loop rather than a for loop?
- Keep asking for a password until it is correct
- Print every whole number from 1 up to 100
- Print each of the 3 options on a fixed menu
- Add up exactly 5 test scores the user types
What does this program print?\n\nlevel = 1\nxp = 0\nwhile xp < 50:\n xp = xp + 15\n level = level + 1\nprint(level, xp)
- 5 60
- 4 60
- 4 45
- 5 50
A training loop halves the error each epoch. What prints?\n\nerror = 8.0\nepochs = 0\nwhile error > 1:\n error = error / 2\n epochs = epochs + 1\nprint(epochs, error)
- 3 1.0
- 4 0.5
- 8 1.0
- 3 2.0
The loop condition is error >= 1. What prints?\n\nerror = 8.0\nepochs = 0\nwhile error >= 1:\n error = error / 2\n epochs = epochs + 1\nprint(epochs, error)
- 4 0.5
- 3 1.0
- 4 1.0
- 5 0.25
The user types 5, 8, -2, 0. What is printed?\n\ntotal = 0\nnum = int(input('n: '))\nwhile num != 0:\n total = total + num\n num = int(input('n: '))\nprint(total)
- n: 5\nn: 8\nn: -2\nn: 0\n11
- n: 5\nn: 8\nn: -2\nn: 0\n13
- n: 5\nn: 8\nn: -2\n11
- 11
A loop reads numbers until the user types 0. That 0 isn't real data; it only signals stop. What is that kind of value called?
- A sentinel value
- A loop variable
- A Boolean flag
- An accumulator
What does this program print?\n\nn = 5\nfact = 1\nwhile n > 1:\n fact = fact * n\n n = n - 1\nprint(fact)
- 120
- 20
- 60
- 24
What does this program print?\n\nx = 1\nwhile x < 10:\n x = x + 3\n if x == 7:\n print('seven')\nprint(x)
- seven\n10
- seven\nseven\n10
- seven
- 10
A while loop checks its condition before every pass, including the first.
What does this program print?\n\nn = 0\nwhile n * n < 30:\n n = n + 1\nprint(n)
What does this program print?\n\nn = 10\nsteps = 0\nwhile n != 1:\n if n % 2 == 0:\n n = n // 2\n else:\n n = 3 * n + 1\n steps = steps + 1\nprint(steps)
Which tasks are a good fit for a while loop?
- Re-prompt until the user enters valid input
- Run a game until the player quits
- Print the numbers 1 to 10
- Process exactly 30 student records from a class roster
What does this program print?\n\nfor i in range(3, 8):\n print(i)
- 3\n4\n5\n6\n7
- 0\n1\n2\n3\n4
- 3\n4\n5\n6\n7\n8
- 4\n5\n6\n7
What does this program print?\n\nfor i in range(0, 12, 4):\n print(i)
- 0\n4\n8
- 0\n4\n8\n12
- 4\n8
- 0\n3\n6\n9
What does this program print?\n\nfor i in range(9, 0, -3):\n print(i)
- 9\n6\n3
- 6\n3\n0
- Nothing is printed
- 9\n6\n3\n0
What does this program print?\n\nfor i in range(5, 5):\n print(i)\nprint('done')
- done
- 5\ndone
- 0\ndone
- Nothing is printed
What does this program print?\n\nfor i in range(1, 10, 2):\n print(i)
- 1\n3\n5\n7\n9
- 2\n4\n6\n8
- 1\n3\n5\n7\n9\n11
- 1\n3\n5\n7
Which program prints 2 4 6 8 10 (one per line)?
- for i in range(2, 11, 2):\n print(i)
- for i in range(1, 11, 2):\n print(i)
- for i in range(2, 10, 2):\n print(i)
- for i in range(0, 10, 2):\n print(i)
Which program prints 5 4 3 2 1 (one per line)?
- for i in range(5, 0, -1):\n print(i)
- for i in range(5, 1, -1):\n print(i)
- for i in range(5, 0):\n print(i)
- for i in range(1, 6):\n print(i)
What does this program print?\n\ncount = 0\nfor i in range(4, 20, 5):\n count = count + 1\nprint(count)
- 4
- 5
- 3
- 16
What does this program print?\n\ntotal = 0\nfor i in range(1, 6):\n total = total + i\nprint(total)
- 15
- 10
- 6
- 21
The print is inside the loop. What prints?\n\ntotal = 0\nfor i in range(1, 6):\n total = total + i\n print(total)
- 1\n3\n6\n10\n15
- 15\n15\n15\n15\n15
- 15
- 1\n2\n3\n4\n5
What does this program print?\n\nproduct = 1\nfor i in range(1, 5):\n product = product * i\nprint(product)
- 24
- 10
- 120
- 0
product starts at 0. What prints?\n\nproduct = 0\nfor i in range(1, 5):\n product = product * i\nprint(product)
- 0
- 10
- 24
- 1
The user types 80, 90, 100. What is printed?\n\ntotal = 0\nfor i in range(3):\n total = total + int(input('Score: '))\nprint(total / 3)
- Score: 80\nScore: 90\nScore: 100\n90.0
- Score: 80\nScore: 90\nScore: 100\n270
- Score: 80\nScore: 90\nScore: 100\n90
- 90.0
What does this program print?\n\ns = ''\nfor i in range(3):\n s = s + 'ab'\nprint(s)
- ababab
- abababab
- ab
- ab\nab\nab
What does this program print?\n\ns = ''\nfor i in range(1, 4):\n s = s + str(i)\nprint(s)
- 123
- 0123
- 1\n2\n3
- 6
Each new piece is added on the left. What prints?\n\ns = ''\nfor i in range(1, 4):\n s = str(i) + s\nprint(s)
- 321
- 1
- 6
- 123
What does this program print?\n\nfor i in range(3):\n print('hi')\nprint(i)
- hi\nhi\nhi\n2
- hi\nhi\nhi\n3
- hi\nhi\nhi
- hi\nhi\nhi\nNameError
What does this program print?\n\ncount = 0\nfor i in range(10):\n if i % 3 == 0:\n count = count + 1\nprint(count)
- 4
- 9
- 3
- 10
The user types 4, 11, 7. What is printed?\n\nlargest = 0\nfor i in range(3):\n n = int(input('n: '))\n if n > largest:\n largest = n\nprint(largest)
- n: 4\nn: 11\nn: 7\n11
- 11
- n: 4\nn: 11\nn: 7\n22
- n: 4\nn: 11\nn: 7\n7
The user types -4, -11, -7. What is printed?\n\nlargest = 0\nfor i in range(3):\n n = int(input('n: '))\n if n > largest:\n largest = n\nprint(largest)
- n: -4\nn: -11\nn: -7\n0
- n: -4\nn: -11\nn: -7\n-4
- n: -4\nn: -11\nn: -7\n-7
- n: -4\nn: -11\nn: -7\n-11