AP CODING
You have the following program below. What is the difference between the largest and smallest value that could get printed to the screen? x ← RANDOM(1, 2) y ← RANDOM(1, 3) DISPLAY(x * y)
A. 1 B. 2 C. 5 D. 6 ANSWER: C
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. REPEAT 2 TIMES { MOVE_FORWARD () MOVE_FORWARD () ROTATE_LEFT () } 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 above?
A) REPEAT 4 TIMES { MOVE_FORWARD () ROTATE_LEFT () } B) REPEAT 2 TIMES { MOVE_FORWARD () MOVE_FORWARD () } ROTATE_LEFT () C) REPEAT 2 TIMES { MOVE_FORWARD () MOVE_FORWARD () } REPEAT 2 TIMES { ROTATE_LEFT () ROTATE_LEFT () } D) REPEAT 2 TIMES { MOVE_FORWARD () } ROTATE_LEFT () REPEAT 2 TIMES { MOVE_FORWARD () } ROTATE_LEFT () ANSWER: D (draw the box and test)
Which of the following Boolean expressions is not equivalent to the expression num * -1 ≥ 10
A. (num < 0) AND (num * -1 = 10) B. (num < -10) OR (num = -10) C. (num * -1 > 10) OR (num = -10) D. NOT num * -1 < 10 ANSWER: A (num Must be -10 or a smaller number -11 etc...)
What are the possible values that could result from the following line of code? RANDOM(1, 5)
A. 1, 2, 3, 4 B. 1, 2, 3, 4, 5 C. 2, 3, 4 D. 2, 3, 4, 5 ANSWER: B
In the procedure Mystery written below, the parameter number is a positive integer greater than 1. PROCEDURE Mystery (number) { REPEAT UNTIL (number ≥ 100) { number ← number * number } IF (number = 100) { RETURN (true) } ELSE { RETURN (false) } } Which of the following best describes the result of running the Mystery procedure?
A. The procedure returns true when the initial value of number is less than 100, and otherwise, it returns false B. The procedure returns true when the initial value of number is 10, and otherwise, it returns false C. The procedure returns true when the initial value of number is 100, and otherwise, it returns false D. The procedure returns true when the initial value of number is either 10 or 100, and otherwise, it returns false
Consider the following program, which is intended to print the count of even numbers between 1 and number count ← 0 i ← 1 REPEAT number TIMES { IF (i MOD 2 = 0) { count ← count + 1 } i ← i + 1 } DISPLAY count Which of the following best describes the behavior of this program?
A. The program correctly displays the count of even numbers between 1 and number B. The program does not work as intended because it displays the count of odd numbers between 1 and number C. The program does not work as intended because it displays count but should instead display i D. The program does not work as intended because the condition in the if statement needs to say (number MOD 2 = 0) ANSWER: A (do the math step by step)
You are writing a program to play a dice game using a fair die with 6 sides. What is the proper call to the RANDOM function that would simulate rolling the die and saving the value in a variable?
A. roll ← RANDOM(1, 1) B. roll ← RANDOM(1, 6) C. roll ← RANDOM(1, 7) D. roll ← RANDOM(6, 6) ANSWER: B
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 the following code segment, how many possible squares could the robot end in? x ← RANDOM (1, 4) REPEAT x TIMES { MOVE_FORWARD () } ROTATE_LEFT () y ← RANDOM (1, x) REPEAT y TIMES { MOVE_FORWARD () }
A. 1 B. 4 C. 10 D. 16 ANSWER: c (draw all the possibilities)
In the procedure Mystery written below, the parameter number is a positive integer greater than 1. PROCEDURE Mystery (number) { REPEAT 4 TIMES { number ← number + 1 } RETURN (number) } Which of the following will be the result of running the Mystery procedure?
A. number * 4 B. number + 4 C. number + 1 D. 4 ANSWER: B
Which of the following Boolean expressions is equivalent to the expression a AND (b OR c)
A. (a AND b) OR c B. (a AND b) OR (a AND c) C. (a AND b) AND (a AND c) D. (b AND c) OR a ANSWER: B (pretty easy)
You are writing a program to simulate a coin toss. What line of code could be used to replace so that there is a 50-50 chances of your program printing heads or tails? IF(<MISSING CONDITION>) { DISPLAY("Heads") } ELSE { DISPLAY("Tails") }
a. RANDOM(1, 2) > 2 b. RANDOM(1, 2) < 1 c. RANDOM(1, 2) = 1 d. RANDOM(1, 2) = 0 ANSWER: C
Which of the following Boolean expressions is equivalent to the expression a * b > 0
A. (a > 0) AND (b > 0) B. (a > 0) OR (b > 0) C. (a > 0) OR (b < 0) D. ((a > 0) AND (b > 0)) OR ((a < 0) AND (b < 0)) ANSWER: D (the product of two negatives is a positive)
Which of the following Boolean expressions is equivalent to the expression (a OR b) AND NOT (c OR d)
A. (a OR b) AND ((NOT c) OR (NOT d)) B. (a AND NOT (c OR d)) OR b C. (a OR b) AND (NOT c) AND (NOT d) D. NOT (a AND b) AND NOT (c OR d) ANSWER: C
Which of the following Boolean expressions is equivalent to the expression num MOD 3 = 0
A. num MOD 3 ≠ 1 B. (num MOD 3 ≠ 1) AND (NOT num MOD 3 = 1) C. (NOT num MOD 3 = 1) AND (NOT num MOD 3 = 2) D. num / 3 ≠ 0 ANSWER: C (remainder for divided by 3 can only be (0, 1, 2))
If you have the following line of code, what are the chances that x has the value of an even number? Line 1: x ← RANDOM(1, 5)
20%, 40%, 50%, 100% ANSWER: 40%
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 sum Which of the following best describes the behavior of this program?
A. The program correctly displays the sum of all positive integers from 1 to number B. The program does not work as intended but rather it displays the numbersquared. C. The program does not work as intended but rather it displays the numberfactorial. D. The program does not work as intended because sum should be initialized to 1. ANSWER: B (do the math on paper number by number)