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
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))
result, x, y, num1, num2, and MAX_NUMBER
Which statement allows us to return values from functions?
return
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
all
What does API stand for in computer science?
Application Program Interface
What is the purpose of a return statement in a function?
stops executing the function and returns the value
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
Functions can have return values, but they are optional
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)
10
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)
returns the smaller of two values passed to it
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
this would generate an error
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
I. and III.
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)
150, 150
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)
this function has no return value
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?
first, second, third