I Hate CBT's

View Original

4.12.1 Python Control Structures Quiz Answers

Question: The AP Exam does not use for loops and while loops, but rather REPEAT or REPEAT UNTIL commands as shown below.REPEAT n TIMES { <block of statements> } REPEAT UNTIL(condition) { <block of statements> }The program below uses a robot in a 5x5 grid of squares. The robot is represented as a triangle, which is initially in the bottom-left square of the grid and facing toward the right of the grid.After running which of the following code segments would the robot end up in the same place facing the same direction as after running the code segment below?REPEAT 2 TIMES { MOVE_FORWARD () } ROTATE_LEFT () REPEAT 2 TIMES { MOVE_FORWARD () } ROTATE_LEFT ()

Answer: REPEAT 2 TIMES { MOVE_FORWARD () MOVE_FORWARD () ROTATE_LEFT () }

Question: We want to print the phrase "CodeHS is the best" exactly 25 times. What kind of control structure should we use?

Answer: For loop

Question: The AP Exam uses the following relational (comparison) operators: =, ≠, >, <, ≥, and ≤.As well, AND, OR and NOT are used instead of and, or and not.A comparison using a relational operator evaluates to a Boolean value. For example, a = b evaluates to true if a and b are equal; otherwise, it evaluates to false.Which of the following Boolean expressions is equivalent to the expression?(a AND b) OR (a AND c)

Answer: a AND (b OR c)

Question: What will the following program print when run?for j in range(2): for i in range(6, 4, -1): print (i)

Answer: 6565

Question: What will the following program print when run?number_one = 5 number_two = 10 if number_one == 5: print(1) if number_one > 5: print(2) if number_two < 5: print(3) if number_one < number_two: print(4) if number_one != number_two: print(5)

Answer: 145

Question: What is the value of the boolean variable can_vote at the end of this program?age = 17 is_citizen = True can_vote = age >= 18 and is_citizen

Answer: FALSE

Question: Which of the following could NOT be an output from the following code?mystery_num = 10 * random.randint(1,10) print(mystery_num)

Answer: 0

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: Question: 9The AP Exam does not use for loops and while loops, but rather REPEAT or REPEAT UNTIL commands as shown below.REPEAT n TIMES { <block of statements> } REPEAT UNTIL(condition) { <block of statements> }Consider the following program, which is intended to print the sum of all the positive integers up to number.sum ← 0 REPEAT number TIMES { sum ← sum + number } DISPLAY sumWhich of the following best describes the behavior of this program?

Answer: The program does not work as intended but rather it displays the number squared.

Question: What is the output of the following program?result = 0 max = 5 for i in range(max): result += i print(result)

Answer: 10

Question: What will be the output of this program?number = 5 greater_than_zero = number > 0 if greater_than_zero: print(number)

Answer: 5

Question: The AP Exam uses the following relational (comparison) operators: =, ≠, >, <, ≥, and ≤.As well, AND, OR and NOT are used instead of and, or and not.In the following code block, assume that the variables rainy and tooCold are boolean.IF ((NOT rainy) AND (NOT tooCold)) { DISPLAY("It's a good beach day") }Which of the following are equivalent to the above code block?

Answer: IF (NOT (rainy OR tooCold)) { DISPLAY("It's a good beach day") }

Question: What will the following program print when run?above_16 = True has_permit = True passed_test = False if above_16 and has_permit and passed_test: print("Issue Driver's License") else: if above_16 or has_permit or passed_test: print("Almost eligible for Driver's License") else: print("No requirements met.")

Answer: Almost eligible for Driver's License

Question: The AP Exam does not use for loops and while loops, but rather REPEAT or REPEAT UNTIL commands as shown below.REPEAT n TIMES { <block of statements> } REPEAT UNTIL(condition) { <block of statements> }Consider the following program, which is intended to print the count of odd numbers between 1 and numbercount ← 0 i ← 1 REPEAT number TIMES { IF (i MOD 2 = 1) { count ← count + 1 } i ← i + 1 } DISPLAY countWhich of the following best describes the behavior of this program?

Answer: The program correctly displays the count of odd numbers between 1 and number

Question: What is printed by the following program?is_raining = False is_cloudy = False is_sunny = not is_raining and not is_cloudy is_summer = False is_warm = is_sunny or is_summer print("Is it warm: " + str(is_warm))

Answer: Is it warm: True

Question: How many times will the following program print "hello"?i = 0 while i < 10: print("hello")

Answer: This code will loop infinitely

Question: The AP Exam uses the following relational (comparison) operators: =, ≠, >, <, ≥, and ≤.As well, AND, OR and NOT are used instead of and, or and not.A county 911 system uses an automated computer program to dispatch one of two ambulances. In order to dispatch the ambulance, the ambulance needs to be available and the emergency needs to be in the ambulance's primary zone or the other ambulance is not available.The following boolean variables are used:ambOneAvail - set to true is ambulance one is available, otherwise set to falseambTwoAvail - set to true is ambulance two is available, otherwise set to falseinZone - Set to true if the emergency is in the primary zone for the ambulance, otherwise set to falseWhich of the following Boolean expressions can be used in a selection statement to cause ambulance one to be dispatched?

Answer: ambOneAvail AND (inZone OR NOT ambTwoAvail)

Question: What will be the output of this program?number = 5 greater_than_zero = number > 0 if greater_than_zero: if number > 5: print(number)

Answer: Nothing will print

Question: Which of the following returns a random number between 1 and 99?

Answer: random.randint(1,99)

Question: What will be the output when the following code runs?logged_in = False print("User logged in?: " + str(not logged_in))

Answer: User logged in?: True

Question: What is printed by the following program?num_apples = 10 num_oranges = 5 if num_apples < 20 or num_oranges == 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

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: The following code continually asks the user for a password until they guess the correct password, then ends. But there is one problem.1 SECRET_PASSWORD = "karel" 2 3 while True: 4 password = input("Enter your password: ") 5 if password == SECRET_PASSWORD: 6 print("You got it!") 7 print("Incorrect password, please try again.")Which of the following will fix this program?

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

Question: The AP Exam does not use for loops and while loops, but rather REPEAT or REPEAT UNTIL commands as shown below.REPEAT n TIMES { <block of statements> } REPEAT UNTIL(condition) { <block of statements> }In the procedure Mystery written below, the parameter number is a positive integer.PROCEDURE Mystery (number) { value ← number REPEAT UNTIL (number = 0) { value ← value * -1 number ← number - 1 } IF (value > 0) { RETURN (false) } ELSE { RETURN (true) } }Which of the following best describes the result of running the Mystery procedure?

Answer: The result will be false whenever the initial value of number is even

Question: (9 doesn't equal 7 AND 17 is less than 4) OR 65 is less than 9Determine what the following expression would evaluate to.(sorry i had to type this one out wierdly bc the question would crash quizlet??? idk)

Answer: FALSE