100 multiple‐choice questions (MCQs) on Python’s Modules & Functions
Q1. What is a module in Python?
A) A compiled binary file
B) A file containing Python definitions and statements
C) A built-in function for mathematics
D) A type of loop
Answer: B
Q2. Which keyword is used to define a function in Python?
A) func
B) function
C) def
D) lambda
Answer: C
Q3. How do you import an entire module named math?
A) include math
B) import math
C) require math
D) using math
Answer: B
Q4. What does the statement from math import sqrt do?
A) Imports the entire math module
B) Imports only the sqrt function from the math module
C) Renames the math module to sqrt
D) Imports all functions but excludes sqrt
Answer: B
Q5. Which file is considered a module in Python?
A) A file with a .py extension containing Python code
B) A file with a .txt extension
C) Any file on the hard drive
D) A compiled .exe file
Answer: A
Q6. What does the module attribute __name__ represent when a module is imported?
A) The file name with path
B) The module’s documentation string
C) The name of the module
D) The module’s version number
Answer: C
Q7. What does the following code print when placed at the end of a module?
if __name__ == "__main__":
print("Hello")
A) Always prints "Hello" regardless of import
B) Prints "Hello" only when the module is executed as a script
C) Prints "Hello" every time the module is imported
D) Produces a SyntaxError
Answer: B
Q8. Which of the following is true about Python packages?
A) They are single Python files
B) They are directories containing a special init.py file
C) They require a setup.py file to run
D) They are only used to store images
Answer: B
Q9. How can you import a module and give it an alias?
A) import module as alias
B) alias = import module
C) from module import alias
D) module => alias
Answer: A
Q10. Which module provides access to the list of directories Python searches for modules?
A) os
B) sys
C) path
D) importlib
Answer: B
Q11. What is the purpose of the __init__.py file in a package?
A) It initializes global variables
B) It is required to mark a directory as a Python package
C) It stores user settings
D) It automatically executes a module function
Answer: B
Q12. Which of the following is a valid way to define a function with no parameters?
A) def my_function[]: pass
B) def my_function(): pass
C) def my_function(void): pass
D) def my_function: pass
Answer: B
Q13. What is a docstring in a function?
A) A comment placed at the end of a function
B) A string literal that appears as the first statement in a function used for documentation
C) A type conversion function
D) A tool for debugging
Answer: B
Q14. How can you call a function named calculate from an imported module stats?
A) stats.calculate()
B) calculate.stats()
C) import calculate from stats
D) stats->calculate()
Answer: A
Q15. What does the lambda keyword in Python do?
A) It declares a generator
B) It creates an anonymous function
C) It imports a module
D) It defines a class method
Answer: B
Q16. Which of the following is a correct lambda function that squares its input?
A) lambda x: x * x
B) lambda x => x ** 2
C) def lambda(x): return x * x
D) lambda x, x
Answer: A
Q17. What is the return value of a function that does not explicitly include a return statement?
A) 0
B) None
C) An empty string
D) It raises an error
Answer: B
Q18. How are default parameter values specified in a function definition?
A) def f(x = 10): …
B) def f(default x:10): …
C) def f(x == 10): …
D) def f(x: 10): …
Answer: A
Q19. Which of the following correctly defines a function with variable-length positional arguments?
A) def f(args): …
B) def f(args): …
C) def f(**args): …
D) def f(args...): …
Answer: A
Q20. How do you define a function that accepts variable-length keyword arguments?
A) def f(*kwargs): …
B) def f(kwargs): …
C) def f(kwargs): …
D) def f(kw...): …
Answer: B
Q21. What is the scope of a variable defined inside a function?
A) Global
B) Local to the function
C) Both global and local
D) Persistent between function calls
Answer: B
Q22. Which keyword is used to modify a global variable inside a function?
A) global
B) nonlocal
C) static
D) def
Answer: A
Q23. Which statement best describes a higher-order function in Python?
A) A function that returns a module
B) A function that takes other functions as arguments or returns a function
C) A function with more than two parameters
D) A function defined inside a class
Answer: B
Q24. How do you access a function’s documentation string?
A) f.doc
B) f.doc()
C) f.documentation
D) doc(f)
Answer: A
Q25. Which built-in function returns the length of a function’s argument list?
A) len()
B) help()
C) code.co_argcount
D) args()
Answer: C
Q26. What does the built-in function callable() check for?
A) Whether an object is a function or can be called
B) The number of arguments a function takes
C) The return type of a function
D) If a module is imported successfully
Answer: A
Q27. Which special function attribute contains the compiled bytecode of the function?
A) code
B) doc
C) call
D) func
Answer: A
Q28. What is the purpose of a decorator in Python?
A) To change the definition or behavior of a function without permanently modifying it
B) To import a module dynamically
C) To convert a function into a generator
D) To define a function inside a class
Answer: A
Q29. Which syntax is used to apply a decorator @my_decorator to a function f?
A) f = my_decorator(f)
B) @my_decorator before the function definition
C) Both A and B
D) my_decorator.f()
Answer: C
Q30. How do you import a specific function foo from a module named bar?
A) import foo from bar
B) from bar import foo
C) include foo in bar
D) require foo from bar
Answer: B
Q31. What is the effect of importing a module more than once in the same program?
A) It reloads the module each time
B) The module code is executed only the first time and then cached
C) It causes a SyntaxError
D) It creates a duplicate module object
Answer: B
Q32. Which module in the standard library helps with dynamic import of modules?
A) os
B) sys
C) importlib
D) modulelib
Answer: C
Q33. What does the help() function do when passed a function or module?
A) Runs the function
B) Displays the documentation string
C) Exports the module
D) Compiles the function
Answer: B
Q34. What is the purpose of the __init__.py file in a Python package?
A) To mark a directory as a Python package and optionally execute package initialization code
B) To serve as the main module
C) To import all functions automatically
D) To compile modules into bytecode
Answer: A
Q35. Which special variable in a module indicates if it is being run as the main program?
A) main
B) name
C) file
D) package
Answer: B
Q36. What happens if you run a module as a script and it contains the block if __name__ == "__main__":?
A) That block is skipped
B) That block is executed
C) The module’s functions are imported automatically
D) The module reloads itself
Answer: B
Q37. Which statement best describes the purpose of relative imports in Python packages?
A) To import modules using absolute paths
B) To import modules relative to the current module’s location
C) To import modules from the system directory
D) To import modules with aliasing
Answer: B
Q38. How can you call a function defined in the current module?
A) Use the module name with dot notation
B) Call it directly by its name
C) Both A and B (if in the same module)
D) You cannot call functions in the same module
Answer: C
Q39. What is a built-in function used to determine the type of an object (including functions)?
A) type()
B) isinstance()
C) id()
D) classof()
Answer: A
Q40. Which of the following is a function object?
A) A variable holding a function defined by def
B) A lambda expression
C) A function imported from a module
D) All of the above
Answer: D
Q41. How do you assign a function to a variable in Python?
A) my_var = func()
B) my_var = func
C) my_var := func
D) function my_var = func
Answer: B
Q42. What does the *args syntax in a function definition allow?
A) It collects extra positional arguments as a tuple
B) It collects extra keyword arguments as a dictionary
C) It unpacks a list into arguments
D) It restricts the number of arguments
Answer: A
Q43. What does the **kwargs syntax in a function definition allow?
A) It collects extra positional arguments
B) It collects extra keyword arguments as a dictionary
C) It converts keyword arguments into a list
D) It specifies required keyword arguments only
Answer: B
Q44. Which of the following correctly defines a function with both positional arguments and variable keyword arguments?
A) def f(a, *args, **kwargs): …
B) def f(a, **kwargs, *args): …
C) def f(*args, a, **kwargs): …
D) def f(a, **args): …
Answer: A
Q45. What is a closure in Python?
A) A function object that remembers values in its enclosing scope even if that scope is no longer present
B) A function that runs only once
C) A module that has been imported completely
D) A type of loop
Answer: A
Q46. Which of the following is a valid use of a lambda function?
A) As a short function passed as an argument
B) For complex multi-statement functions
C) To decorate a function
D) To import modules dynamically
Answer: A
Q47. Which built-in function can turn a lambda into a callable function?
A) lambda()
B) call()
C) There is no need; lambda expressions are already callable
D) func()
Answer: C
Q48. Which function returns a sequence of numbers and is commonly used for looping?
A) range()
B) enumerate()
C) zip()
D) list()
Answer: A
Q49. What is the output of the following code?
def add(a, b=5):
return a + b
print(add(10))
A) 10
B) 15
C) 5
D) Error
Answer: B
Q50. What does the statement del func do if func is a function defined earlier?
A) It deletes the function object entirely
B) It removes the name binding to the function
C) It causes the function to run
D) It renames the function
Answer: B
Q51. Which of the following is used to document a module, function, or class?
A) Comments starting with //
B) Triple-quoted string immediately following the definition
C) A special function called document()
D) The doc attribute
Answer: B
Q52. How can you access a module’s docstring?
A) module.doc
B) doc(module)
C) module.doc()
D) help(module)
Answer: A
Q53. Which of the following is true about function objects in Python?
A) They can be passed as arguments to other functions
B) They can be returned from functions
C) They can be assigned to variables
D) All of the above
Answer: D
Q54. How do you create an anonymous function in Python that adds two numbers?
A) def lambda x, y: x + y
B) lambda x, y: x + y
C) function(x, y) => x + y
D) anon(x, y): x + y
Answer: B
Q55. What is the output of the following expression?
( lambda x: x * 3 )(4)
A) 7
B) 12
C) 34
D) 3
Answer: B
Q56. What feature does Python offer for function introspection?
A) The code attribute
B) The globals attribute
C) The closure attribute
D) All of the above
Answer: D
Q57. Which special attribute of a function object contains the tuple of argument names?
A) args
B) parameters
C) code.co_varnames
D) signature
Answer: C
Q58. How do you ensure that a function does not modify a mutable default argument across calls?
A) Use None as the default and initialize inside the function
B) Use a constant value
C) Use lambda to create a new object
D) It is not possible
Answer: A
Q59. Which module provides support for working with function signatures and introspection?
A) inspect
B) types
C) sys
D) io
Answer: A
Q60. Which statement about function decorators is true?
A) Decorators can modify or extend a function’s behavior without permanently changing it
B) A decorator must be written in C
C) Decorators cannot access the original function’s arguments
D) Decorators are only used in class definitions
Answer: A
Q61. What is the syntax to apply multiple decorators to a function?
A) Place each decorator on a new line above the function definition
B) Separate them with commas on the same line
C) Use nested function calls
D) It is not allowed to apply more than one decorator
Answer: A
Q62. How would you import a module named utilities that is in the same directory as your script?
A) import ./utilities
B) from . import utilities
C) import utilities
D) require utilities
Answer: C
Q63. What is the purpose of the import * syntax?
A) To import all names defined in a module into the current namespace
B) To import only the functions of a module
C) To import names with leading underscores
D) To combine two modules
Answer: A
Q64. Which of the following is a potential drawback of using from module import *?
A) It can lead to namespace pollution
B) It decreases code readability
C) It may overwrite existing names in the namespace
D) All of the above
Answer: D
Q65. How can you reload a previously imported module?
A) Use the reload() function from the importlib module
B) Re-import the module normally
C) Delete the module from sys.modules
D) It is not possible
Answer: A
Q66. Which built-in function can you use to check if an object is a module?
A) isinstance(obj, module)
B) type(obj) is ModuleType
C) hasattr(obj, 'file')
D) Both B and C
Answer: D
(Assuming you import ModuleType from the types module.)
Q67. How can you list all attributes defined in a module named mod?
A) dir(mod)
B) mod.attributes()
C) list(mod)
D) mod.all
Answer: A
Q68. Which of the following methods is used to document a function’s parameters and expected types?
A) Type hints (annotations) in the function signature
B) The doc string
C) Comments before the function definition
D) Both A and B
Answer: D
Q69. What does the following function return?
def f(a, b=2):
return a ** b
A) The sum of a and b
B) The product of a and b
C) a raised to the power of b
D) b raised to the power of a
Answer: C
Q70. What are function annotations used for in Python?
A) To enforce type checking at runtime
B) To provide metadata about function parameters and return types
C) To modify function behavior
D) They have no purpose
Answer: B
Q71. Which function from the functools module is used to cache function return values?
A) lru_cache()
B) memoize()
C) cache()
D) persist()
Answer: A
Q72. What is the role of the nonlocal keyword inside a nested function?
A) It declares a variable as global
B) It allows modifying a variable from an enclosing (non-global) scope
C) It creates a new local variable
D) It prevents variable modification
Answer: B
Q73. Which of the following can be used to inspect the source code of a function?
A) inspect.getsource()
B) help()
C) type()
D) repr()
Answer: A
Q74. What does the map() function do?
A) Applies a given function to each item of an iterable and returns a map object
B) Filters elements based on a condition
C) Merges two lists
D) Converts a list to a dictionary
Answer: A
Q75. What is the output type of the map() function in Python 3?
A) List
B) Tuple
C) Iterator
D) Dictionary
Answer: C
Q76. Which built-in function applies a function cumulatively to the items of an iterable?
A) reduce()
B) accumulate()
C) sum()
D) product()
Answer: A
(Note: In Python 3, reduce() is in the functools module.)
Q77. What is a generator function?
A) A function that returns a generator iterator using the yield keyword
B) A function that generates random numbers
C) A function that takes no arguments
D) A function that must return a list
Answer: A
Q78. How do you define a generator function?
A) Using the def keyword with at least one yield statement
B) Using the lambda keyword
C) Using the generate keyword
D) Using a for loop only
Answer: A
Q79. Which statement best describes a recursive function?
A) A function that calls itself
B) A function that is defined using lambda
C) A function that has no parameters
D) A function that imports modules recursively
Answer: A
Q80. What is a potential risk of using recursion in functions?
A) It always produces incorrect results
B) It can lead to excessive memory usage and a stack overflow if not properly designed
C) It prevents the function from returning a value
D) It makes the function non-callable
Answer: B
Q81. Which module provides advanced tools for function manipulation such as partial function application?
A) functools
B) itertools
C) operator
D) sys
Answer: A
Q82. What does functools.partial() do?
A) It creates a new function with some arguments of the original function fixed
B) It splits a function into multiple parts
C) It partially imports a module
D) It converts a function into a lambda
Answer: A
Q83. Which special method must a function object implement to be callable?
A) init
B) call
C) func
D) object
Answer: B
Q84. What does the built-in function isinstance() check for when used with functions?
A) Whether an object is callable
B) Whether an object is an instance of a specific type (such as function types)
C) The number of arguments the function takes
D) The existence of a call attribute
Answer: B
Q85. How can you pass a function as a parameter to another function?
A) Simply pass its name without parentheses
B) Convert it to a string
C) Use the import statement
D) You cannot pass functions as parameters
Answer: A
Q86. Which of the following is a first-class citizen feature of Python functions?
A) They can be assigned to variables
B) They can be passed as arguments
C) They can be returned from other functions
D) All of the above
Answer: D
Q87. What is the output of the following code snippet?
def greet(name="World"):
return "Hello, " + name
print(greet(), greet("Alice"))
A) Hello, greet()
B) Hello, World Hello, Alice
C) "Hello, " "Hello, Alice"
D) Error
Answer: B
Q88. Which built-in function can be used to get a function’s memory address?
A) id()
B) addr()
C) memory()
D) pointer()
Answer: A
Q89. What is function currying?
A) Transforming a function that takes multiple arguments into a sequence of functions that each take a single argument
B) Converting a function into a lambda
C) Combining two functions into one
D) Reducing a function’s runtime
Answer: A
Q90. How do you denote a function that does not return any value?
A) It returns 0
B) It returns None by default
C) It returns an empty string
D) It must explicitly use return False
Answer: B
Q91. What is the purpose of the __all__ variable in a module?
A) To list all names that should be imported when using from module import *
B) To list all functions in the module
C) To set the module’s version
D) To store the module’s documentation
Answer: A
Q92. Which built-in module helps in handling dynamic function invocation?
A) importlib
B) operator
C) types
D) eval
Answer: D
(eval can be used to evaluate an expression dynamically, though use with caution.)
Q93. What does the function globals() return when called inside a function?
A) The global namespace as a dictionary
B) The local variables as a dictionary
C) The built-in variables
D) It returns None
Answer: A
Q94. How can you create a function that accepts an arbitrary number of arguments and returns their sum?
A) def sum_all(*nums): return sum(nums)
B) def sum_all(nums): return sum(nums)
C) def sum_all(**nums): return sum(nums)
D) def sum_all(*nums): return sum(*nums)
Answer: A
Q95. Which syntax allows you to specify type hints for function parameters and return value?
A) def func(x: int) -> int: …
B) def func(int x) -> int: …
C) def func(x): int …
D) def func(x): return int
Answer: A
Q96. What is the typical use of the if __name__ == "__main__": block in a module?
A) To execute test code or a script when the module is run directly
B) To import the module
C) To define functions
D) To export variables
Answer: A
Q97. Which keyword indicates that a function does not accept any arguments?
A) void
B) noargs
C) An empty parameter list ()
D) It uses the keyword none
Answer: C
Q98. What is the significance of function annotations?
A) They enforce runtime type checking
B) They serve as metadata for parameters and return types without enforcement
C) They slow down function execution
D) They transform functions into lambdas
Answer: B
Q99. Which statement is true about functions defined inside other functions?
A) They cannot access variables from the outer function
B) They form closures if they reference variables from the enclosing scope
C) They are automatically global
D) They require the keyword nested
Answer: B
Q100. Which of the following best summarizes Python’s modules and functions topic?
A) Modules organize code into separate files while functions encapsulate reusable blocks of logic, and both are treated as first-class objects
B) Modules are for variables only and functions are for loops only
C) Functions cannot be defined in modules
D) Modules and functions are deprecated in modern Python
Answer: A