Font size
WorksheetsChapter 12 Exam Review
Total questions: 20
Worksheet time: 10mins
How do you identify the statements that belong to the body of a function?
Indent those statement the same amount underneath the function "def" statement
Add the "def" keyword to the beginning of each statement
Add opening and closing curly braces { and } to mark the start and stop of the function body
Add the same comment (#) to the right of each statement within the body
What character should be placed at the end of the function "def" line, after the parentheses?
Hashtag (#)
Colon (:)
Exclamation (!)
Dash (-)
Which keyword marks a spot in a function where the program flow will go back to the calling code?
continue
jump
return
break
If a function named "mystery" is defined as taking no parameters, how would you call that function from your code?
mystery
mystery()
# mystery
mystery[ ]
Which of the following best describes where you should define your function in your code?
Near the top of the source file, before that function is called by any main program code
Near the bottom of the source file, after that function is called by any main program code
Always place functions in a separate source file
The location is not important, so place the function wherever you like
Given the code below, which printed output will appear first when the code runs?
BOO
I'm not afraid of ghosts
Syntax error - haunted() is not a valid function name
Runtime exception - the haunted() function is called with incorrect parameters
Which of the following is NOT a Python rule for naming functions?
Function names must be less than 32 characters
Function names can contain letters, numbers and underscores
The first character in a function name cannot be a number
Function names are case-sensitive
Which of the following statements correctly defines a function named "haunted" that takes a parameter named "fear"?
def haunted:fear
function haunted[fear]:
def haunted(fear):
haunted("fear")
Given the "haunted" function defined below, which of the following statements will correctly call the function and set the "fear" parameter equal to 10 and the "surprise" parameter equal to 5?
haunted(10,5)
haunted[10:5]
haunted(5,10)
haunted(range(10,5))
Which of the following definitions for the "haunted" function will correctly set the default value for the "surprise" parameter equal to the number 1?
def haunted(fear,=1):
def haunted(fear, surprise "1")
haunted(fear, surprise) = 1
def haunted(fear, surprise=1):
Which of the following function definitions allows values for the two parameters to be specified by names as shown below? haunt(surprise=5, fear=10)
def haunt(fear, surprise):
def haunt("fear","surprise");
def haunt([fear], [surprise])
haunt(surprise, fear)
Which of the following statements will successfully send the value "EEK" back from a function to the calling code?
final "EEK"
return "EEK"
"EEK".return()
result["EEK"]
If given the "haunt" function defined below, which of the following statements is valid?
def haunt(fear, surprise):
return fear * surprise
haunt(1,2)
result = haunt(1,2)
print( str.format("Your scare result is {}",haunt(1,2)) )
All of these are valid
How many return statements can you place inside one function body?
As many as needed; each marks a spot where program flow returns to the calling code
Exactly one
You are limited by the number of body statements, divided by 2
You can have one return statement per parameter that is defined by the function "def"
What controls a variable's scope?
The length of your variable name
The comment you place to the right of the variable name
The capitalization you select for the letters in your function name
The location where it is first initialized within your code
Which of the following rules is true about globally scoped variables?
Global variables are valid as long as your program is running
Global variables can only be accessed from within the function where they were defined
Global variables will be destroyed after each function ends
After you initialize a global variable, you cannot change the value later
Which of the following rules is true about locally scoped variables?
Local variables can only be seen and used from inside a function
Local variables will keep their same value over repeated calls to the function
Local variables can be seen and accessed from any part of the program code
Local variables can hold less data than global variables
Given the code, which answer correctly matches the variable name and the scope type?
name is global, attitude is local
attitude is global, name is local
name and attitude are both local
name and attitude are both global
What will be printed to the screen when the code runs?
Creeper
Casper
ghost
CasperCreeper
What will be printed to the screen when the code runs?
Phantastic
ghost
Slimer
name
