APCSP - Unit 4 CodeHS Flashcards

1
Q

what are valid values for a boolean?

A

True, False

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

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

A

True

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

what symbol represents and operator in Python?

A

and

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

what symbol represents or operator in Python?

A

or

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

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

A

==

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

what is the proper operator for “not equals” in Python?

A

!=

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

determine whether the following expression would evaluate to true or false:

7=6 OR 8≥4

A

True

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

determine whether the following expression would evaluate to true or false:

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

A

False

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

which of the following Boolean expressions is equivalent to the expression

a AND (b OR c)

A

(a AND b) OR (a AND c)

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

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) AND (NOT d)

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

which of the following are equivalent to the code block?

IF (NOT (rainy OR tooCold))
{
DISPLAY(“It’s a good beach day”)
}

A

IF ((NOT rainy) AND (NOT tooCold)))
{
DISPLAY(“It’s a good beach day”)
}

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

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

def animate(event):
ball.move(5, 5)

A

add_key_down_handler(animate)

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

how many times will the following program print “hello”?

for i in range(5):
print(“hello”)

A

5

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

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)

A

18

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

how many times will the following code print “hello”?

for i in range(3, 8):
print(“hello”)

A

5

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

how many times will the following code print “hello”?

for i in range(0, 8, 2):
print(“hello”)

A

4

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

why do we use constant variables?

A
  • to avoid having “magic numbers” (unique numbers with unexplained meaning) in our code
  • to give values a readable names 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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

A

10

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

which returns a random number between 1 and 10?

A

random.randint(1,10)

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

how many possible values can the following code return?

random.choice([1,5])

A

2

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

how many times will this program print “hello”?

i = 50
while i < 100:
print(“hello”)

A

this code will loop infinitely

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

how many times will this program print “hello”?

i = 10
while i > 0:
print(“hello”)
i -= 1

A

10

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

consider the following program, which is intended to print the sum of all the positive integers up to number. which of the following bests describes the behavior of this program?

sum ← 0
REPEAT number TIMES
{
sum ← sum + number
}
DISPLAY sum

A

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

24
Q

consider the following program, which is intended to print the count of even numbers between 1 and number. which of the following bests describes the behavior of this program?

count ← 0

i ← 1

REPEAT number TIMES
{
IF (i MOD 2 = 0)
{
count ← count + 1
}
i ← i + 1
}
DISPLAY count

A

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

25
Q

in the procedure Mystery written below, the parameter number is a positive integer. which of the following best describes the result of running the Mystery procedure?

PROCEDURE Mystery (number)
{
value ← number
REPEAT UNTIL (number = 0)
{
value ← value * -1
number ← number - 1
}
IF (value > 0)
{
RETURN (true)
}
ELSE
{
RETURN (false)
}
}

A

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

26
Q

which initial value of number would cause an infinite loop?

REPEAT UNTIL(number = 0)
{
number ← number - 1
}

A

any negative integer

27
Q

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

REPEAT UNTIL(number MOD 2 = 0)
{
number ← number - 1
}

A

any even integer

28
Q

if I am using the following condition in my program, which keyword should I use to make sure I don’t create an infinite loop?

while True:

A

break

29
Q

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

A

continue

30
Q

what is the last thing printed by the following program?

start = 30
stop = 10
for i in range(start, stop - 1, -5):
if i % 2 == 0:
print(i * 2)
else:
print(i)

A

20

31
Q

we want to simulate constantly flipping a coin until we get 3 heads in a row. what kind of loop should we use?

A

while loop

32
Q

how many times will the following program print “hello?”

i = 0
while i < 10:
print(“hello”)

A

this code will loop infinitely

33
Q

the following code continually asks the user for a password until they guess the correct password, then ends. but there is one problem. which of the following will fix this program?

SECRET_PASSWORD = “karel”
while True:
password = input(“Enter your password: “)
if password == SECRET_PASSWORD:
print(“You got it!”)
print(“Incorrect password, please try again.”)
Which of the following will fix this program?

A

add a break; statement after line 6 so that the program doesn’t loop infinitely

34
Q

what will the following program print when run?

for j in range(2):
for i in range(6, 4, -1):
print (i)

A

6
5
6
5

35
Q

what is the value of the boolean variable can_vote at the end of this program?

age = 17
is_citizen = True
can_vote = age >= 18 and is_citizen

A

False

36
Q

what will be the output of this program?

number = 5
greater_than_zero = number > 0
if greater_than_zero:
print(number)

A

5

37
Q

what will be the output of this program?

number = 5
greater_than_zero = number > 0
if greater_than_zero:
if number > 5:
print(number)

A

nothing will print

38
Q

what is printed by the following program?

is_raining = False
is_cloudy = False
is_sunny = not is_raining and not is_cloudy
is_summer = False
is_warm = is_sunny or is_summer
print(“Is it warm: “ + str(is_warm))

A

It is warm: True

39
Q

what is printed by the following program?

num_apples = 10
num_oranges = 5

if num_apples < 20 or num_oranges == num_apples:
print(“Hello, we are open!”)
else:
print(“Sorry, we are closed!”)
print(“Sincerely, the grocery store”)

A

Hello, we are open!
Sincerely, the grocery store

40
Q

what will the following program print when run?

above_16 = True
has_permit = True
passed_test = False

if above_16 and has_permit and passed_test:
print(“Issue Driver’s License”)
else:
if above_16 or has_permit or passed_test:
print(“Almost eligible for Driver’s License”)
else:
print(“No requirements met.”)

A

Almost eligible for Driver’s License

41
Q

what will the following program print when run?

number_one = 5
number_two = 10

if number_one == 5:
print(1)

if number_one > 5:
print(2)

if number_two < 5:
print(3)

if number_one < number_two:
print(4)

if number_one != number_two:
print(5)

A

1
4
5

42
Q

we want to print the phrase “CodeHS is the best” exactly 25 times. What kind of control structure should we use?

A

for loop

43
Q

what is the output of the following program?

result = 0
max = 5
for i in range(max):
result += i

print(result)

A

10

44
Q

what will be the output when the following code runs?

logged_in = False
print(“User logged in?: “ + str(not logged_in))

A

User logged in?: True

45
Q

determine what the following expression would evaluate to
(9 ≠ 7 AND 17 < 4) OR 65 < 9

A

False

46
Q

which of the following Boolean expressions is equivalent to the expression?
(a AND b) OR (a AND c)

A

a AND (b OR c)

47
Q

in the following code block, assume that the variables rainy and tooCold are boolean. which of the following are equivalent to the above code block?

IF ((NOT rainy) AND (NOT tooCold))
{
DISPLAY(“It’s a good beach day”)
}

A

if (NOT (rainy OR tooCold))
{
DISPLAY(“It’s a good beach day”)
}

48
Q

which of the following returns a random number between 1 and 99?

A

random.randint(1,99)

49
Q

which of the following best describes the behavior of this program?
count ← 0

i ← 1
REPEAT number TIMES
{
IF (i MOD 2 = 1)
{
count ← count + 1
}

i ← i + 1 } DISPLAY count
A

the program correctly displays the count of odd numbers between 1 and number

50
Q

in the procedure Mystery written below, the parameter number is a positive integer. which of the following best describes the result of running the Mystery procedure?

PROCEDURE Mystery (number)
{
value ← number

REPEAT UNTIL (number = 0)
{
    value ← value * -1
    number ← number - 1
}

IF (value > 0)
{
    RETURN (false)
}
ELSE
{
    RETURN (true)
} }
A

the result will be false whenever the initial value of number is even

51
Q

a county 911 system uses an automated computer program to dispatch one of two ambulances. In order to dispatch the ambulance, the ambulance needs to be available and the emergency needs to be in the ambulance’s primary zone or the other ambulance is not available.

The following boolean variables are used:
ambOneAvail - set to true is ambulance one is available, otherwise set to false
ambTwoAvail - set to true is ambulance two is available, otherwise set to false
inZone - Set to true if the emergency is in the primary zone for the ambulance, otherwise set to false

Which of the following Boolean expressions can be used in a selection statement to cause ambulance one to be dispatched?

A

ambOneAvail AND (inZone OR NOT ambTwo Avail)

52
Q

variable

A

something that stores information that can be used later

53
Q

concatenation

A

when you put two strings side by side

54
Q

input function

A

a function that prints a prompt and retrieves text from the user

55
Q

mouse event

A

when a user does something with eh mouse, like clicking or moving

56
Q
A