wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python_Ch4_All

Total questions: 59

Worksheet time: 30mins

Name
Class
Date
1.

Which of the following is a way to define and use functions in your programs?

a)

Using default values

b)

Using named arguments

c)

Using local variables

d)

Using global variables

2.

Which of the following is a step in using your own modules?

a)

Create the module

b)

Document the module

c)

Import the module

d)

All of the above

3.

Which module should be imported to use random functionalities in Python?

a)

math

b)

random

c)

os

d)

sys

4.

Use a hierarchy chart or outline to plan the functions of a program.

a)

To organize the program structure

b)

To execute the program

c)

To debug the program

d)

To compile the program

5.

In general terms, describe how to define a function, including the use of a return statement.

a)

A function is defined using a specific syntax and may include a return statement to output a value.

b)

A function is defined by listing all variables and does not use a return statement.

c)

A function is defined by writing a paragraph describing its purpose.

d)

A function is defined by using a loop and a return statement is optional.

6.

In general terms, how do you call a function?

a)

By specifying the function name followed by parentheses.

b)

By writing the function definition again.

c)

By using a loop to iterate over the function.

d)

By declaring the function as a variable.

7.

In general terms, describe how to define and call a main() function.

a)

Define the main() function using the 'def' keyword and call it using 'main()'.

b)

Use the 'function' keyword to define main() and call it with 'execute main'.

c)

Declare main() with 'start' and invoke it with 'run main'.

d)

Define main() with 'create' and call it using 'invoke main'.

8.

Describe the use of default values in a function definition and in the statements that call the function.

a)

Default values allow functions to be called with fewer arguments than defined.

b)

Default values are mandatory for all function parameters.

c)

Default values can only be used in the function call, not in the definition.

d)

Default values must be the same for all function calls.

9.

Describe the use of named arguments in calling statements.

a)

Named arguments allow you to specify the name of the parameter in the function call.

b)

Named arguments are used to define the function itself.

c)

Named arguments are only used in anonymous functions.

d)

Named arguments are a type of loop structure.

10.

Which of the following best describes the recommended use of global variables, local variables, and global constants?

a)

Global variables should be used extensively for all data storage.

b)

Local variables should be used within functions to limit scope.

c)

Global constants should be frequently changed to adapt to new requirements.

d)

Local variables should be avoided to ensure data is accessible everywhere.

11.

In general terms, which of the following steps are involved in creating and documenting a module?

a)

Define the module's purpose and scope.

b)

Write the module code and test it.

c)

Document the module's functionality and usage.

d)

All of the above.

12.

Distinguish among importing a module into the default namespace, a specified namespace, and the global namespace.

a)

Importing into the default namespace makes the module available without any prefix.

b)

Importing into a specified namespace requires using a prefix to access the module.

c)

Importing into the global namespace makes the module available globally without any prefix.

d)

All of the above are correct distinctions.

13.

Which Python standard module can be used to generate random numbers?

a)

os

b)

sys

c)

random

d)

math

14.

What problem can occur if you import a module into the global namespace?

a)

It can lead to name conflicts.

b)

It improves code readability.

c)

It enhances module security.

d)

It reduces memory usage.

15.

Using a hierarchy chart or outline is useful for planning the functions of a program because it:

a)

Helps visualize the structure and flow of the program

b)

Increases the execution speed of the program

c)

Reduces the number of lines of code needed

d)

Ensures the program is free of bugs

16.

Select the correct syntax for defining a function in Python.

a)

def function_name(parameters):

b)

function function_name(parameters):

c)

define function_name(parameters):

d)

function_name(parameters) ->

17.

What is the purpose of the 'print_welcome()' function in the given Python code?

a)

To print a welcome message to the user

b)

To initialize the program

c)

To handle user input

d)

To terminate the program

18.

Fill in the blank: The function 'print_welcome()' is defined to print 'Welcome to the Future Value Calculator'.

a)

The function 'print_welcome()' is defined to print 'Welcome to the Future Value Calculator'.

b)

The function 'print_welcome()' is defined to print 'Hello, World!'.

c)

The function 'print_welcome()' is defined to print 'Goodbye!'.

d)

The function 'print_welcome()' is defined to print 'Welcome to the Past Value Calculator'.

19.

What is the correct way to define a function in Python that takes one argument?

a)

A) def function_name():

b)

B) def function_name(arg1, arg2):

c)

C) def function_name(arg1):

20.

How do you call a function in Python that has one argument, using the example provided in the image?

a)

function_name(argument)

b)

call function_name with argument

c)

execute function_name with argument

d)

run function_name(argument)

21.

What is the correct way to define a function in Python that calculates miles per gallon?

a)

def miles_per_gallon(miles, gallons): return miles / gallons

b)

function miles_per_gallon(miles, gallons) { return miles / gallons; }

c)

miles_per_gallon = lambda miles, gallons: miles / gallons

d)

def miles_per_gallon(miles, gallons): print(miles / gallons)

22.

Fill in the blank: To calculate miles per gallon, the function divides ______ by gallons.

a)

miles

b)

liters

c)

kilometers

d)

hours

23.

What is the result of calling the function calculate_miles_per_gallon with miles = 500 and gallons = 14?

a)

35.71

b)

36.00

c)

34.50

d)

35.00

24.

What is the purpose of the 'main()' function in the given Python code?

a)

To define the entry point of the program

b)

To import necessary libraries

c)

To handle exceptions

d)

To declare global variables

25.

What input does the 'main()' function ask the user to provide?

a)

User's name

b)

User's age

c)

User's email address

d)

User's phone number

26.

Which function is called within the 'main()' function to calculate the future value?

a)

calculateFutureValue()

b)

computeValue()

c)

findFutureValue()

d)

determineValue()

27.

What is the output of the 'main()' function in the given Python code?

a)

The function returns 0

b)

The function prints 'Hello, World!'

c)

The function raises an error

d)

The function does not produce any output

28.

What is one way to call a main() function in Python that is not recommended?

a)

Using a simple call statement

b)

Using a call statement within an if statement

c)

Using a loop

d)

Using a class

29.

What is the recommended way to call a main() function in Python?

a)

Call it directly without any condition.

b)

Use the if __name__ == '__main__': construct.

c)

Use a try-except block to call it.

d)

Call it from another module.

30.

What is the monthly investment amount entered in the Future Value program?

a)

100

b)

200

c)

300

d)

400

31.

Fill in the blank: The yearly interest rate entered in the Future Value program is ____.

a)

12

b)

10

c)

15

d)

8

32.

What is the future value calculated by the program?

a)

23233.91

b)

20000.00

c)

25000.00

d)

30000.00

33.

What is the purpose of the 'calculate_future_value' function in the given Python code?

a)

To calculate the present value of an investment

b)

To determine the future value of an investment

c)

To compute the interest rate of an investment

d)

To find the duration of an investment

34.

In the given Python code, what does the variable 'monthly_interest_rate' represent?

a)

The annual interest rate divided by 12

b)

The total interest for the year

c)

The interest rate for a single day

d)

The principal amount

35.

How is the 'future_value' calculated in the given Python code?

a)

By multiplying the present value with the interest rate

b)

By adding the present value to the interest

c)

By dividing the present value by the interest rate

d)

By subtracting the interest from the present value

36.

What is the return value of the 'calculate_future_value' function in the given Python code?

a)

The future value of an investment

b)

The present value of an investment

c)

The interest rate used in the calculation

d)

The time period for the investment

37.

What is the purpose of the 'choice' variable in the Future Value program?

a)

To store the user's selection of investment type

b)

To calculate the future value of an investment

c)

To determine the interest rate applied

d)

To track the number of investment periods

38.

In the Future Value program, what function is used to calculate the future value?

a)

calculateFutureValue()

b)

computeValue()

c)

futureValueCalc()

d)

getFutureValue()

39.

What input does the user need to provide in the Future Value program?

a)

Initial investment amount, interest rate, and time period

b)

Only the interest rate

c)

Only the time period

d)

Only the initial investment amount

40.

What does the following line of code do?

a)

It assigns the user's input to the variable 'choice'.

b)

It prints 'Continue? (y/n): ' to the console.

c)

It checks if the user wants to continue.

d)

It terminates the program if the user inputs 'n'.

41.

What will be printed if the user inputs 'n' when prompted with "Continue? (y/n): "?

a)

The program will terminate.

b)

The program will continue.

c)

An error message will be displayed.

d)

The input will be ignored.

42.

What is the purpose of the following code snippet? if __name__ == "__main__": main()

a)

To execute the main function when the script is run directly.

b)

To define a function named main.

c)

To import the main module.

d)

To check if the main function exists.

43.

In the code snippet, what will happen if the user inputs 'y' when prompted?

a)

The program will terminate.

b)

The program will continue.

c)

An error will occur.

d)

The program will restart.

44.

What is the default value of the 'years' parameter in the function 'calculate_future_value'?

a)

10

b)

20

c)

30

d)

40

45.

What is the purpose of the 'calculate_future_value' function in Python?

a)

To calculate the present value of an investment

b)

To determine the future value of an investment

c)

To compute the interest rate of a loan

d)

To find the depreciation value of an asset

46.

In the function 'calculate_future_value', what does the variable 'monthly_interest_rate' represent?

a)

The annual interest rate divided by 12

b)

The interest rate for a single month

c)

The total interest rate over the investment period

d)

The interest rate compounded annually

47.

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).

a)

By passing all required arguments without specifying default values.

b)

By omitting the argument for which the default value is set.

c)

By specifying all arguments including those with default values.

d)

By using a different function that does not have default values.

48.

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).

a)

By passing arguments in the function call

b)

By modifying the function definition

c)

By using global variables

d)

By using a decorator

49.

How do you specify a default value for an argument in a function definition?

a)

By assigning a value to the argument in the function call

b)

By assigning a value to the argument in the function definition

c)

By using a global variable

d)

By using a return statement

50.

In a function definition, where must the arguments with default values be coded?

a)

First

b)

Last

c)

Anywhere

d)

In the middle

51.

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).

a)

By specifying the argument names followed by their values.

b)

By listing the values without specifying argument names.

c)

By using a dictionary to pass the arguments.

d)

By using a list to pass the arguments.

52.

What is a named argument in Python?

a)

An argument passed to a function with a specific name.

b)

An argument that is always optional.

c)

An argument that can only be used in built-in functions.

d)

An argument that is always required.

53.

When calling a function without named arguments, how must the arguments be coded?

a)

In the order they are defined in the function

b)

In any order

c)

Using keyword arguments

d)

Using default values

54.

True or False: When using named arguments, you must code the arguments in the sequence that they’re coded in the function definition.

a)

True

b)

False

55.

Why is it a good practice to use named arguments for functions with many arguments?

a)

It improves code readability.

b)

It reduces the function's execution time.

c)

It allows for dynamic typing.

d)

It increases memory usage.

56.

What is the purpose of the 'calc_tax' function in the given code snippet?

a)

To calculate the total price including tax

b)

To apply a discount to the price

c)

To determine the tax rate

d)

To format the price for display

57.

In the given code, what is the value of 'tax' when 'calc_tax' is called with arguments 85.0 and 0.05?

a)

4.25

b)

3.50

c)

5.00

d)

4.00

58.

Which of the following statements is true about the 'tax' variable in the given code snippet?

a)

'tax' is a global variable.

b)

'tax' is a local variable.

c)

'tax' is a constant.

d)

'tax' is an argument.

59.

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?

a)

The global variable 'tax' is modified inside the main function and its final value is 15.

b)

The global variable 'tax' is not modified inside the main function and its final value remains 10.

c)

The global variable 'tax' is modified inside the main function and its final value is 20.

d)

The global variable 'tax' is modified inside the main function and its final value is 25.