AP Computer Science Unit 2 Review
(Edhesive)
What will be the output of the following program if the user input number is -5? - ✔✔-15
A travel organization organizes a trip to London. The price changes based on the nu
...
AP Computer Science Unit 2 Review
(Edhesive)
What will be the output of the following program if the user input number is -5? - ✔✔-15
A travel organization organizes a trip to London. The price changes based on the number of
applicants. The following program is used to display the price of the trip, and uses the variable
Applicants which is set to the number of applicants for the trip.
If the number of applicants is 60, what will be displayed? - ✔✔The price of the trip is $250!
Consider the following code:
IF (n > 0)
{
DISPLAY ("The number is positive.")
}
ELSE
{
DISPLAY ("The number is not positive.")
}
What will be output by this code if the number n is zero? - ✔✔The number is not positive
Which of the following blocks will display even if the positive number num is even? Select two
answers. - ✔✔IF ((num MOD 2) = 0)
{DISPLAY ["even"]}
AND
IF ((num MOD 2) = 1)
{DISPLAY ["odd"]}
ELSE
{DISPLAY ["even"]}
Below is a program that is supposed to be a "Guess a Number" game, wherein the user is trying
to accurately guess a randomly generated number between 1 and 100. However, the program is
not functioning as intended.
Which two lines in the program need swapped in order for this program to work correctly? Select
two answers. - ✔✔LINE 3
AND
LINE 5
The following uses a robot in a grid of squares. The robot is represented by a triangle and is in
the upper left corner facing toward the bottom of the grid.
Which blocks of code will properly navigate the robot to the final destination of the grey square
in the bottom right corner of the grid? Select two answers. - ✔✔Both of the boxes that state
"Repeat 4 Times"
Which of the following best describes the result of running the program below?
number ← RANDOM (1, 6)
REPEAT UNTIL number > 100
{
number ← number * 2
}
IF (number = 128)
{
DISPLAY (true)
}
ELSE
{
DISPLAY (false)
} - ✔✔If number is a 1, 2 or 4, then the program will display true.
Consider the following equivalent code segments that each display the first 12 positive and even
integers.
Which of the following is NOT an advantage of Segment B over Segment A? - ✔✔Segment B
will execute in less time than Segment A.
A program has been developed to compute the sum of all elements with a value greater than 10
in a list of integers. Which of the following statements best describes how iteration may be used
to in the program in this example? - ✔✔The program will repeat the execution of a block of
commands which test the value of an item and add it to a running total (if it is greater than 10)
for each value in the list.
The program below calculates and displays the sum of the integers from m to n inclusive, where
m < n.
Line 1 i ← m
Line 2 r ← n-m+1
Line 3 sum ← 0
Line 4 REPEAT r TIMES
Line 5 {
Line 6 sum ← sum+i
Line 7 i ← i+1
Line 8 }
Line 9 DISPLAY sum
If Line 1 is changed to read i ← n, which of the following changes will ensure that the program
continues to calculates and displays the sum of the integers from m to n inclusive? - ✔✔Change
line 7 to read i ← i-1
A study of beech trees shows that around 40% of these trees may be susceptible to a newly
discovered disease. The computer program below is intended to determine the number of trees
which may be susceptible to the disease in a forest containing k beech trees:
Line 1: total ← 1
Line 2: repeat k times
Line 3: {
Line 4: IF ()
Line 5: {
Line 6: total ← total + 1
Line 7: }
Line 8: }
Line 9: DISPLAY (total)
Which of the following could be used to replace to ensure the
program works as intended? Select two answers. - ✔✔RANDOM (1, 5) < 3
AND
RANDOM (1, 5) ≤ 2
Which of these statements will move the sprite 100 steps? - ✔✔{repeat (10)
{move (10) steps
{ ->}
What would a Sprite say as a result of executing this script? - ✔✔13
A standard deck of playing cards contains 52 cards, with each card being one of four suits:
SPADES, HEARTS, CLUBS, and DIAMONDS. Each suit is represented by 13 cards whose
values include ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK,
QUEEN, and KING. Consider the following two algorithms that are intended to find the card
representing the "TEN of HEARTS" in a randomly shuffled deck and separate it from the other
51 cards in the deck.
Algorithm A
Step 1: Pick up the shuffled deck of cards.
Step 2: Look at the topmost card in your hands.
Step 3: If the card's suit is a HEART, place the card on top of the pile to your left.
Step 4: Otherwise, place the card on top of the pile to your right.
Step 5: Repeat steps 2 through 4 until you are no longer holding any cards.
Step 6: Pick up the cards in the pile to your left.
Step 7: Look at the topmost card in your hands.
Step 8: If the card's value is a TEN, place all other cards in your hands on top of the pile to your
right.
Step 9: Otherwise, place the card on top of the pile to your right.
Step 10: Repeat steps 7 through 9 until you are holding only one card.
Step 11: The only card in your hand is the TEN of HEARTS.
Algorithm B
Step 1: Pick up the shuffled deck of cards.
Step 2: Look at the topmost card in your hands.
Step 3: If the card's suit is a HEART or the card's value is a TEN, place all other cards in your
hands on top of the pile to your right.
Step 4: Otherwise, place the card on top of the pile to your right.
Step 5: Repeat steps 2 through 4 until you are holding only one card.
Step 6: The only card in your hand is the TEN of HEARTS. - ✔✔Only Algorithm A will always
work as intended.
The following procedure, validate (x), is intended to display "PASS" if an integer, x, is within
the range of 128 through 32768, inclusive. Values of x that are not within the range should
display "FAIL".
Which of the following s
[Show More]