5.14. Multiple Choice Questions¶
- a, b
- It will print the value of a and b.
- -3, b
- It will print -3 and the value of b.
- a, 5
- It will print the value of a and 5.
- -3, 5
- It looks like it will print this, but it will actually print the values with a space between them.
- -3 5
- It prints the values of a and b with a space between them. The default value of b is 5.
Q-1: What will be printed when the following code executes?
def test(a, b = 5):
print(a, b)
test(-3)
- Zap ABC jane fred jane
- 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.
Q-2: What will the following Python program print out? (Given that each word will actually print on a new line)
def fred():
print("Zap")
def jane():
print("ABC")
jane()
fred()
jane()
- value
- Incorrect! "value" is the name of a variable. This code will print what "value" is assigned to. Try again.
- Second
- Correct! "value" is assigned to the value of parameter which is "Second", so that's what will print.
- parameter
- Incorrect! "parameter" is equal to the function call's argument. Try again.
- First
- Incorrect! Although the variable value is assigned to "First" initially, it is then reassigned to be equal to the parameter. Try again.
- Jane Doe
- Incorrect! The "name" variable is unused in the function call, so it does not affect what is printed. Try again.
Q-3: What value is printed when the following code is executed?
name = "Jane Doe"
def myFunction(parameter):
value = "First"
value = parameter
print (value)
myFunction("Second")
- 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.
Q-4: A named sequence of statements is known as which of the following?
- 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 ture if it was 5 raised to the 5th power (5 * 5 * 5 * 5 * 5) = 3125.
Q-5: What will the following code print?
1def pow(b, p):
2 y = b ** p
3 return y
4
5def square(x):
6 a = pow(x, 2)
7 return a
8
9n = 5
10result = square(n)
11print(result)
- 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)
Q-6: Consider the following Python code. What does this code print?
1def rem(a, b):
2 return a % b
3
4print(rem(3,7))
- a
- Incorrect! a is a valid variable name. Try again.
- len
- Correct! This would not work as a variable name because it is already reserved as a built-in function name.
- length
- Incorrect! length would work as a variable because it is not a reserved word or a built-in function. Try again.
- x
- Incorrect! x is a valid variable name. Try again.
Q-7: Which of the following would NOT work as a variable name?
- 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.
Q-8: Consider the code below. What prints?
1print(int(33.7))
- 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 collectively are the function definition. Try again.
- the function named
- Incorrect! The name is printWeather.
Q-9: Consider the code below. Line 1 is called…
1def printWeather():
2 print("It is sunny!")
- The lyrics print like normal.
- 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.
Q-10: Consider the code block below. What happens when you run this program?
repeat_lyrics()
def repeat_lyrics():
print_lyrics()
print_lyrics()
def print_lyrics():
print("I'm a lumberjack, and I'm okay.")
print('I sleep all night and I work all day.')
You have attempted of activities on this page