AP CPSP UNIT 2 Practice Test Flashcards

Prepared from AP practice test online.

1
Q

Q1 Behavior of mystery procedure with if statement

In the following procedure, assume that the parameter x is an integer.

Which of the following best describes the behavior of the procedure?

A) It displays nothing if x is negative and displays true otherwise.
B) It displays nothing if x is negative and displays false otherwise.
C) It displays true if x is negative and displays nothing otherwise.
D) It displays true if x is negative and displays false otherwise.

A

C
It displays true if x is negative and displays nothing otherwise.

Correct. When x is negative, y is assigned the value true and the value of y is displayed. When x is not negative, y is assigned the value false and the display statement is never executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q3 Error in reducing score with if else

In the following code segment, score and penalty are initially positive integers. The code segment is intended to reduce the value of score by penalty. However, if doing so would cause score to be negative, score should be assigned the value 0.

For example, if score is 20 and penalty is 5, the code segment should set score to 15.
If score is 20 and penalty is 30, score should be set to 0.

The code segment does not work as intended.

Line 1: IF(score - penalty < 0)
Line 2: {
Line 3: score score - penalty
Line 4: }
Line 5: ELSE
Line 6: {
Line 7: score 0
Line 8: }

Which of the following changes can be made so that the code segment works as intended?
Responses

A) Changing line 1 to IF(score < 0)
B) Changing line 1 to IF(score + penalty < 0)
C) Changing line 7 to score score + penalty
D) Interchanging lines 3 and 7

A

D) Interchanging lines 3 and 7

Correct. Interchanging lines 3 and 7 ensures that if reducing score by penalty results in a negative value, score is set to 0. Otherwise, score is reduced by penalty.

Related Videos

1.4: Daily Video 1
1.4: Daily Video 2
1.4: Daily Video 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q4 Which variables are equal to 50 after assignments

Consider the following code segment.

x 25
y 50
z 75
x y
y z
z x

Which of the variables have the value 50 after executing the code segment?

A) x only
B) y only
C) x and z only
D) x, y, and z

A

Answer C

The first three statements assign values to the variables. The fourth statement assigns the value of y (which is 50) to x. The fifth statement assigns the value of z (which is 75) to y. The sixth statement assigns the value of x (which is 50) to z. Therefore, x and z both have the value 50.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q15 Values of variables after arithmetic operations

Consider the following code segment.

What is displayed as a result of executing the code segment?

A) 10 20 30 40
B) 21 30 40 50
C) 21 40 30 40
D) 21 40 30 50

A

Answer D

The first five statements assign values to the variables. The sixth statement assigns the value of x + b (which is 40) to b. The seventh statement assigns the value of x + 1 (which is 21) to a. The eighth statement assigns the value of c + d / 2 to d. According to the order of operations, division has higher precedence than addition. Since c is 30 and d / 2 is 20, d is assigned the value 50. The last four statements display the values of a, b, c, and d.

3.3: Daily Video 1

3.3: Daily Video 2

3.3: Daily Video 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q18 Values after sequence of Boolean expressions

Consider the following code segment.
What is displayed as a result of executing the code segment?

A) true true true
B) false false false
C) true false true
D) false false true

A

Answer B

The first three statements assign values to the variables. The fourth statement assigns the value of (NOT (a OR b)) AND c to a. Since a OR b is true, NOT (a OR b) is false, so (NOT (a OR b)) AND c is false. The fifth statement assigns the value of c AND a to c. Since a is now false, c AND a is false. The last three statements display the values of the variables.

Related Content & Skills
Skill
4.B
Related Videos

3.5: Daily Video 1
3.5: Daily Video 2
3.5: Daily Video 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q19 Set maxPS based on time

In a certain video game, the variable maxPS represents the maximum possible score a player can earn. The maximum possible score depends on the time it takes the player to complete the game. The value of maxPS should be 30 if time is greater than 120 and 50 otherwise.

Which of the following code segments correctly sets the value of maxPS based on the value of time ?

Select two answers.

A

Answer A
This code segment sets maxPS to 50 by default, then uses the IF statement to set maxPS to 30 when time > 120.

Answer D
This code segment uses the IF statement to set maxPS to 30 when time > 120 and uses the ELSE statement to set maxPS to 50 otherwise.

Related Content & Skills

Skill2.B
Related Videos
3.6: Daily Video 1
3.6: Daily Video 2
3.6: Daily Video 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q21 Value of sum after IF ELSE IF

Consider the following code segment.

What is the value of sum after the code segment is executed?

A) 12
B) 14
C) 16
D) 18

A

Answer C

The first three statements assign values to the variables. Since num1 < num2 evaluates to false, the body of the ELSE block is executed and num3 is assigned the value 4. Since num2 ≥ num3 evaluates to true, the body of the second IF block is executed and num1 is assigned the value 8. Lastly, sum is assigned the value of 8 + 4 + 4, or 16.

3.6: Daily Video 1
3.6: Daily Video 2
3.6: Daily Video 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q22 Assign bonus based on score

In a certain game, the integer variable bonus is assigned a value based on the value of the integer variable score.

If score is greater than 100, bonus is assigned a value that is 10 times score.
If score is between 50 and 100 inclusive, bonus is assigned the value of score.
If score is less than 50, bonus is assigned a value of 0.
Which of the following code segments assigns bonus correctly for all possible integer values of score ?

Select two answers.

A

IF(score > 100)
{
bonus score * 10
}
ELSE
{
IF(score ≥ 50)
{
bonus score
}
ELSE
{
bonus 0
}
}

B

IF(score ≥ 50)
{
IF(score > 100)
{
bonus score * 10
}
ELSE
{
bonus 0
}
}
ELSE
{
bonus score
}

C

IF(score < 50)
{
bonus 0
}
ELSE
{
IF(score ≥ 50)
{
bonus score
}
ELSE
{
bonus score * 10
}
}

D

IF(score < 50)
{
bonus 0
}
ELSE
{
IF(score > 100)
{
bonus score * 10
}
ELSE
{
bonus score
}
}

A

Answer A
This code segment sets bonus to score * 10 when score > 100, sets bonus to score when 100 ≥ score ≥ 50, and sets bonus to 0 when score < 50.

Answer D
This code segment sets bonus to 0 when score < 50, sets bonus to score * 10 when score > 100, and sets bonus to score when 100 ≥ score ≥ 50.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q24 Algorithm to determine youngest person

Suppose a large group of people in a room were all born in the same year. Consider the following three algorithms, which are each intended to identify the people in the room who have the earliest birthday based on just the month and day. For example, a person born on February 10 is considered to have an earlier birthday than a person born on March 5. Which of the three algorithms will identify the correct people?

I) All the people in the room stand up. All standing people form pairs where possible, leaving at most one person not part of a pair. For each pair, the person with the earlier birthday remains standing, while the other person in the pair sits down. If there is a tie, both people sit down. Any individual not part of a pair remains standing. Continue doing this until only one person remains standing. That person has the earliest birthday.

II) All the people in the room stand up. All standing people form pairs with another standing person that they have not previously been paired with where possible, leaving at most one person not part of a pair. For each pair, the person with the earlier birthday remains standing, while the other person in the pair sits down. If there is a tie, both people in the pair remain standing. Any individual not part of a pair remains standing. Continue doing this until only one person remains standing or all persons standing have the same birthday. Anyone still standing has the earliest birthday.

III) Beginning with the number 1, ask if anyone was born on that day of any month. Continue with the numbers 2, 3, and so on until a positive response is received. If only one person responds, that person has the earliest birthday. If more than one person responds, determine which person was born in the earliest month, and that person or those persons have the earliest birthday.

A) I only
B) II only
C) I and II
D) II and III

A

Answer B - II Only

Algorithm I does not work correctly. In algorithm I, if two people are tied for the earliest birthday, they both sit down when they are eventually paired.
Algorithm II works correctly. Because algorithm II allows both people to remain standing when there is a tie, a person with the earliest birthday is not eliminated. Algorithm II continues until all remaining people have the same birthday, which is the earliest birthday.
Algorithm III does not work correctly. Algorithm III chooses the person(s) with the earliest day, disregarding the month. For example, algorithm III will incorrectly determine that a person born on February 1 has an earlier birthday than a person born on January 5.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q25 Algorithm to print integers

Which of the following algorithms display all integers between 1 and 20, inclusive, that are not divisible by 3 ?

Select two answers.

A)
Step 1: Set x to 0.
Step 2: Increment x by 1.
Step 3: If x is not divisible by 3, then display x.
Step 4: Repeat steps 2 and 3 until x is 20.

B)
Step 1: Set x to 0.
Step 2: If x is divisible by 3, then display x.
Step 3: Increment x by 1.
Step 4: Repeat steps 2 and 3 until x is greater than 20.

C)
Step 1: Set x to 1.
Step 2: If x is divisible by 3, then do nothing; otherwise display x.
Step 3: Increment x by 1.
Step 4: Repeat steps 2 and 3 until x is 20.

D)
Step 1: Set x to 1.
Step 2: If x is divisible by 3, then do nothing; otherwise display x.
Step 3: Increment x by 1.
Step 4: Repeat steps 2 and 3 until x is greater than 20.

A

Answer A
This algorithm displays the numbers 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, and 20. After 20 is displayed, the algorithm terminates.

Answer D
This algorithm displays the numbers 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, and 20. After 20 is displayed, x is incremented to 21. The value 21 is not displayed since it is a multiple of 3 and the algorithm terminates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q26 Values after REPEAT UNTIL block

Consider the following code segment.

What is displayed as a result of executing the code segment?

A) true false false
B) true false true
C) true true false
D) true true true

A

Answer D

At the beginning of the first iteration of the loop, the values of a, b, and c are true, false, and true, respectively. At the end of the first iteration of the loop, the values of a, b, and c are true, false, and false, respectively. At the end of the second (and last) iteration of the loop, the values of a, b, and c are true, true, and true, respectively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q27 If statement code that produces the same result

Consider the following code segment with an integer variable num.

IF(num > 0)
{
DISPLAY(“positive”)
}
IF(num < 0)
{
DISPLAY(“negative”)
}
IF(num = 0)
{
DISPLAY(“zero”)
}
Which of the following code segments is equivalent to the code segment above?

A)

IF(num < 0)
{
DISPLAY(“negative”)
}
ELSE
{
DISPLAY(“positive”)
}
IF(num = 0)
{
DISPLAY(“zero”)
}

B)

IF(num < 0)
{
DISPLAY(“negative”)
}
ELSE
{
IF(num = 0)
{
DISPLAY(“zero”)
}
ELSE
{
DISPLAY(“positive”)
}
}

C)

IF(num ≤ 0)
{
DISPLAY(“negative”)
}
ELSE
{
IF(num = 0)
{
DISPLAY(“zero”)
}
ELSE
{
DISPLAY(“positive”)
}
}

D)
IF(num ≤ 0)
{
DISPLAY(“negative”)
}
IF(num = 0)
{
DISPLAY(“zero”)
}
ELSE
{
DISPLAY(“positive”)
}

A

Answer B

The given code segment displays “positive” when num is positive, displays “negative” when num is negative, and displays “zero” when num is 0. This code segment produces the same result. When num is negative, “negative” is displayed. Otherwise, when num is 0, “zero” is displayed. Otherwise, “positive” is displayed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q29 Algorithm to place three numbers in order

Three different numbers need to be placed in order from least to greatest. For example, if the numbers are ordered 9, 16, 4, they should be reordered as 4, 9, 16. Which of the following algorithms can be used to place any three numbers in the correct order?

A
If the first number is greater than the last number, swap them. Then, if the first number is greater than the middle number, swap them.
B
If the first number is greater than the middle number, swap them. Then, if the middle number is greater than the last number, swap them.
C
If the first number is greater than the middle number, swap them. Then, if the middle number is greater than the last number, swap them. Then, if the first number is greater than the last number, swap them.
D
If the first number is greater than the middle number, swap them. Then, if the middle number is greater than the last number, swap them. Then, if the first number is greater than the middle number, swap them.

A

Answer D

After comparing the first number and the middle number and potentially swapping them, and then comparing the middle number and the last number and potentially swapping them, the largest number will be in the third position. Next, after comparing the first number and the middle number and potentially swapping them, the three numbers will be ordered from least to greatest.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q31 Store even numbers in evenList

Consider the following code segment, which is intended to store ten consecutive even integers, beginning with 2, in the list evenList. Assume that evenList is initially empty.

i 1
REPEAT 10 TIMES
{

<MISSING>
}
Which of the following can be used to replace <MISSING> so that the code segment works as intended?

A)
APPEND(evenList, i)
i i + 2

B)
i i + 2
APPEND(evenList, i)

C)
APPEND(evenList, 2 * i)
i i + 1

D)
i i + 1
APPEND(evenList, 2 * i)
</MISSING></MISSING>

A

Answer C

For the first iteration of the loop, twice the value of i, or 2, is appended to evenList, and then i is incremented to 2. For the second iteration of the loop, twice the value of i, or 4, is appended to the list, and then i is incremented to 3. This continues eight more times, appending the next eight even numbers to evenList. This code segment will generate the list [2, 4, 6, 8, 10, 12, 14, 16, 18, 20].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q35 Value displayed by call to doSomething procedure

Consider the following procedure.

PROCEDURE doSomething(num1, num2)
{
DISPLAY(num1)
RETURN(num1)
DISPLAY(num2)
}
Consider the following statement.

DISPLAY(doSomething(10, 20))
What is displayed as a result of executing the statement above?

A) 10 10
B) 10 20
C) 10 10 20
D) 10 20 10

A

Answer A

The first statement in the doSomething procedure displays the value of num1, or 10. The second statement returns the value 10 and terminates execution of the procedure. The returned value 10 is displayed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q36 Procedure most useful in calculating an average

A student has a data file containing 10,000 numerical values. The student is writing a program to compute the average of the numbers contained in the file. Which of the following procedures is most likely to be useful in the student’s program?

A) A procedure that returns the quotient when dividing one value by another
B) A procedure that returns the sum when adding one value to another
C) A procedure that returns the sum of the values in the file
D) A procedure that returns true if the file contains any duplicate values and returns false otherwise

A

Answer C

The sum of the values in the file can be divided by the number of values in the file (10,000) to compute the desired average.

17
Q

Q37 Call to RANDOM to simulate game spinner

Consider a game where a player spins a wheel divided into four identical sections, labeled A, B, C, and D. If the player spins A, the score is 10. If the player spins B, the score is 5. If the player spins C or D, the score is -1.

The following code segment is intended to implement the game.

Which of the following could be used as a replacement for <MISSING> so the code segment works as intended?
A
spin ← RANDOM [1, 3]
B
spin ← RANDOM [1, 4]
C
spin ← RANDOM [1, 6]
D
spin ← RANDOM [1, 8]</MISSING>

A

Answer B

The code segment sets score to 10 25% of the time, to 5 25% of the time, and to -1 the remaining 50% of the time.

18
Q

Q38 Displaying random values in a loop

Consider the following code segment.
Which of the following CANNOT be displayed as a result of executing the code segment?

A) 1 1 1 1
B) 1 2 3 2
C) 1 2 3 4
D) 1 3 2 4

A

Answer D

In the second iteration of the loop, i is equal to 2. Thus RANDOM(1, i) returns either 1 or 2. So, the output 1 3 2 4 is not possible.

19
Q

Q39 Spin spinner three times

A spinner contains 12 regions of equal size. The regions are numbered 1 to 12. Which of the following code segments can be used to simulate the results of spinning the spinner three times and assigns the sum of the values obtained by the three spins to the variable sum ?
Responses

A) sum RANDOM(1, 12) + RANDOM(1, 12) + RANDOM(1, 12)
B) sum RANDOM(1, 36)
C) sum 3 * RANDOM(1, 12)
D) sum 12 * RANDOM(1, 3)

A

Answer A

The first spin is simulated by the first call to RANDOM, which returns a random integer between 1 and 12, inclusive. Similarly, the second and third spins are simulated by the second and third calls to RANDOM, producing a total of three integer values. The sum of the three values is assigned to sum.

20
Q

Q44 Publishing software using Creative Commons

A programmer created a piece of software and wants to publish it using a Creative Commons license. Which of the following is a direct benefit of publishing the software with this type of license?

A) The programmer can ensure that the algorithms used in the software are free from bias.
B) The programmer can ensure that the source code for the software is backed up for archival purposes.
C) The programmer can include code that was written by other people in the software without needing to obtain permission.
D) The programmer can specify the ways that other people are legally allowed to use and distribute the software.

A

Answer D

A Creative Commons license allows an author to impose restrictions on the use of the licensed product, including restrictions on commercial use.

21
Q

Q45 Action that raises legal or ethical concerns

Which of the following actions is most likely to raise legal or ethical concerns?
Responses
A) An analyst writes a program that scans through a database of open-access scientific journals and creates a document with links to articles written on a particular topic.
B) A computer scientist adds several features to an open-source software program that was designed by another individual.
C) A musician creates a song using samples of a copyrighted work and then uses a Creative Commons license to publish the song.
D) A public interest group alerts people to a scam that involves charging them for a program that is available for free under a Creative Commons license.

A

Answer C
The use of material created by someone else without permission raises ethical concerns and can have legal consequences. Using a Creative Commons license to publish a derived work does not free the musician from having to first obtain permission.