Python Module MCQ Questions and Answers

1. What is a module in Python?

a) A Python file containing definitions and statements
b) A folder containing Python scripts
c) A library of built-in functions
d) A Python framework

Answer:

a) A Python file containing definitions and statements

Explanation:

A module in Python is a file containing Python definitions and statements. The file name is the module name with the suffix '.py'.

2. How do you import a module named 'mymodule' in Python?

a) include mymodule
b) require('mymodule')
c) import mymodule
d) load 'mymodule'

Answer:

c) import mymodule

Explanation:

Modules are imported into Python scripts using the 'import' statement.

3. What is the correct syntax to import only a specific function from a module?

a) from module import function
b) import function from module
c) include module.function
d) require('module', 'function')

Answer:

a) from module import function

Explanation:

To import a specific function from a module, use the syntax 'from module import function'.

4. How do you rename a module at the time of import?

a) import module as new_name
b) import module rename new_name
c) import new_name from module
d) include module as new_name

Answer:

a) import module as new_name

Explanation:

The 'as' keyword is used to import a module under a different name.

5. Where should a module be placed to be recognized by Python?

a) In the current directory or the directories listed in PYTHONPATH
b) In the Python installation directory
c) In any folder on the computer
d) In the directory where Python.exe is located

Answer:

a) In the current directory or the directories listed in PYTHONPATH

Explanation:

Python searches for modules in the current directory and in the list of directories given by the PYTHONPATH environment variable.

6. What file is used to turn a folder into a Python package?

a) __init__.py
b) __main__.py
c) __package__.py
d) __module__.py

Answer:

a) __init__.py

Explanation:

An __init__.py file in a directory indicates that the directory is a Python package, and it can contain modules.

7. How can you list all functions and variables defined in a module?

a) use module.list()
b) call dir(module)
c) print(module.functions)
d) module.showall()

Answer:

b) call dir(module)

Explanation:

The dir() function returns a list of names in a module.

8. What happens if two modules have functions with the same name and both modules are imported?

a) The function in the first imported module is used
b) The function in the last imported module is used
c) A conflict error is raised
d) Both functions are merged

Answer:

b) The function in the last imported module is used

Explanation:

If two imported modules have functions with the same name, the function in the last imported module overwrites the previous one.

9. How do you reload a module in Python?

a) reload(module)
b) importlib.reload(module)
c) module.reload()
d) reimport(module)

Answer:

b) importlib.reload(module)

Explanation:

The reload() function from the importlib module is used to reload a previously imported module.

10. What is the output of print(__name__) when executed in a script file?

a) __main__
b) The name of the script file
c) __name__
d) The name of the Python interpreter

Answer:

a) __main__

Explanation:

When a script is executed, the special variable __name__ is set to __main__.

11. How do you access a variable 'var' defined in a module 'mymodule'?

a) access var from mymodule
b) mymodule.var
c) var.mymodule
d) get var from mymodule

Answer:

b) mymodule.var

Explanation:

You access a variable in a module using the syntax module.variable.

12. What is the purpose of the if __name__ == "__main__": check?

a) To check if the Python interpreter is running
b) To ensure that the module is run as a script
c) To execute code only if the module is run as a script, not imported
d) To define the main function

Answer:

c) To execute code only if the module is run as a script, not imported

Explanation:

This check is used to run code only when the module is executed as a script, not when it is imported in another module.

13. How do you install an external module in Python?

a) download(module)
b) install(module)
c) pip install module
d) module.install()

Answer:

c) pip install module

Explanation:

External modules are typically installed using pip, Python's package installer.

14. What is a built-in module in Python?

a) A module included with Python installation
b) A module that must be imported to use Python
c) A module created by the user
d) A module that automatically loads with every Python script

Answer:

a) A module included with Python installation

Explanation:

Built-in modules are part of the Python Standard Library and are included with Python.

15. How do you create your own module in Python?

a) Write a Python script and save it with a .mod extension
b) Write a Python script and save it with a .py extension
c) Use the module keyword to define a module
d) Create a new module in the Python installation directory

Answer:

b) Write a Python script and save it with a .py extension

Explanation:

A Python module is simply a Python script saved with a .py extension.


Comments