Unit 4: AP CSP - Python Control Structures Quizzes

0.0(0)
studied byStudied by 8 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/35

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

36 Terms

1
New cards

BOOLEANS QUIZ - 4.1.2

Which of the following is not a valid value for a boolean?


True

False

yes

The above are all valid

yes

2
New cards

LOGICAL OPERATORS QUIZ - 4.2.2

After the following code runs, what is the value of is_weekend?

is_saturday = True

is_sunday = False

is_weekend = is_saturday or is_sunday


True

False

"is_weekend"

"is_saturday || is_sunday"

True

3
New cards

LOGICAL OPERATORS QUIZ - 4.2.2

What symbol represents the and operator in Python?


AND

and

&

&&

and

4
New cards

LOGICAL OPERATORS QUIZ - 4.2.2

What symbol represents the or operator in Python?


OR

or

|

||

or

5
New cards

COMPARISON OPERATORS QUIZ - 4.3.2

What is the proper way to compare if two values are equal in a boolean expression in Python?


=

==

EQUALS

is

==

6
New cards

COMPARISON OPERATORS QUIZ - 4.3.2

What is the proper operator for "not equals" in Python?


=!

!=

~=

NOT EQUALS

!=

7
New cards

AP PRACTICE: COMPARISON OPERATORS - 4.3.7

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.

Determine whether the following expression would evaluate to true or false.

7 = 6 OR 8 ≥ 4


false

true

true

8
New cards

AP PRACTICE: COMPARISON OPERATORS - 4.3.7

Determine whether the following expression would evaluate to true or false.

(9 ≠ 13 AND 12 < 4) OR 15 < 9


false

true

false

9
New cards

AP PRACTICE: COMPARISON OPERATORS - 4.3.7

Which of the following Boolean expressions is equivalent to the expression

a AND (b OR c)


(a AND b) OR c

(a AND b) OR (a AND c)

(a AND b) AND (a AND c)

(b AND c) OR a

(a AND b) OR (a AND c)

10
New cards

AP PRACTICE: COMPARISON OPERATORS - 4.3.7

Which of the following Boolean expressions is equivalent to the expression

(a OR b) AND NOT (c OR d)


(a OR b) AND ((NOT c) OR (NOT d))

(a AND NOT (c OR d)) OR b

(a OR b) AND (NOT c) AND (NOT d)

NOT (a AND b) AND NOT (c OR d)

(a OR b) AND (NOT c) AND (NOT d)

11
New cards

IF STATEMENTS QUIZ - 4.4.2

Assume you are writing a program, and you have a boolean variable called b, defined like so:

b = True

Pick the correct if statement to follow the code above. The if statement should be correct Python, and the body of the if statement should only run if b is True.



if b:

(tab)print("b is True!")


if b:

print("b is True!")


if True:

(tab)print(b)


if True:

print(b)

if b:

(tab)print("b is True!")

12
New cards

IF STATEMENTS QUIZ - 4.4.2

Which of the following programs will not print anything?



x = True

if x:

(tab)print("hi")


if False:

(tab)print("hi")


x = False

if x:

(tab)print("hi")

else:

(tab)print("hello")


if False:

(tab)print ("hi")

else:

(tab)print("Hello")

if False:

(tab)print("hi")

13
New cards

AP PRACTICE: IF/ELSE STATEMENTS - 4.4.9

In the following code block, assume that the variables rainy and tooCold are boolean.

IF (NOT (rainy OR tooCold))

{

DISPLAY("It's a good beach day")

}

Which of the following are equivalent to the above code block?



IF (( NOT rainy) OR (NOT tooCold))

{

DISPLAY("It's a good beach day")

}


IF (( NOT rainy) AND tooCold)

{

DISPLAY("It's a good beach day")

}


IF (( NOT rainy) AND (NOT tooCold))

{

DISPLAY("It's a good beach day")

}


IF (rainy AND tooCold)

{

DISPLAY("It's a good beach day")

}

IF (( NOT rainy) AND (NOT tooCold))

{

DISPLAY("It's a good beach day")

}

14
New cards
<p>AP PRACTICE: IF/ELSE STATEMENTS - 4.4.9</p><p>If the variables onTime and absent both have the value false, what is displayed as a result of running the code segment?</p><div data-type="horizontalRule"><hr></div><p>Is anyone there?</p><p>Better late than never.</p><p>Hello. Is anyone there?</p><p>Hello. Better late than never.</p>

AP PRACTICE: IF/ELSE STATEMENTS - 4.4.9

If the variables onTime and absent both have the value false, what is displayed as a result of running the code segment?


Is anyone there?

Better late than never.

Hello. Is anyone there?

Hello. Better late than never.

Better late than never.

15
New cards

AP PRACTICE: IF/ELSE STATEMENTS - 4.4.9

A summer camp offers a morning session and an afternoon session. The list morningList contains the names of all children attending the morning session, and the list afternoonList contains the names of all children attending the afternoon session.

Only children who attend both sessions eat lunch at the camp. The camp director wants to create lunchList, which will contain the names of children attending both sessions.

The following code segment is intended to create lunchList, which is initially empty. It uses the procedure IsFound (list, name), which returns true if name is found in list and returns false otherwise. You also will use the APPEND(list, name) procedure which will add a name to a given list.

FOR EACH child IN morningList

{

<MISSING CODE>

}

Which of the following could replace <MISSING CODE> so that the code segment works as intended?



IF (IsFound (afternoonList, child))

{

APPEND (lunchList, child)

}


IF (IsFound (lunchList, child))

{

APPEND (afternoonList, child)

}


IF (IsFound (morningList, child))

{

APPEND (lunchList, child)

}


IF ((IsFound (morningList, child)) OR (IsFound (afternoonList, child)))

{

APPEND (lunchList, child)

}

IF (IsFound (afternoonList, child))

{

APPEND (lunchList, child)

}

16
New cards
<p>AP PRACTICE: IF/ELSE STATEMENTS - 4.4.9</p><p>An office building has two floors. A computer program is used to control an elevator that travels between the two floors. Physical sensors are used to set the following Boolean variables.</p><p>The elevator moves when the door is closed and the elevator is called to the floor that it is not currently on. Which of the following Boolean expressions can be used in a selection statement to cause the elevator to move.</p><div data-type="horizontalRule"><hr></div><p>(onFloor1 AND callTo2) AND (onFloor2 AND callTo1)</p><p>(onFloor1 AND callTo2) OR (onFloor2 AND callTo1)</p><p>(onFloor1 OR callTo2) AND (onFloor2 OR callTo1)</p><p>(onFloor1 OR callTo2) OR (onFloor2 OR callTo1)</p>

AP PRACTICE: IF/ELSE STATEMENTS - 4.4.9

An office building has two floors. A computer program is used to control an elevator that travels between the two floors. Physical sensors are used to set the following Boolean variables.

The elevator moves when the door is closed and the elevator is called to the floor that it is not currently on. Which of the following Boolean expressions can be used in a selection statement to cause the elevator to move.


(onFloor1 AND callTo2) AND (onFloor2 AND callTo1)

(onFloor1 AND callTo2) OR (onFloor2 AND callTo1)

(onFloor1 OR callTo2) AND (onFloor2 OR callTo1)

(onFloor1 OR callTo2) OR (onFloor2 OR callTo1)

(onFloor1 AND callTo2) OR (onFloor2 AND callTo1)

17
New cards

KEY EVENTS QUIZ - 4.5.2

We’ve written a function animate that moves a ball across the screen like this:

def animate(event):

ball.move(5, 5)

How can we set up animate to be called every time a key on the keyboard is pressed down?


add_key_down_handler(animate())

add_key_down_handler("animate")

add_key_down_handler(animate(event))

add_key_down_handler(animate)

add_key_down_handler(animate)

18
New cards

FOR LOOPS QUIZ - 4.6.2

How many times will the following program print "hello"?

for i in range(5):

print("hello")


4

5

6

i

5

19
New cards

FOR LOOPS QUIZ - 4.6.2

In the following code, what will be the last number to print to the screen before the program finishes?

for i in range(10):

if i % 2 == 0:

print(i)

else:

print(2 * I)


10

20

9

18

18

20
New cards

GENERAL FOR LOOP QUIZ - 4.7.2

How many times will the following code print "hello"?

for i in range(3, 8):

print("hello")


3

5

6

8

5

21
New cards

GENERAL FOR LOOP QUIZ - 4.7.2

How many times will the following code print "hello"?

for i in range(0, 8, 2):

print("hello")


0

2

4

8

4

22
New cards

FOR LOOP EXAMPLES QUIZ - 4.8.2

Why do we use constant variables?


To avoid having “magic numbers” (unique numbers with unexplained meaning) in our code

To give values a readable name, so that their purpose is clear

To let us easily change the behavior of our program by only having to change a value in one place

All of the above

All of the above

23
New cards

FOR LOOP EXAMPLES QUIZ - 4.8.2

What will be the value of sum after this code runs?

START = 1

END = 5

sum = 0;

for i in range(START, END):

sum += i


10

4

1

40

10

24
New cards

RANDOM NUMBERS QUIZ - 4.9.2

Which of the following returns a random number between 1 and 10?


randint(1,10)

random.randint(1,10)

random.randint(1,11)

randint(1,11)

random.randint(1,10)

25
New cards

RANDOM NUMBERS QUIZ - 4.9.2

How many possible values can the following code return random.choice([1,5]) return?


2

4

5

6

2

26
New cards

WHILE LOOPS QUIZ - 4.10.2

How many times will this program print "hello"?

i = 50

while i < 100:

print("hello")


0

50

100

This code will loop infinitely

This code will loop infinitely

27
New cards

WHILE LOOPS QUIZ - 4.10.2

How many times will this program print "hello"?

i = 10

while i > 0:

print("hello")

i -= 1


0

10

i

This code will loop infinitely

10

28
New cards

AP PRACTICE: ITERATION - 4.10.6

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.

Consider the following code segment:

REPEAT 2 TIMES

{

MOVE_FORWARD ()

MOVE_FORWARD ()

ROTATE_LEFT ()

}

Which of the following code segment options would produce the same result as the code above?



REPEAT 4 TIMES

{

MOVE_FORWARD ()

ROTATE_LEFT ()

}


REPEAT 2 TIMES

{

MOVE_FORWARD ()

MOVE_FORWARD ()

}

ROTATE_LEFT ()


REPEAT 2 TIMES

{

MOVE_FORWARD ()

MOVE_FORWARD ()

}

REPEAT 2 TIMES

{

ROTATE_LEFT ()

ROTATE_LEFT ()

}


REPEAT 2 TIMES

{

MOVE_FORWARD ()

}

ROTATE_LEFT ()

REPEAT 2 TIMES

{

MOVE_FORWARD ()

}

ROTATE_LEFT ()

REPEAT 2 TIMES

{

MOVE_FORWARD ()

}

ROTATE_LEFT ()

REPEAT 2 TIMES

{

MOVE_FORWARD ()

}

ROTATE_LEFT ()

29
New cards

AP PRACTICE: ITERATION - 4.10.6

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?


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

The program correctly displays the sum of all positive integers from 1 to number

The program does not work as intended but rather it displays the number factorial.

The program does not work as intended because sum should be initialized to 1.

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

30
New cards

AP PRACTICE: ITERATION - 4.10.6

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?


The program correctly displays the count of even numbers between 1 and number

The program does not work as intended because it displays the count of odd numbers between 1 and number

The program does not work as intended because it displays count but should instead display i

The program does not work as intended because the condition in the if statement needs to say (number MOD 2 = 0)

The program correctly displays the count of even numbers between 1 and number

31
New cards

AP PRACTICE: ITERATION - 4.10.6

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 (true)

}

ELSE

{

RETURN (false)

}

}

Which of the following best describes the result of running the Mystery procedure?


The result will always be true for any initial value of number

The result will always be false for any initial value of number

The result will be true whenever the initial value of number is even

The result will be true whenever the initial value of number is odd

The result will be true whenever the initial value of number is even

32
New cards

AP PRACTICE: ITERATION - 4.10.6

In the procedure Mystery written below, the parameter number is a positive integer.

PROCEDURE Mystery (number)

{

value ← 0

REPEAT UNTIL (number = 0)

{

IF (number MOD 2 = 0)

{

value ← value + 1

}

ELSE

{

value ← value - 1

}

number ← number - 1

}

IF (value = 0)

{

RETURN (true)

}

ELSE

{

return (false)

}

}

Which of the following best describes the result of running the Mystery procedure?


The result will always be true for any initial value of number

The result will always be false for any initial value of number

The result will be true whenever the initial value of number is even

The result will be true whenever the initial value of number is odd

The result will be true whenever the initial value of number is even

33
New cards

AP PRACTICE: ITERATION - 4.10.6

Which initial value of number would cause an infinite loop?

REPEAT UNTIL(number = 0)

{

number ← number - 1

}


Any positive integer.

Any negative integer.

Any even integer.

Any odd integer.

Any negative integer.

34
New cards

AP PRACTICE: ITERATION - 4.10.6

Which initial value of number would cause the loop to be skipped?

REPEAT UNTIL(number MOD 2 = 0)

{

number ← number - 1

}


Any positive integer.

Any negative integer.

Any even integer.

Any odd integer.

Any even integer.

35
New cards

LOOP AND A HALF QUIZ - 4.11.2

If I am using the following condition in my program:

while True:

which keyword should I use to make sure I don’t create an infinite loop?


break

continue

break

36
New cards

LOOP AND A HALF QUIZ - 4.11.2

Which Python keyword skips back to the beginning of a loop?


break

continue

continue