This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Python Modules”.
1. Which of these definitions correctly describes a module?
a) Denoted by triple quotes for providing the specification of certain program elements
b) Design and implementation of specific functionality to be incorporated into a program
c) Defines the specification of how it is to be used
d) Any program that reuses code
View Answer
Explanation: The term “module” refers to the implementation of specific functionality to be incorporated into a program.
2. Which of the following is not an advantage of using modules?
a) Provides a means of reuse of program code
b) Provides a means of dividing up tasks
c) Provides a means of reducing the size of the program
d) Provides a means of testing individual parts of the program
View Answer
Explanation: The total size of the program remains the same regardless of whether modules are used or not. Modules simply divide the program.
3. Program code making use of a given module is called a ______ of the module.
a) Client
b) Docstring
c) Interface
d) Modularity
View Answer
Explanation: Program code making use of a given module is called the client of the module. There may be multiple clients for a module.
4. ______ is a string literal denoted by triple quotes for providing the specifications of certain program elements.
a) Interface
b) Modularity
c) Client
d) Docstring
View Answer
Explanation: Docstring used for providing the specifications of program elements.
5. Which of the following is true about top-down design process?
a) The details of a program design are addressed before the overall design
b) Only the details of the program are addressed
c) The overall design of the program is addressed before the details
d) Only the design of the program is addressed
View Answer
Explanation: Top-down design is an approach for deriving a modular design in which the overall design.
6. In top-down design every module is broken into same number of submodules.
a) True
b) False
View Answer
Explanation: In top-down design every module can even be broken down into different number of submodules.
7. All modular designs are because of a top-down design process.
a) True
b) False
View Answer
Explanation: The details of the program can be addressed before the overall design too. Hence, all modular designs are not because of a top-down design process.
8. What will be the output of the following Python code?
#mod1 def change(a): b=[x*2 for x in a] print(b) #mod2 def change(a): b=[x*x for x in a] print(b) from mod1 import change from mod2 import change #main s=[1,2,3] change(s)
a) [2,4,6]
b) [1,4,9]
c)
[2,4,6] [1,4,9]
d) There is a name clash
View Answer
Explanation: A name clash is when two different entities with the same identifier become part of the same scope. Since both the modules have the same function name, there is a name clash.
9. Which of the following isn’t true about main modules?
a) When a python file is directly executed, it is considered main module of a program
b) Main modules may import any number of modules
c) Special name given to main modules is: __main__
d) Other main modules can import main modules
View Answer
Explanation: Main modules are not meant to be imported into other modules.
10. Which of the following is not a valid namespace?
a) Global namespace
b) Public namespace
c) Built-in namespace
d) Local namespace
View Answer
Explanation: During a Python program execution, there are as many as three namespaces – built-in namespace, global namespace and local namespace.
11. Which of the following is false about “import modulename” form of import?
a) The namespace of imported module becomes part of importing module
b) This form of import prevents name clash
c) The namespace of imported module becomes available to importing module
d) The identifiers in module are accessed as: modulename.identifier
View Answer
Explanation: In the “import modulename” form of import, the namespace of imported module becomes available to, but not part of, the importing module.
12. Which of the following is false about “from-import” form of import?
a) The syntax is: from modulename import identifier
b) This form of import prevents name clash
c) The namespace of imported module becomes part of importing module
d) The identifiers in module are accessed directly as: identifier
View Answer
Explanation: In the “from-import” form of import, there may be name clashes because names of the imported identifiers aren’t specified along with the module name.
13. Which of the statements about modules is false?
a) In the “from-import” form of import, identifiers beginning with two underscores are private and aren’t imported
b) dir() built-in function monitors the items in the namespace of the main module
c) In the “from-import” form of import, all identifiers regardless of whether they are private or public are imported
d) When a module is loaded, a compiled version of the module with file extension .pyc is automatically produced
View Answer
Explanation: In the “from-import” form of import, identifiers beginning with two underscores are private and aren’t imported.
14. What will be the output of the following Python code?
from math import factorial print(math.factorial(5))
a) 120
b) Nothing is printed
c) Error, method factorial doesn’t exist in math module
d) Error, the statement should be: print(factorial(5))
View Answer
Explanation: In the “from-import” form of import, the imported identifiers (in this case factorial()) aren’t specified along with the module name.
15. What is the order of namespaces in which Python looks for an identifier?
a) Python first searches the global namespace, then the local namespace and finally the built-in namespace
b) Python first searches the local namespace, then the global namespace and finally the built-in namespace
c) Python first searches the built-in namespace, then the global namespace and finally the local namespace
d) Python first searches the built-in namespace, then the local namespace and finally the global namespace
View Answer
Explanation: Python first searches for the local, then the global and finally the built-in namespace.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Python Internship
- Practice Programming MCQs
- Check Information Technology Books
- Apply for Programming Internship
- Check Python Books