Deck 5: Functions
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/83
Play
Full screen (f)
Deck 5: Functions
1
In Python, program routines are known as functions.
True
2
A function is a named group of instructions performing some task.
True
3
A function may be called one or more times within a program.
True
4
All functions, when they terminate, return to the point in the program from which they were called.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
5
All functions are designed to return a value.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
6
Functions can be designed are useful in more than one program.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
7
The function header of a function definition indicates how the function is to be called.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
8
Functions must contain at least one formal parameter.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
9
Actual arguments are the identifiers used when defining a function in Python.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
10
As long as a function is defined somewhere in a program, it can be called anywhere within the program.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
11
All value-returning functions must have a return statement.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
12
Non-value-returning functions are called for their side effect.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
13
Any function in Python that does not contain a return statement returns special value None.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
14
Calls to value-returning functions in Python are expressions.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
15
Calls to non-value-returning functions cannot be used anywhere that a statement is expected.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
16
In Python, user-defined functions begin with a function header that starts with the keyword,
A) public
B) void
C) function
D) def
E) There are several keywords that can be used
A) public
B) void
C) function
D) def
E) There are several keywords that can be used
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
17
Examine the following Python code:

Which line/lines of code make up the suite (body) of the function?
A) Line 1
B) Line 2
C) Line 3
D) Lines 1 and 2
E) Lines 2 and 3

Which line/lines of code make up the suite (body) of the function?
A) Line 1
B) Line 2
C) Line 3
D) Lines 1 and 2
E) Lines 2 and 3
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
18
Examine the following Python code:

In Line 1, what is (n1, n2, n3) referred to as?
A) Function headers
B) Actual arguments
C) Parameter list
D) Logging variables
E) None of the above

In Line 1, what is (n1, n2, n3) referred to as?
A) Function headers
B) Actual arguments
C) Parameter list
D) Logging variables
E) None of the above
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
19
Examine the following Python code:

When calling this function, how many arguments would be passed to it?
A) 3
B) Zero
C) 1
D) 2
E) Any of the above

When calling this function, how many arguments would be passed to it?
A) 3
B) Zero
C) 1
D) 2
E) Any of the above
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
20
Match the following:
-value passed to a function when called
A) routine
B) function
C) formal parameter
D) actual argument
E) function invocation
-value passed to a function when called
A) routine
B) function
C) formal parameter
D) actual argument
E) function invocation
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
21
Match the following:
-A named group of instructions performing some task
A) routine
B) function
C) formal parameter
D) actual argument
E) function invocation
-A named group of instructions performing some task
A) routine
B) function
C) formal parameter
D) actual argument
E) function invocation
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
22
Match the following:
-A named group of instructions performing some task
A) routine
B) function
C) formal parameter
D) actual argument
E) function invocation
-A named group of instructions performing some task
A) routine
B) function
C) formal parameter
D) actual argument
E) function invocation
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
23
Match the following:
-Python's version of a program routine
A) routine
B) function
C) formal parameter
D) actual argument
E) function invocation
-Python's version of a program routine
A) routine
B) function
C) formal parameter
D) actual argument
E) function invocation
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
24
Match the following:
-an identifier provided in a function header
A) routine
B) function
C) formal parameter
D) actual argument
E) function invocation
-an identifier provided in a function header
A) routine
B) function
C) formal parameter
D) actual argument
E) function invocation
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
25
Match the following:
-called for its returned value
A) function header
B) function body (suite)
C) value-returning function
D) non-value-returning function
E) return statement
-called for its returned value
A) function header
B) function body (suite)
C) value-returning function
D) non-value-returning function
E) return statement
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
26
Match the following:
-contains the function name and parameter list
A) function header
B) function body (suite)
C) value-returning function
D) non-value-returning function
E) return statement
-contains the function name and parameter list
A) function header
B) function body (suite)
C) value-returning function
D) non-value-returning function
E) return statement
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
27
Match the following:
-required in all value-returning functions
A) function header
B) function body (suite)
C) value-returning function
D) non-value-returning function
E) return statement
-required in all value-returning functions
A) function header
B) function body (suite)
C) value-returning function
D) non-value-returning function
E) return statement
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
28
Match the following:
-called for its side effects
A) function header
B) function body (suite)
C) value-returning function
D) non-value-returning function
E) return statement
-called for its side effects
A) function header
B) function body (suite)
C) value-returning function
D) non-value-returning function
E) return statement
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
29
Match the following:
-contains the functions instructions
A) function header
B) function body (suite)
C) value-returning function
D) non-value-returning function
E) return statement
-contains the functions instructions
A) function header
B) function body (suite)
C) value-returning function
D) non-value-returning function
E) return statement
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
30
A __________ is a named group of instructions performing some task.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
31
_____________ is an example of a built-in function in Python.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
32
A function is said to be ___________ when it is called.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
33
The identifiers in a function meant to receive the arguments passed to it are called _________________________________.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
34
The values passed to a function when called are referred to as ___________________.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
35
A __________________________ function is called for its returned value.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
36
A __________________________ function is called for its side effects.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
37
Write the function header only of a function named largest that takes three integer parameters and returns the largest of the three.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
38
Using function largest in the previous problems, write Python code that gives an example of its use.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
39
Examine the following Python code:
def displayFunction():
print('This program will convert between Fahrenheit and Celsius')
print('Enter (C) to convert to Celsius from Fahrenheit')
print('Enter (F) to convert to Fahrenheit to Celsius')
Write a line of Python code that properly calls function displayFunction.
def displayFunction():
print('This program will convert between Fahrenheit and Celsius')
print('Enter (C) to convert to Celsius from Fahrenheit')
print('Enter (F) to convert to Fahrenheit to Celsius')
Write a line of Python code that properly calls function displayFunction.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
40
Function calls to value-returning functions can be used anywhere that a function's return value is appropriate.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
41
There is a fundamental difference in the way value-returning and non-value-returning functions are called.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
42
There can be only one function call in a given expression.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
43
The actual arguments within a function call may also be function calls.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
44
All functions in Python return a numeric value.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
45
Function calls may not be used within a print instruction in Python.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
46
Value-returning functions can return more than one value by returning a single tuple containing multiple values.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
47
Tuple assignment is a means of copying the values of one tuple to another.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
48
In function calls to functions that do not have any arguments, a set of empty parentheses can be optionally provided.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
49
The correspondence of actual and formal parameters is determined by the names of the actual arguments passed.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
50
Only arguments that are a mutable types can be altered when passed to a function.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
51
Whether an argument is mutable or immutable in Python is determined by its type.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
52
Keyword arguments are different from positional arguments, in that their name must be the same as the name of the corresponding parameter when calling a function.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
53
There can only be one keyword argument used in a given function call.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
54
All positional arguments must come before any default arguments in a function definition.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
55
A local variable can only be accessed from within the function it is defined in.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
56
Each function in Python has its own local scope.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
57
The lifetime of local variables is the same as global variables, except that they are only accessible from within the function they are defined in.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
58
It is generally considered good practice to use global variables whenever possible.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
59
Which of the following is not true?
A) An expression may contain multiple function calls
B) Conditional expressions may contain function calls
C) A function call may appear on the left side of an assignment statement
D) Arguments in a print instruction may contain function calls
E) A function call may contain function calls as arguments
A) An expression may contain multiple function calls
B) Conditional expressions may contain function calls
C) A function call may appear on the left side of an assignment statement
D) Arguments in a print instruction may contain function calls
E) A function call may contain function calls as arguments
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
60
Which of the following is not a valid use of function avg?
A) average = avg(num1, num2, num3)
B) result = factor * avg(num1, num2, num3)
C) avg(num1, num2, num3) = (num1 + num2 + num3) / 3
D) print('The average of num1, num2 and num3 is', avg(num1, num2, num3))
E) if avg(num1, num2, num3) > 100:
Print('Average above 100 found')
A) average = avg(num1, num2, num3)
B) result = factor * avg(num1, num2, num3)
C) avg(num1, num2, num3) = (num1 + num2 + num3) / 3
D) print('The average of num1, num2 and num3 is', avg(num1, num2, num3))
E) if avg(num1, num2, num3) > 100:
Print('Average above 100 found')
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
61
Which of the following is not a valid use of function maxmin?
A) result = maxmin([20, 5, 30, 45, 60, 15])
B) result = maxmin([max(num_list1), max(num_list2)])
C) result = maxmin([max(num_list1), max(num_list2), max(num_list3)])
D) largest, smallest = maxmin(num_list)
E) if maxmin(num_list) > 100:
Print('Largest value above 100')
A) result = maxmin([20, 5, 30, 45, 60, 15])
B) result = maxmin([max(num_list1), max(num_list2)])
C) result = maxmin([max(num_list1), max(num_list2), max(num_list3)])
D) largest, smallest = maxmin(num_list)
E) if maxmin(num_list) > 100:
Print('Largest value above 100')
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
62
Examine the following Python code:
What will the last line print out?
A) [5, -2, 9, 4, -6, 1]
B) [5, 0, 9, 4, 0, 1]
C) nums_1
D) [19]
E) None of the above
![<strong>Examine the following Python code: What will the last line print out?</strong> A) [5, -2, 9, 4, -6, 1] B) [5, 0, 9, 4, 0, 1] C) nums_1 D) [19] E) None of the above](https://d2lvgg3v3hfg70.cloudfront.net/TB10282/11ee8f4f_7694_de27_8f58_cdad9d6658bd_TB10282_11.jpg)
What will the last line print out?
A) [5, -2, 9, 4, -6, 1]
B) [5, 0, 9, 4, 0, 1]
C) nums_1
D) [19]
E) None of the above
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
63
Examine the following Python code:

What will Line 6 print out?
A) 5
B) 1
C) 15
D) 9
E) 10

What will Line 6 print out?
A) 5
B) 1
C) 15
D) 9
E) 10
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
64
Examine the following Python code:

What will Line 9 print out?
A) 0
B) 80
C) 85
D) 5
E) 40

What will Line 9 print out?
A) 0
B) 80
C) 85
D) 5
E) 40
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
65
Examine the following function header:
Def mortgagePayment(amount, rate, term):
Which of the following calls to function mortgagePayment is invalid in Python?
A) mortgagePayment(350000, 5.5, 30)
B) mortgagePayment(amount=350000, rate=5.5, term=30)
C) mortgagePayment(350000, rate=5.5, term=30)
D) mortgagePayment(350000, term=30, rate=5.5)
E) mortgagePayment(rate=5.5, term=30, 350000)
Def mortgagePayment(amount, rate, term):
Which of the following calls to function mortgagePayment is invalid in Python?
A) mortgagePayment(350000, 5.5, 30)
B) mortgagePayment(amount=350000, rate=5.5, term=30)
C) mortgagePayment(350000, rate=5.5, term=30)
D) mortgagePayment(350000, term=30, rate=5.5)
E) mortgagePayment(rate=5.5, term=30, 350000)
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
66
Examine the following function header:
Def mortgagePayment(amount, rate, term=30):
Which of the following calls to function mortgagePayment is invalid in Python?
A) mortgagePayment(350000, 5.5, 30)
B) mortgagePayment(amount=350000, rate=5.5, term=30)
C) mortgagePayment(350000, rate=5.5, term=30)
D) mortgagePayment(rate=5.5, term=30)
E) mortgagePayment(amount=350000, rate=5.5)
Def mortgagePayment(amount, rate, term=30):
Which of the following calls to function mortgagePayment is invalid in Python?
A) mortgagePayment(350000, 5.5, 30)
B) mortgagePayment(amount=350000, rate=5.5, term=30)
C) mortgagePayment(350000, rate=5.5, term=30)
D) mortgagePayment(rate=5.5, term=30)
E) mortgagePayment(amount=350000, rate=5.5)
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
67
Which of the following is not true about the scope and lifetime of variables in Python:
A) local variables have local scope
B) global variables have global scope
C) local variables have a longer lifetime than global variables
D) the same identifier name can be used for different local variables of other functions
E) The use of global variables is not good programming practice
A) local variables have local scope
B) global variables have global scope
C) local variables have a longer lifetime than global variables
D) the same identifier name can be used for different local variables of other functions
E) The use of global variables is not good programming practice
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
68
A value-returning function may return more than one value by returning all the values as a __________.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
69
The assignment of multiple variables in the same assignment statement in Python involving a call to a value-returning function is called ______________________________.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
70
The correspondence of actual argument and formal parameters in a function call is determined by the __________ of the arguments.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
71
Arguments of type list in a function call can be altered as a result the call. Such arguments are said to be
___________________.
___________________.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
72
________________ arguments correspond with the associated parameters in a function call by their position in the arguments passed.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
73
________________ arguments correspond with the associated parameters in a function call by their their name.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
74
A ___________ argument is one that can be optionally provided.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
75
Variables defined within a function have ___________ scope.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
76
Variables defined outside of any function have ___________ scope.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
77
The period of time for which a variable exists is called its ___________.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
78
For a function that takes no arguments, parentheses must still be used when calling it it. Why is this?
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
79
Examine the following Python code:

The formal parameters in the function definition are ordered num1, num2, but in the function call,
the actual arguments are ordered num2, num1. This is neither a syntactic nor a semantic error. Explain why.

The formal parameters in the function definition are ordered num1, num2, but in the function call,
the actual arguments are ordered num2, num1. This is neither a syntactic nor a semantic error. Explain why.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck
80
Examine the following Python code:

What will the last line print out? Explain how you got this answer.

What will the last line print out? Explain how you got this answer.
Unlock Deck
Unlock for access to all 83 flashcards in this deck.
Unlock Deck
k this deck