Incorrect! This code will only print the strings "Zap" or "ABC", not "jane" or "fred". Try again.
Zap ABC Zap
Incorrect! This would be correct if the call was fred() jane() fred(). Try again.
ABC Zap jane
Incorrect! This code will only print the strings "Zap" or "ABC", not "jane" or "fred". Try again.
ABC Zap ABC
Correct! jane() fred() jane() will print ABC Zap ABC.
Zap Zap Zap
Incorrect! This would be correct if the call was fred() fred() fred(). Try again.
2.
What value is printed when the following code is executed?
def myFunction(parameter):
value = 27.4
value = parameter
return value
price = 3.95
print(myFunction(25.25))
value
Incorrect! "value" is the name of a variable. This code will print what is returned in value. Try again.
25.25
Correct! value is assigned to the value of parameter, which is 25.25, so that’s what will get returned and printed.
parameter
Incorrect! parameter is a variable which will be set to the function call’s argument. Try again.
27.4
Incorrect! Although the variable value is set to 27.4 initially, it is then reassigned to be equal to the parameter. Try again.
3.95
Incorrect! The price variable is unused in the program, so it does not affect what is printed. Try again.
3.
A named sequence of statements is known as which of the following?
definition
Incorrect! A definition is used to create functions. Try again.
method
Incorrect! A method is a function that is defined in a class.
turtle
Incorrect! "turtle" is a specific Python module that lets you draw lines and shapes. Try again.
module
Incorrect! A module is a file that contains Python statements and definitions. Try again.
function
Correct! A function is a named sequence of statements in Python.
4.
What will the following code print?
def pow(b, p):
y = b ** p
return y
def square(x):
a = pow(x, 2)
return a
n = 5
result = square(n)
print(result)
5
This would be true if it first printed the value of x.
10
This would be true if it printed 5 * 2 but it is 5 ** 2 which is 5 * 5.
25
It prints the value of 5 raised to the 2nd power which is 5 * 5 = 25.
32
This would be true if it was 2 raised to the 5th power (2 * 2 * 2 * 2 * 2) = 32.
3125
This would be true if it was 5 raised to the 5th power (5 * 5 * 5 * 5 * 5) = 3125.
5.
Consider the following Python code. What does this code print?
def rem(a, b):
return a % b
print(rem(3,7))
0
This would be true if it was rem(3, 3). The value 3 would go into 3 one time with 0 remainder.
3
The value 7 goes into 3 zero times, so the remainder is 3.
7
How many times does 7 go evenly into 3 and what is the remainder?
1
This would be true if it was rem(7,3)
6.
Which of the following would NOT work as a function name?
a
Incorrect! a is a valid (but meaningless) function name. Try again.
return
Correct! You use the return statement in functions, but it can not be the name of a function because it is a reserved word in Python.
length
Incorrect! length would work as a function name because it is not a reserved word or a built-in function. Try again.
x
Incorrect! x is a valid (but meaningless) function name. Try again.
7.
Consider the code below. What prints?
print(int(33.7))
34.0
Incorrect! The int function converts a floating point number to an integer. Try again.
34
Incorrect! The int function does not round up. Try again.
33.70
Incorrect! The int function converts a floating point number to an integer. Try again.
33
Correct! The int function always truncates floating point numbers.
8.
Consider the code below. Line 1 is called…
def print_weather():
print("It is sunny!")
the function header
Correct! The first line of a function definition is the header.
the function body
Incorrect! Everything but the first line of a function definition is the function body. Try again.
the function definition
Incorrect! Lines 1 and 2 together are the function definition. Try again.
the function name
Incorrect! The name is print_weather.
9.
Consider the code block below. What happens when you run this program?
answer = repeat_calculation(3)
print(answer)
def repeat_calculation(n):
result = calculation(n)
result = result + calculation(n)
return result
def calculation(n):
return n * 5
The program prints the number 30.
Incorrect! An error occurs when you call a function before it is defined. Try again.
We get a TypeError.
Incorrect! This will not cause a TypeError because there is not an issue with the variable types. Try again.
We get a NameError.
Correct! You get a NameError when you call a function before it is defined.
The program compiles, but nothing prints.
Incorrect! This program will not compile. Try again.