APCSP Unit 5 CodeHS Flashcards

1
Q

What is the output of the following code?

def quadruple_number(x):
quad_x = 4 * x
print(quad_x)

x = 5
quadruple_number(x)

A

20

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

Why do we write functions?

A

Make our code easier to understand by giving a readable name to a group of instructions
Avoid writing repeated code
Make our code reusable

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

What is the parameter of the function quadruple_number?

def quadruple_number(x):
quad_x = 4 * x
print(quad_x)

A

x

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

What is printed by the following code?

def print_numbers(first, second, third):
print(first)
print(second)
print(third)

middle = 5
print_numbers(4, middle, 6)

A

4
5
6

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

What are the parameters of the print_numbers function?

def print_numbers(first, second, third):
print(first)
print(second)
print(third)

A

first, second, third

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

If we want to draw a circle using our helpful draw_circle function at position (300, 400) with a radius of 40 and color blue, which is the correct function call?

As a reminder, here’s the function header for draw_circle:

def draw_circle(radius, color, x, y)

A

draw_circle(40, Color.blue, 300, 400)

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

Do functions need to have parameters?

A

No, they don’t have to

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

Which of the following functions successfully returns double the variable ‘number’?

def my_function(number):
return number*2

def my_function(number*2):
return

def my_function():
return number

def my_function(number):
number*2

A

def my_function(number):
return number*2

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

Which of the following functions successfully returns the number 10?

def my_function():
10

def my_function(10):
return

def my_function():
return 10

def my_function():
print 10

A

def my_function():
return 10

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

How many parameters go into the function sum?

def sum(first, second):
result = first + second
return result

A

2

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

How many return values come out of the function sum?

def sum(first, second):
result = first + second
return result

A

1

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

In which namespace is the variable ‘x’ defined in the following program?

x = 10

def add_nums():
y = 2
z = x + y
print z

A

Global namespace

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

If added to the program below, which of the following additional functions would not cause an error?

x = 10

def add_nums():
y = 2
z = x + y
print z
_____________________
def subtract_nums():
z = x - y
print z

def subtract_nums():
z = z - x
print z

def subtract_nums():
y = 5
z = x - y
print z

def subtract_nums():
z = y
z = x - y
print

A

def subtract_nums():
y=5
z=x-y
print z

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

Which keywords are relevant to exceptions in Python?

A

try and except

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

What type of error occurs when a programing is expecting an integer value and the user enters a string?

A

ValueError

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

What is the meaning of the x variable in the following code:

size = 20
x = 100
y = 200
ball = Circle(size)
circle.set_position(x, y)
_____________________
The x coordinate of the left side of a box that would hold the circle

The x coordinate of the right edge of the circle

The radius of the circle

The x position of the center circle

A

The x position of the center circle

17
Q

The following program should draw a circle on the screen. But when we run this code, we don’t see the circle. What is missing?

def draw_circle(x, y):
circle = Circle(100)
circle.set_position(x, y)

A

add(circle) command

18
Q

We want to position a Circle on our screen to be at a random position, but we want to keep the entire shape on the screen. Which of the following will accomplish this given the following function? (Assume SIZE, HEIGHT, and WIDTH are properly defined)

def draw_circle(x, y, radius):
circle = Circle(radius)
circle.set_position(x, y)
add(circle)

A

x =random.randint(SIZE,WIDTH-SIZE)
y =random.randint(SIZE,HEIGHT-SIZE)
draw_circle(x, y, SIZE)

19
Q

In the following function print_three_times:

def print_three_times(word):
print(word)
print(word)
print(word)
What is the parameter of the function?

A

word

20
Q

What is the output of the following program?

def sum_to(num):
sum = 0
for i in range(num+1):
sum += i
print(sum)

x = 5
sum_to(x)
print(x)

A

15
5

21
Q

What is printed by the following program?

def print_numbers(two, one, zero):
print(two)
print(one)
print(zero)

zero = 0
one = 1
two = 2
print_numbers(zero, one, two)

A

0
1
2

22
Q

How many parameters go into the function sum, and how many return values come out of the function sum?

def sum(first, second, third):
result = first + second + third
print(first)
print(second)
print(third)
return result

A

3 parameters come in, 1 return value comes out

23
Q

“It’s a bird! It’s a plane! No, it’s Superman!”
We want to write a function is_superman that takes in two parameters is_bird and is_plane and returns True if it is in fact Superman, and False otherwise.
If it’s not a bird and it’s not a plane, it must be Superman.
Which of the following functions is the correct implementation of is_superman?
_____________________
def is_superman(is_bird, is_plane):
return is_bird or is_plane

def is_superman(is_bird, is_plane):
return not is_bird or not is_plane

def is_superman(is_bird, is_plane):
return not is_bird and not is_plane

def is_superman(is_bird, is_plane):
return is_bird and is_plane

A

def is_superman(is_bird, is_plane):
return not is_bird and not is_plane

24
Q

What is printed by the following program?

def product(x, y):
return x * y

def difference(x, y):
return x - y

x = 2
y = 5
value1 = product(x, y)
value2 = difference(y, x)

result = difference(value1, value2)
print(result)

A

7

25
Q

Which variables exist at point A? In other words, which variables are in scope at point A? Which variables could you type at point A and the program would still work? In the following code:

import random

MAX_NUMBER = 10

def sum(x, y):
result = x + y

#Point A

return result

num1 = random.randint(0, MAX_NUMBER)
num2 = random.randint(0, MAX_NUMBER)
print(sum(num1, num2))

A

result, x, y, num1, num2, and MAX_NUMBER

26
Q

Which statement allows us to return values from functions?

A

return

27
Q

Which of the following is true for an API?

I. Allows different devices to connect with each other
II. Allows for Interactivity between devices and software
III. Messenger program that takes requests and delivers the response back to the user
IV. Allows the creation of applications that access the features or data of an operating system, application, or other services
V. Specifies how software components should interact
VI. Used when programming graphical user interface (GUI) components

A

all

28
Q

What does API stand for in computer science?

A

Application Program Interface

29
Q

What is the purpose of a return statement in a function?

A

stops executing the function and returns the value

30
Q

Which of the following is true about return values?

Functions can have return values, but they are optional.

Functions can have more than one return value

Functions must have a return value

Functions need to return the same number of values that were passed in to the function

A

Functions can have return values, but they are optional

31
Q

Consider the following code snippet. What is returned and printed for the value of a?

def mystery(x, y):
if x < y:
return x
else:
return y

a = mystery(10, 14)
b = mystery(82, 28)

print(a)
print(b)

A

10

32
Q

What does the mystery function do in the code snippet below?

def mystery(x, y):
if x < y:
return x
else:
return y

a = mystery(10, 14)
b = mystery(82, 28)

print(a)
print(b)

A

returns the smaller of two values passed to it

33
Q

Consider the following code snippet and what would the following print?
y = mystery2(12)
print(y)
___________________
def mystery1(x):
result = x + 1
return result

def mystery2(x, y):
result = x + y
return result

def mystery3(x):
result = x * x
return result

A

this would generate an error

34
Q

Which of the following functions will correctly return True if it is passed an odd integer value for x?

I.

def is_odd (x) :
return x % 2 == 1
II.

def is_odd (x) :
return x / 2 == 1
III.

def is_odd (x) :
if x % 2 == 1:
return True
else:
return False

A

I. and III.

35
Q

Consider the following code snippet. In the third call to draw_circle(), what values are passed to the coordinates of the center of the circle?

WIDTH = 300
HEIGHT = 300

def draw_circle(radius, color, x, y):
circle = Circle(radius)
circle.set_color(color)
circle.set_position(x, y)
add(circle)

draw_circle(40, “red” 100, 300)
draw_circle(50, “green”, 50, 100)
draw_circle(70, “blue”, WIDTH/2, HEIGHT/2)
draw_circle(25, “yellow”, WIDTH/2, 300)

A

150, 150

36
Q

What is the return value for this function?

def draw_circle(radius, color, x, y):
circle = Circle(radius)
circle.set_color(color)
circle.set_position(x, y)
add(circle)

A

this function has no return value

37
Q

Consider the code snippet:

def print_numbers(first, second, third):
print(first)
print(second)
print(third)

print_numbers(12, 17, 65)
print_numbers(1, 2, 3)
print_numbers(3, 3, 20)
What are names of the parameters?

A

first, second, third