APCSP Unit 5 CodeHS Flashcards
What is the output of the following code?
def quadruple_number(x):
quad_x = 4 * x
print(quad_x)
x = 5
quadruple_number(x)
20
Why do we write functions?
Make our code easier to understand by giving a readable name to a group of instructions
Avoid writing repeated code
Make our code reusable
What is the parameter of the function quadruple_number?
def quadruple_number(x):
quad_x = 4 * x
print(quad_x)
x
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)
4
5
6
What are the parameters of the print_numbers function?
def print_numbers(first, second, third):
print(first)
print(second)
print(third)
first, second, third
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)
draw_circle(40, Color.blue, 300, 400)
Do functions need to have parameters?
No, they don’t have to
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
def my_function(number):
return number*2
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
def my_function():
return 10
How many parameters go into the function sum?
def sum(first, second):
result = first + second
return result
2
How many return values come out of the function sum?
def sum(first, second):
result = first + second
return result
1
In which namespace is the variable ‘x’ defined in the following program?
x = 10
def add_nums():
y = 2
z = x + y
print z
Global namespace
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
def subtract_nums():
y=5
z=x-y
print z
Which keywords are relevant to exceptions in Python?
try and except
What type of error occurs when a programing is expecting an integer value and the user enters a string?
ValueError
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
The x position of the center circle
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)
add(circle) command
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)
x =random.randint(SIZE,WIDTH-SIZE)
y =random.randint(SIZE,HEIGHT-SIZE)
draw_circle(x, y, SIZE)
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?
word
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)
15
5
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)
0
1
2
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
3 parameters come in, 1 return value comes out
“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
def is_superman(is_bird, is_plane):
return not is_bird and not is_plane
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)
7