4.12.1 Python Control Structures Quiz Answers

Question: What is the last thing printed by the following program?

start = 30 stop = 10 for i in range(start, stop - 1, -5): if i % 2 == 0: print(i * 2) else: print(i)

Answer: 20

Question: We want to simulate constantly flipping a coin until we get 3 heads in a row. What kind of loop should we use?

Answer: While Loop

Question: How many times will the following program print “hello”?

Answer: This code will loop infinitely

Question: The following code continually asks the user for a password until they guess the correct password, then ends. But there is one problem.

Answer: Add a break; statement after line 6 so that the program doesn’t loop infinitely

Question: What will the following program print when run?

for j in range(2): for i in range(6, 4, -1): print (i)

Answer: 6

5

6

5

Question: What is the value of the boolean variable can_vote at the end of this program?

age = 17 iscitizen = True canvote = age >= 18 and is_citizen

Answer: False

Question: What will be the output of this program?

number = 5 greaterthanzero = number > 0 if greaterthanzero: print(number)

Answer: 5

Question: What will be the output of this program?

number = 5 greaterthanzero = number > 0 if greaterthanzero: if number > 5: print(number)

Answer: Nothing will print

Question: What is printed by the following program?

israining = False iscloudy = False issunny = not israining and not iscloudy issummer = False iswarm = issunny or issummer print(“Is it warm: “ + str(iswarm))

Answer: Is it warm: True

Question: What is printed by the following program?

numapples = 10 numoranges = 5 if numapples < 20 or numoranges == num_apples: print(“Hello, we are open!”) else: print(“Sorry, we are closed!”) print(“Sincerely, the grocery store”)

Answer: Hello, we are open!Sincerely, the grocery store

Donation Page

Support Our Work

Do you appreciate the value this website provides? If so, please consider donating to help keep it running. Your donation will go a long way in helping us continue to provide the same quality of content and services. Every bit helps, and your support is greatly appreciated. Thank you for your generosity.