Worksheet #24090602 Functions in Python: Types, User-Defined Functions, Arguments, Parameters, and Scope.
Multiple Choice Questions (MCQs)
- Which of the following is a built-in function in Python?
- a)
print()
- b)
my_function()
- c)
calculate()
- d)
run_code()
- a)
- What is a user-defined function in Python?
- a) A function available in the Python standard library
- b) A function defined by the user using the
def
keyword - c) A function that cannot be modified by the user
- d) A function that Python defines automatically
- Which of the following correctly defines a function in Python?
- a)
function myFunc() {}
- b)
def myFunc():
- c)
myFunc def():
- d)
def myFunc:
- a)
- What is the correct syntax for a function with default parameters?
- a)
def func(x=10, y):
- b)
def func(x, y=20):
- c)
def func(x=10, y=20):
- d)
def func(y=20, x):
- a)
- What does a function return if there is no return statement?
- a)
None
- b)
0
- c) The last value computed
- d) An error message
- a)
- What is the local scope of a variable?
- a) The variable is accessible throughout the entire program
- b) The variable is accessible only inside the function where it is defined
- c) The variable is stored permanently
- d) The variable is defined outside any function
- What is the correct way to call a function named
myFunc
with arguments 10 and 20?- a)
myFunc(10, 20)
- b)
myFunc x=10, y=20
- c)
myFunc(10; 20)
- d)
myFunc(x: 10, y: 20)
- a)
- Which of the following is true about positional parameters in Python functions?
- a) They can only be integers
- b) They must be provided in the order the function defines them
- c) They are always optional
- d) They can be placed in any order when calling a function
- What will the following code print?
def testFunc(x, y=5):
return x + y
print(testFunc(10))
- a)
5
- b)
15
- c)
10
- d)
Error
- Which keyword is used to specify a global variable inside a function?
- a)
global
- b)
nonlocal
- c)
local
- d)
var
- a)
Programming Questions
- Write a Python program to create a user-defined function
greet()
that takes a name as a parameter and prints “Hello, [name]!” - Define a function
add_numbers()
that takes two numbers as positional arguments and returns their sum. - Create a function
find_max()
that accepts three numbers as parameters and returns the largest number among them. - Write a program that defines a function with default parameters and prints different results when called with and without providing arguments.
- Define a function
calculate_area()
that takes length and width as parameters and returns the area of a rectangle. - Create a program that demonstrates the use of a global variable inside a function.
- Write a Python program to demonstrate the flow of execution inside nested functions (functions within functions).
- Write a function
factorial(n)
that returns the factorial of a given numbern
. Demonstrate calling this function with an argument. - Define a function that takes a list of numbers and returns the sum of the numbers. Demonstrate the use of both positional and keyword arguments when calling the function.
- Write a Python program to demonstrate the difference between local and global scope by defining and printing variables inside and outside a function.