# Set #2¶

The following questions make up Set #2 of the Untimed Practice Exam Questions. The questions resemble, both in format and substance, what you might see on the AP CS Principles exam. You should finish these questions within 17 minutes to satisfy the time constraints of the AP exam. You may refer to the AP CS Reference Sheet, which can be found here.

Click the “Start” button when you are ready to begin the exam. Click the “Pause” button to pause the exam (you will not be able to see the questions when the exam is paused). It will show how much time you have used, but you have unlimited time. Click on the “Finish Exam” button at the end when you are done. The number correct, number wrong, and number skipped will be displayed at the bottom of the page. Feedback for each answer will also be shown as well as your answer.

You will not be able to change your answers after you hit the “Finish Exam” button.

25-2-1: What will the following code print?

PROCEDURE counter(lst):

count ← 0

FOR EACH item IN lst:

{ count ← count + item }

RETURN (count)

new_list ← [1,2,3,4]

run_counter ← counter(new_list)

DISPLAY (run_counter)

• 10
• Correct. PROCEDURE counter(lst) is called on new_list and adds together its values.
• [1,2,3,4]
• Incorrect. This is what is assigned to the variable new_list.
• 4
• Incorrect. This is the final value in new_list.
• None
• Incorrect. The value of run_counter is what is returned from count(new_list).

25-2-2: Which of the following is NOT why we stop at 100 in the following code?

sum ← 0

numbers ← RANGE(0,101,2)

FOR EACH number IN numbers:

{ sum ← sum + number }

DISPLAY(sum)

• Because we started at 0.
• Incorrect. Since we start at 0, we are counting by even numbers with a step of 2.
• Because the range only extends to 101.
• Incorrect. Since the range extends to 101, the last number we use is 100.
• Because the computer only understands 1s and 0s.
• Correct. Binary code is not relevant to why this code stops at 100.
• Because we are using a step of 2.
• Incorrect. Since we start at 0 and use a step of 2, we are counting by even numbers up to 101.

25-2-3: Which of the following will result in an infinite loop?

x ← “heLLo”

while x[2] == ’L’:

DISPLAY(x)

x ← x.split(‘h’)

(B)

my_num ← 6

while my_num ≠ 0:

DISPLAY(“Hello World”)

my_num ← my_num % 2

(C)

counter ← 10

while counter > 0:

DISPLAY(counter)

counter ← counter + 1

(D)

x ← 5

while x > 0:

DISPLAY(x)

x ← x - 1

DISPLAY(“x is now” + x)

• Incorrect. After the .split() function, the while loop will break.
• Incorrect. The remainder of 6 divided by 2 is equal to 0 and the loop will break.
• Correct. The variable counter begins at a value greater than 0 and continues to increase which leads to an infinite loop.
• Incorrect. The program will eventually end since x is subtracted by 1 each time through the loop.

25-2-4: What number should be added in the blank to make the following function print the number 6?

x ← 3

i ← 0

while i < 3:

x ← i*___

i ← i + 1

DISPLAY(x)

• 4
• Incorrect. Variable x will never equal 6 if 4 is added in the blank.
• 2
• Incorrect. Variable x will never equal 6 if 2 is added in the blank.
• i
• Incorrect. Variable x will never equal 6 if i is added in the blank.
• 3
• Correct. After two times through the loop, variable i will equal 2 and it will be multiplied by 3 which equals 6.

25-2-5: What will the following code print?

newS ← “?”

phrase ← ”Where are you going with that?”

for EACH item in phrase:

{ newS ← newS + item }

DISPLAY(newS)

• "Where are you going with that?"
• Incorrect. We are adding onto the existing value of newS which is "?", so newS will begin with a "?".
• Nothing
• Incorrect. newS has a value, therefore something will display.
• "?"
• Incorrect. "? is the initial value of the variable newS".
• "?Where are you going with that?"
• Correct. The code iterates through the string phrase and adds each character in the string to the variable newS which is initially "?".

25-2-6: Refer to the code below. Suppose someone wants to test this. When they reach line 3, they enter “5.” What will happen?

Line 1: x ← 1

Line 2: DISPLAY(x)

Line 3: x ← INPUT()

Line 4: DISPLAY(x)

• The following output occurs: 1x
• Incorrect. Assigning a value of 1 to variable x does not change its value to 1x.
• An error occurs. A person could use the INPUT() function like this, but nothing is entered into the INPUT() function, so nothing would print out.
• Incorrect. This is not true. "5" is entered into the INPUT() function .
• The following output occurs: 1 5
• Correct. When x is displayed in line 2, its value is 1. When x is displayed in line 4, its value is 5.
• An error occurs. You cannot display the value of variables in programming.
• Incorrect. This is false. You can displayed the value of variables in programming.

25-2-7: Tucker is writing his first program. He wants the program to say “hey” to him. Below is an overview of what he hopes the program will do:

2. The user enters in their name: TUCKER
3. The computer displays: “Hey TUCKER”.

Which of the following programs will do what Tucker wants?

(A)

x ← INPUT()

DISPLAY(x)

DISPLAY(“Hey”)

(B)

x ← INPUT()

DISPLAY(“Hey”)

DISPLAY(x)

(C)

DISPLAY(“Hey”)

x ← INPUT()

DISPLAY(x)

(D)

x ← INPUT()

DISPLAY(x)

DISPLAY(“Tucker”)

• Incorrect. This code will display "Tucker Hey Please enter your name".
• Correct. This code will ask to enter your name and store it in variable x. Then, will display Hey Tucker.
• Incorrect. This code will display "Hey Please enter your name Tucker".
• Incorrect. This code will display "Tucker Tucker".
25-2-8: Given two variables, num1 and num2, which of the following would mean that both num1 and num2 are positive integers?
• (num1 = num2)
• Incorrect. The two variables can be equal to each other and still be negative integers.
• (num1 = num2) OR (num1 ≠ num2)
• Incorrect. Regardless of whether the two variables are equal or not equal to one another, they can still be negative.
• (num1 = num2) AND (num1<0)
• Incorrect. The two variables would both be negative in this case.
• (num1 = num2) AND (num2>0)
• Correct. If num1 is equal to num2 and num2 is greater than 0, then both values must be positive.

25-2-9: Consider the code below.

IF(freshman)

{ DISPLAY(“I am a freshman.”) }

If freshman is True, what is displayed?

• DISPLAY(“I am a freshman.”)
• Incorrect. Only what is inside the quotations in the DISPLAY function gets displayed.
• “I am a freshman.”
• Correct. The text in quotations inside the DISPLAY function gets displayed when called on.
• DISPLAY(freshman)
• Incorrect. The variable freshman is never called on in the DISPLAY function in this code.
• Nothing will print out.
• Incorrect. Nothing would print if freshman were not True.

25-2-10: Consider the code below.

IF(freshman)

{ DISPLAY(“I am a freshman.”) }

ELSE

{ DISPLAY(“I am not a freshman.”)}

If the variable freshman is false, what is displayed?

• “I am a freshman.”
• Incorrect. This would print if freshman were True.
• Nothing is displayed.
• Incorrect. Nothing would display if there were no ELSE clause.
• "I am not a freshman"
• Correct. Since freshman is False and there is an ELSE clause, the block after the ELSE is run.
• DISPLAY("I am not a freshman")
• Incorrect. Only the text inside the quotations in the DISPLAY function is displayed.

25-2-11: Consider the code below.

IF(freshman)

{ DISPLAY(“I am a freshman.”) }

ELSE

IF(sophomore)

{ DISPLAY(“I am a sophomore”) }

ELSE

{ DISPLAY(“Who knows what I am?”) }

If the variable sophomore is false, what is displayed?

• “I am a sophomore.”
• Incorrect. Sophomore is False, so this would not be displayed.
• "I am not a freshman"
• Incorrect. We do not know if freshman is True, so we cannot say whether this would be displayed.
• "Who knows what I am?"
• Incorrect. We do not know if freshman is True, so we cannot say whether this would be displayed.
• It is impossible to tell with the given information.
• Correct. Since we do not know whether freshman is True, we cannot say whether the code block under freshman is run or if the ELSE statement after sophomore will be executed.

25-2-12: Consider the code below.

IF(freshman)

{ DISPLAY(“I am a freshman.”) }

ELSE

IF(sophomore)

{ DISPLAY(“I am a sophomore”) }

ELSE

{ DISPLAY(“Who knows what I am?”) }

If variables freshman and sophomore are false, what is displayed?

• “I am a sophomore.”
• Incorrect. Sophomore is False, so this would not be displayed.
• "I am not a freshman"
• Incorrect. Freshman is False, so this would not be displayed.
• "Who knows what I am?"
• Correct. Since we know that freshman is and sophomore are False, the ELSE block after sophomore is executed.
• It is impossible to tell with the given information.
• Incorrect. Since we know that freshman is and sophomore are False, the ELSE block after sophomore is executed.

25-2-13: Consider the code below.

IF(freshman)

{ DISPLAY(“I am a freshman.”) }

ELSE

IF(sophomore)

{ DISPLAY(“I am a sophomore”) }

ELSE

{ DISPLAY(“Who knows what I am?”) }

If freshman is False and sophomore is True, what is displayed?

• “I am a sophomore.”
• Correct. Sophomore is True, so the code block after is executed.
• "I am not a freshman"
• Incorrect. Freshman is False, so this would not be displayed.
• "Who knows what I am?"
• Incorrect. Since we know that freshman is False and sophomore are True, the ELSE block after sophomore is not executed.
• It is impossible to tell with the given information.
• Incorrect. Sophomore is True, so the code block after is executed.

25-2-14: Consider the following code:

x ← 2

figure ← 0

REPEAT UNTIL figure > 15

{

figure ← 3 * x

x ← x + 1

}

DISPLAY(“figure =”)

DISPLAY(figure)

DISPLAY(“, x =”)

DISPLAY(x)

What is displayed as a result of running the code above?

• figure = 15, x = 6
• Incorrect. The code runs for 5 periods total. We start in period 0 with fig = 0 and x = 2. The value for x increases by 1 after each period. So, the values for figure are as follows for every period: After period 2, figure = 3 * 3 = 9 and x = 4; after period 3, figure = 3 * 4 = 12 and x = 5; after period 4, figure = 3 * 5 = 15 and x = 6; after period 5, figure = 3 * 6 = 18 and x = 7. The program will stop after period 5 since figure exceeds 15.
• figure = 18, x = 3
• Incorrect. The code runs for 5 periods total. We start in period 0 with fig = 0 and x = 2. The value for x increases by 1 after each period. So, the values for figure are as follows for every period: After period 2, figure = 3 * 3 = 9 and x = 4; after period 3, figure = 3 * 4 = 12 and x = 5; after period 4, figure = 3 * 5 = 15 and x = 6; after period 5, figure = 3 * 6 = 18 and x = 7. The program will stop after period 5 since figure exceeds 15.
• figure = 15, x = 7
• Incorrect. The code runs for 5 periods total. We start in period 0 with fig = 0 and x = 2. The value for x increases by 1 after each period. So, the values for figure are as follows for every period: After period 2, figure = 3 * 3 = 9 and x = 4; after period 3, figure = 3 * 4 = 12 and x = 5; after period 4, figure = 3 * 5 = 15 and x = 6; after period 5, figure = 3 * 6 = 18 and x = 7. The program will stop after period 5 since figure exceeds 15.
• figure = 18, x = 7
• Correct. The code runs for 5 periods total. We start in period 0 with fig = 0 and x = 2. The value for x increases by 1 after each period. So, the values for figure are as follows for every period: After period 2, figure = 3 * 3 = 9 and x = 4; after period 3, figure = 3 * 4 = 12 and x = 5; after period 4, figure = 3 * 5 = 15 and x = 6; after period 5, figure = 3 * 6 = 18 and x = 7. The program will stop after period 5 since figure exceeds 15.

25-2-15: Consider the following code:

x ← 1

figure ← 0

REPEAT UNTIL figure > 20

{

figure ← 5 * x

x ← x + 1

}

DISPLAY(“figure =”)

DISPLAY(figure)

DISPLAY(“, x =”)

DISPLAY(x)

What is displayed as a result of running the code above?

• figure = 30, x = 6
• Incorrect. The code runs for 5 periods total. We start in period 0 with fig = 0 and x = 1. The value for x increases by 1 after each period. So, the values for figure are as follows for every period: After period 2, figure = 5 * 2 = 10 and x = 3; after period 3, figure = 5 * 3 = 15 and x = 4; after period 4, figure = 5 * 4 = 20 and x = 5; after period 5, figure = 5 * 5 = 25 and x = 6. The program will stop after period 5 since figure exceeds 20.
• figure = 20, x = 5
• Incorrect. The code runs for 5 periods total. We start in period 0 with fig = 0 and x = 1. The value for x increases by 1 after each period. So, the values for figure are as follows for every period: After period 2, figure = 5 * 2 = 10 and x = 3; after period 3, figure = 5 * 3 = 15 and x = 4; after period 4, figure = 5 * 4 = 20 and x = 5; after period 5, figure = 5 * 5 = 25 and x = 6. The program will stop after period 5 since figure exceeds 20.
• figure = 25, x = 6
• Correct. The code runs for 5 periods total. We start in period 0 with fig = 0 and x = 1. The value for x increases by 1 after each period. So, the values for figure are as follows for every period: After period 2, figure = 5 * 2 = 10 and x = 3; after period 3, figure = 5 * 3 = 15 and x = 4; after period 4, figure = 5 * 4 = 20 and x = 5; after period 5, figure = 5 * 5 = 25 and x = 6. The program will stop after period 5 since figure exceeds 20.
• figure = 25, x = 5
• Incorrect. The code runs for 5 periods total. We start in period 0 with fig = 0 and x = 1. The value for x increases by 1 after each period. So, the values for figure are as follows for every period: After period 2, figure = 5 * 2 = 10 and x = 3; after period 3, figure = 5 * 3 = 15 and x = 4; after period 4, figure = 5 * 4 = 20 and x = 5; after period 5, figure = 5 * 5 = 25 and x = 6. The program will stop after period 5 since figure exceeds 20.