NEW
Font size
WorksheetsPython_Ch4_All
Total questions: 59
Worksheet time: 30mins
Which of the following is a way to define and use functions in your programs?
Using default values
Using named arguments
Using local variables
Using global variables
Which of the following is a step in using your own modules?
Create the module
Document the module
Import the module
All of the above
Which module should be imported to use random functionalities in Python?
math
random
os
sys
Use a hierarchy chart or outline to plan the functions of a program.
To organize the program structure
To execute the program
To debug the program
To compile the program
In general terms, describe how to define a function, including the use of a return statement.
A function is defined using a specific syntax and may include a return statement to output a value.
A function is defined by listing all variables and does not use a return statement.
A function is defined by writing a paragraph describing its purpose.
A function is defined by using a loop and a return statement is optional.
In general terms, how do you call a function?
By specifying the function name followed by parentheses.
By writing the function definition again.
By using a loop to iterate over the function.
By declaring the function as a variable.
In general terms, describe how to define and call a main() function.
Define the main() function using the 'def' keyword and call it using 'main()'.
Use the 'function' keyword to define main() and call it with 'execute main'.
Declare main() with 'start' and invoke it with 'run main'.
Define main() with 'create' and call it using 'invoke main'.
Describe the use of default values in a function definition and in the statements that call the function.
Default values allow functions to be called with fewer arguments than defined.
Default values are mandatory for all function parameters.
Default values can only be used in the function call, not in the definition.
Default values must be the same for all function calls.
Describe the use of named arguments in calling statements.
Named arguments allow you to specify the name of the parameter in the function call.
Named arguments are used to define the function itself.
Named arguments are only used in anonymous functions.
Named arguments are a type of loop structure.
Which of the following best describes the recommended use of global variables, local variables, and global constants?
Global variables should be used extensively for all data storage.
Local variables should be used within functions to limit scope.
Global constants should be frequently changed to adapt to new requirements.
Local variables should be avoided to ensure data is accessible everywhere.
In general terms, which of the following steps are involved in creating and documenting a module?
Define the module's purpose and scope.
Write the module code and test it.
Document the module's functionality and usage.
All of the above.
Distinguish among importing a module into the default namespace, a specified namespace, and the global namespace.
Importing into the default namespace makes the module available without any prefix.
Importing into a specified namespace requires using a prefix to access the module.
Importing into the global namespace makes the module available globally without any prefix.
All of the above are correct distinctions.
Which Python standard module can be used to generate random numbers?
os
sys
random
math
What problem can occur if you import a module into the global namespace?
It can lead to name conflicts.
It improves code readability.
It enhances module security.
It reduces memory usage.
Using a hierarchy chart or outline is useful for planning the functions of a program because it:
Helps visualize the structure and flow of the program
Increases the execution speed of the program
Reduces the number of lines of code needed
Ensures the program is free of bugs
Select the correct syntax for defining a function in Python.
def function_name(parameters):
function function_name(parameters):
define function_name(parameters):
function_name(parameters) ->
What is the purpose of the 'print_welcome()' function in the given Python code?
To print a welcome message to the user
To initialize the program
To handle user input
To terminate the program
Fill in the blank: The function 'print_welcome()' is defined to print 'Welcome to the Future Value Calculator'.
The function 'print_welcome()' is defined to print 'Welcome to the Future Value Calculator'.
The function 'print_welcome()' is defined to print 'Hello, World!'.
The function 'print_welcome()' is defined to print 'Goodbye!'.
The function 'print_welcome()' is defined to print 'Welcome to the Past Value Calculator'.
What is the correct way to define a function in Python that takes one argument?
A) def function_name():
B) def function_name(arg1, arg2):
C) def function_name(arg1):
How do you call a function in Python that has one argument, using the example provided in the image?
function_name(argument)
call function_name with argument
execute function_name with argument
run function_name(argument)
What is the correct way to define a function in Python that calculates miles per gallon?
def miles_per_gallon(miles, gallons): return miles / gallons
function miles_per_gallon(miles, gallons) { return miles / gallons; }
miles_per_gallon = lambda miles, gallons: miles / gallons
def miles_per_gallon(miles, gallons): print(miles / gallons)
Fill in the blank: To calculate miles per gallon, the function divides ______ by gallons.
miles
liters
kilometers
hours
What is the result of calling the function calculate_miles_per_gallon with miles = 500 and gallons = 14?
35.71
36.00
34.50
35.00
What is the purpose of the 'main()' function in the given Python code?
To define the entry point of the program
To import necessary libraries
To handle exceptions
To declare global variables
What input does the 'main()' function ask the user to provide?
User's name
User's age
User's email address
User's phone number
Which function is called within the 'main()' function to calculate the future value?
calculateFutureValue()
computeValue()
findFutureValue()
determineValue()
What is the output of the 'main()' function in the given Python code?
The function returns 0
The function prints 'Hello, World!'
The function raises an error
The function does not produce any output
What is one way to call a main() function in Python that is not recommended?
Using a simple call statement
Using a call statement within an if statement
Using a loop
Using a class
What is the recommended way to call a main() function in Python?
Call it directly without any condition.
Use the if __name__ == '__main__': construct.
Use a try-except block to call it.
Call it from another module.
What is the monthly investment amount entered in the Future Value program?
100
200
300
400
Fill in the blank: The yearly interest rate entered in the Future Value program is ____.
12
10
15
8
What is the future value calculated by the program?
23233.91
20000.00
25000.00
30000.00
What is the purpose of the 'calculate_future_value' function in the given Python code?
To calculate the present value of an investment
To determine the future value of an investment
To compute the interest rate of an investment
To find the duration of an investment
In the given Python code, what does the variable 'monthly_interest_rate' represent?
The annual interest rate divided by 12
The total interest for the year
The interest rate for a single day
The principal amount
How is the 'future_value' calculated in the given Python code?
By multiplying the present value with the interest rate
By adding the present value to the interest
By dividing the present value by the interest rate
By subtracting the interest from the present value
What is the return value of the 'calculate_future_value' function in the given Python code?
The future value of an investment
The present value of an investment
The interest rate used in the calculation
The time period for the investment
What is the purpose of the 'choice' variable in the Future Value program?
To store the user's selection of investment type
To calculate the future value of an investment
To determine the interest rate applied
To track the number of investment periods
In the Future Value program, what function is used to calculate the future value?
calculateFutureValue()
computeValue()
futureValueCalc()
getFutureValue()
What input does the user need to provide in the Future Value program?
Initial investment amount, interest rate, and time period
Only the interest rate
Only the time period
Only the initial investment amount
What does the following line of code do?
It assigns the user's input to the variable 'choice'.
It prints 'Continue? (y/n): ' to the console.
It checks if the user wants to continue.
It terminates the program if the user inputs 'n'.
What will be printed if the user inputs 'n' when prompted with "Continue? (y/n): "?
The program will terminate.
The program will continue.
An error message will be displayed.
The input will be ignored.
What is the purpose of the following code snippet? if __name__ == "__main__": main()
To execute the main function when the script is run directly.
To define a function named main.
To import the main module.
To check if the main function exists.
In the code snippet, what will happen if the user inputs 'y' when prompted?
The program will terminate.
The program will continue.
An error will occur.
The program will restart.
What is the default value of the 'years' parameter in the function 'calculate_future_value'?
10
20
30
40
What is the purpose of the 'calculate_future_value' function in Python?
To calculate the present value of an investment
To determine the future value of an investment
To compute the interest rate of a loan
To find the depreciation value of an asset
In the function 'calculate_future_value', what does the variable 'monthly_interest_rate' represent?
The annual interest rate divided by 12
The interest rate for a single month
The total interest rate over the investment period
The interest rate compounded annually
Explain how to call a function in Python and use its default value using the example provided: future_value = calculate_future_value(100, 8.5).
By passing all required arguments without specifying default values.
By omitting the argument for which the default value is set.
By specifying all arguments including those with default values.
By using a different function that does not have default values.
Explain how to call a function in Python and override its default value using the example provided: future_value = calculate_future_value(100, 8.5, 10).
By passing arguments in the function call
By modifying the function definition
By using global variables
By using a decorator
How do you specify a default value for an argument in a function definition?
By assigning a value to the argument in the function call
By assigning a value to the argument in the function definition
By using a global variable
By using a return statement
In a function definition, where must the arguments with default values be coded?
First
Last
Anywhere
In the middle
Explain how to call a function with named arguments using the given example: future_value = calculate_future_value(years=10, monthly_investment=100, yearly_interest=8.5).
By specifying the argument names followed by their values.
By listing the values without specifying argument names.
By using a dictionary to pass the arguments.
By using a list to pass the arguments.
What is a named argument in Python?
An argument passed to a function with a specific name.
An argument that is always optional.
An argument that can only be used in built-in functions.
An argument that is always required.
When calling a function without named arguments, how must the arguments be coded?
In the order they are defined in the function
In any order
Using keyword arguments
Using default values
True or False: When using named arguments, you must code the arguments in the sequence that they’re coded in the function definition.
True
False
Why is it a good practice to use named arguments for functions with many arguments?
It improves code readability.
It reduces the function's execution time.
It allows for dynamic typing.
It increases memory usage.
What is the purpose of the 'calc_tax' function in the given code snippet?
To calculate the total price including tax
To apply a discount to the price
To determine the tax rate
To format the price for display
In the given code, what is the value of 'tax' when 'calc_tax' is called with arguments 85.0 and 0.05?
4.25
3.50
5.00
4.00
Which of the following statements is true about the 'tax' variable in the given code snippet?
'tax' is a global variable.
'tax' is a local variable.
'tax' is a constant.
'tax' is an argument.
Explain how the global variable 'tax' is used in the given Python code snippet. What is the final value of 'tax' after the execution of the main function?
The global variable 'tax' is modified inside the main function and its final value is 15.
The global variable 'tax' is not modified inside the main function and its final value remains 10.
The global variable 'tax' is modified inside the main function and its final value is 20.
The global variable 'tax' is modified inside the main function and its final value is 25.
