MCQs on Functions and Scope | Python programming

1. Defining Functions

  1. Which of the following is the correct syntax to define a function in Python?
    • a) def functionName[]:
    • b) def functionName():
    • c) functionName def():
    • d) def functionName:
  2. What does the def keyword do in Python?
    • a) Defines a function
    • b) Declares a variable
    • c) Creates an object
    • d) Starts a loop
  3. What is the output of the following code?
    def greet(): print("Hello!") greet()
    • a) Error
    • b) Hello!
    • c) None
    • d) Nothing
  4. How do you call a function named example?
    • a) example;
    • b) call example()
    • c) example()
    • d) call example
  5. Which statement is true about Python functions?
    • a) Functions must always return a value
    • b) Functions do not need parentheses
    • c) Functions must be defined before they are used
    • d) Functions in Python are not reusable

2. Arguments and Parameters

  1. What is a parameter in a function?
    • a) The value passed to the function
    • b) A variable defined inside the function
    • c) The name defined in the function signature
    • d) The type of the function
  2. What is an argument in a function?
    • a) A name defined in the function signature
    • b) The return value of a function
    • c) The value passed to a function during a call
    • d) A type of variable
  3. What will be the output of the following code?
    def add(a, b): return a + b print(add(5, 3))
    • a) 8
    • b) 5
    • c) 3
    • d) Error
  4. Which of the following is the correct way to pass arguments to a function?
    • a) function(arg1, arg2)
    • b) function{arg1, arg2}
    • c) function[arg1, arg2]
    • d) function<arg1, arg2>
  5. What will be the output of the following code?
    def print_info(name, age): print(name, age) print_info("John", 25)
    • a) John 25
    • b) Error
    • c) name age
    • d) None

3. Return Statement

  1. What is the purpose of the return statement in a function?
    • a) To exit the function
    • b) To pass the value back to the caller
    • c) To print output
    • d) To stop execution of the program
  2. What will the following code return?
    def multiply(x, y): return x * y result = multiply(4, 5) print(result)
    • a) 45
    • b) 20
    • c) 4
    • d) None
  3. Can a function return multiple values in Python?
    • a) Yes, by using a list
    • b) No, only one value can be returned
    • c) Yes, by separating values with commas
    • d) Yes, using multiple return statements
  4. What will be the output of the following code?
    def square(x): return x * x print(square(6))
    • a) 6
    • b) 36
    • c) 12
    • d) Error
  5. What happens if a function does not have a return statement?
    • a) It returns None by default
    • b) It causes an error
    • c) It returns a random value
    • d) It exits the program

4. Lambda Functions

  1. What is the correct syntax for defining a lambda function in Python?
    • a) lambda x, y: x + y
    • b) function lambda(x, y) {return x + y}
    • c) def lambda(x, y): return x + y
    • d) lambda: return x + y
  2. How is a lambda function different from a regular function?
    • a) Lambda functions can only return a value, whereas regular functions can print as well
    • b) Lambda functions are anonymous and don’t need a name
    • c) Lambda functions cannot take arguments
    • d) Lambda functions are slower than regular functions
  3. What will be the output of the following code?pythonCopy codeadd = lambda x, y: x + y print(add(3, 5))
    • a) 35
    • b) 8
    • c) 0
    • d) Error
  4. Can a lambda function have multiple statements?
    • a) Yes, with the return keyword
    • b) No, it can only have one expression
    • c) Yes, by using semicolons
    • d) No, it is only for calculations
  5. Which of the following is true about lambda functions in Python?
    • a) They are faster than regular functions
    • b) They can only have one parameter
    • c) They can be used in places where functions are not required
    • d) They are used for variable declarations

5. Recursion

  1. What is recursion in programming?
    • a) A function calling itself
    • b) A function calling another function
    • c) A loop calling itself
    • d) A program calling another program
  2. What is the base case in recursion?
    • a) A condition where recursion stops
    • b) A function that calls itself
    • c) A loop that repeats the process
    • d) A case where the function takes no arguments
  3. What is the output of the following recursive function?
    def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) print(factorial(4))
    • a) 4
    • b) 24
    • c) 10
    • d) 120
  4. Which of the following can result in a recursion error?
    • a) When the function doesn’t have a base case
    • b) When the function returns a value
    • c) When the function is called multiple times
    • d) When the function is not defined correctly
  5. What is the risk of deep recursion?
    • a) It can result in stack overflow
    • b) It makes the program faster
    • c) It requires less memory
    • d) It optimizes the function’s performance

6. Variable Scope (Global and Local Variables)

  1. What is a global variable in Python?
    • a) A variable declared inside a function
    • b) A variable declared outside of all functions
    • c) A variable that can only be used inside the main program
    • d) A variable that cannot be used inside functions
  2. Which of the following statements about local variables is correct?
    • a) Local variables are accessible from anywhere in the program
    • b) Local variables are only accessible within the function they are declared in
    • c) Local variables are only accessible inside loops
    • d) Local variables do not have a scope
  3. How can you modify a global variable inside a function?
    • a) By using the global keyword
    • b) By passing the variable as an argument
    • c) By returning the modified value
    • d) By redefining the variable inside the function
  4. What is the scope of a variable defined inside a function?
    • a) Local scope
    • b) Global scope
    • c) Module scope
    • d) Block scope
  5. What will be the output of the following code?
    x = 5 def my_function(): global x x = 10 my_function() print(x)
    • a) 5
    • b) 10
    • c) Error
    • d) None

Answers (Tabular Form)

Question No.Answer
1b
2a
3b
4c
5c
6c
7c
8a
9a
10a
11b
12b
13c
14b
15a
16a
17b
18b
19b
20c
21a
22a
23b
24a
25a
26b
27b
28a
29a
30b

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top