Python Unit 6: Conditionals Practice Questions
Practice questions for Python Unit 6 on conditionals (if, elif, and else statements and boolean logic), with explanations for every answer.
Questions
What does this program print? is_open = 9 < 5 print(is_open)
- False
- 9 < 5
- is_open
- True
What does this program print? a = 10 b = a == 10 print(type(b))
- <class 'bool'>
- True
- <class 'int'>
- <class 'str'>
has_key holds the text "False" in quotes. What does the program print? has_key = "False" if has_key: print("open") else: print("locked")
- open
- locked
- Nothing is printed
- TypeError
What does this program print? ready = True print("Ready: " + ready)
- TypeError
- Ready:True
- Ready: ready
- Ready: True
What does this program print? ready = True print(f"Ready: {ready}")
- Ready: True
- Ready: ready
- Ready: {ready}
- TypeError
What happens when this runs? done = false print(done)
- NameError
- False
- None
is_big is set while x is 7, then x changes. What prints? x = 7 is_big = x > 5 x = 2 print(is_big)
- True
- 2
- x > 5
- False
What does this program print? light = True light = not light light = not light light = not light print(light)
- False
- not light
- NameError
- True
A program needs to remember whether a student turned in homework so it can use the value in an if statement later. Which line is the best choice?
- turned_in = True
- turned_in = "yes"
- turned_in == True
What does this program print? score = 72 passed = score >= 70 print(passed, not passed)
- True False
- 72 True
- False True
- True True
Which program prints True?
- x = 4 print(x % 2 == 0)
- x = 4 print(x / 2 == 0)
- x = 4 print("x % 2 == 0")
- x = 5 print(x % 2 == 0)
What does this program print? is_member = True print(str(is_member) + "!")
- True!
- TypeError
- is_member!
- 1!
The user types True. What does the program print? answer = input("Done? ") print(answer == True)
- Done? True False
- Done? True True
- TypeError
- False
A student writes needs_water = "True" (with quotes). Printing it looks the same as the Boolean. What is the real problem?
- It is a string, so even "False" would count as true
- Python cannot print a string that looks like a Boolean
- It raises a NameError the moment the variable is printed
- Nothing; quotes around True and False are optional
What does this program print? wet = True print(wet == "True")
- False
- None
- True
- wet
Which variable name best describes a Boolean that stores whether a game is over?
- game_over
- game
- over_count
- game_over_text
What does this program print? a = 3 > 4 b = not a print(a) print(b)
- False True
- False False
- True True
- True False
temp changes after freezing is set. What prints? temp = 10 freezing = temp <= 32 temp = 50 print(freezing)
- True
- TypeError
- False
- 10 >= 10
In Python, the Boolean values True and False must be capitalized.
What does this program print? print(not 0)
Which of these values are falsy in Python?
- 0
- "" (an empty string)
- False
- "False" (a non-empty string)
- -1
What does this program print? temp = 95 if temp > 90: print("Hot") print("Done")
- Hot Done
- Hot
- Done
- Done Hot
What does this program print? temp = 60 if temp > 90: print("Hot") print("Done")
- Done
- Nothing is printed
- Hot Done
- Hot
What does this program print? x = 3 if x > 5: print("A") print("B") print("C")
- C
- A B C
- B C
- Nothing is printed
What does this program print? x = 3 if x > 5: print("A") print("B") print("C")
- B C
- C
- A C
- A B C
What does this program print? age = 15 if age >= 16: print("Drive") else: print("Wait") print("Bye")
- Wait Bye
- Wait
- Drive Wait Bye
- Drive Bye
What happens when this runs? x = 1 if x == 1 print("one")
- SyntaxError
- NameError
- TypeError
- one
n is 0. What prints? n = 0 if n: print("non-zero") else: print("zero")
- zero
- non-zero
- TypeError
- Nothing is printed
What does this program print? points = 10 if points > 5: points = points * 2 print(points)
- 20
- 10
- 15
- Nothing is printed
What does this program print? points = 10 if points > 50: points = points * 2 else: points = points - 1 print(points)
- 9
- 20
- 19
- 10
Two separate ifs change x. What is printed? x = 5 if x > 3: x = x - 4 if x > 3: x = x + 10 print(x)
- 1
- 11
- 5
- 15
What does this program print? x = 2 if x > 3: x = x - 4 else: x = x + 10 if x > 3: x = x * 2 print(x)
- 24
- 2
- 12
- -2
The user types kai (lowercase). What is printed? name = input("Name: ") if name == "Kai": print("Welcome back") else: print("Hello, stranger")
- Name: kai Hello, stranger
- Hello, stranger
- Name: kai Welcome back
- Name: kai
Which program prints I can swim! when can_swim is True?
- can_swim = True if can_swim: print("I can swim!") else: print("I can't swim.")
- can_swim = True if can_swim print("I can swim!")
- can_swim = True if not can_swim: print("I can swim!")
- can_swim = True if can_swim = True: print("I can swim!")
What does this program print? raining = True have_umbrella = False if raining: if have_umbrella: print("Walk") else: print("Wait") else: print("Walk")
- Wait
- Walk Wait
- Walk
- Nothing is printed
A thermostat should print Heat on when temp is below 65 and do nothing otherwise. Which structure fits best?
- A single if with no else
- Two separate if statements that both print Heat on
- An if/else whose else prints Heat on
- An if/elif/else with three branches
What does this program print? x = 10 if x > 5: print("big") else: print("small") print("check")
- big
- big check
- small check
- check
What does this program print? x = 1 if x > 5: print("big") else: print("small") print("check")
- small check
- big check
- small big check
- small
What does this program print? flag = False if not flag: flag = True if flag: print("on") else: print("off")
- on
- on off
- Nothing is printed
- off
What does this program print? s = 7 if s > 0: print("a") if s > 5: print("b") else: print("c") print("d")
- a b d
- b d
- a b c d
- a d
Which line runs no matter what value x has? x = int(input("x: ")) if x > 0: print("positive") print("checked") print("finished")
- Line 5
- Line 2
- Line 4
- Line 3
Which change makes Wash your hands print only when hungry is True? hungry = True if hungry: print("Get a snack") print("Wash your hands")
- Indent line 4 to line up with line 3
- Change the if on line 2 into an elif
- Add else: on its own line above line 4
- Remove the colon at the end of line 2
Every if statement must end with an else.
What does this program print? a = 4 b = 9 print(a < b, a >= b, a != b)
- True False True
- False False True
- True True False
- True False False
What does this program print? print(10 <= 10, 10 < 10)
- True False
- True True
- False True
- False False
What does this program print? print("apple" == "Apple")
- False
- True
- apple
- TypeError
What does this program print? print(3 == 3.0)
- True
- TypeError
- False
- 3.0
What does this program print? print("5" == 5)
- False
- 5
- TypeError
- True
The user types 15. What happens? age = input("Age: ") print(age > 12)
- Age: 15 TypeError
- Age: 15 False
- Age: 15 True
- Age: 15 ValueError
The user types 15. What is printed? age = int(input("Age: ")) print(age > 12)
- Age: 15 True
- True
- Age: 15 TypeError
- Age: 15 False