Python Essentials 2 Module 1 Completion – Module Test Answers
Python Essentials 2: PE2: Module 1. Modules, Packages, and PIP test Answers full new questions
1. Knowing that a function named fun() resides in a module named mod, choose the correct way to import it.
- import fun from mod
- from fun import mod
- from mod import fun
- import fun
Remember that to import specific functions from a module, the keyword from followed by the module’s name is used. Afterward, specify function names after the import keyword, separated by commas.
2. Knowing that a function named fun() resides in a module named mod, and it has been imported using the following line:
import mod
Choose the way it can be invoked in your code.
- mod.fun()
- mod::fun()
- mod‑>fun()
- fun()
If the entire module is imported, function invocation must use the following syntax module_name.funcion_name().
3. A function which returns a list of all entities available in a module is called:
- listmodule()
- entities()
- content()
- dir()
Remember that the dir() function returns all properties and methods of a specified object, including built-in properties.
4. A pyc file contains:
- compiled Python code
- a Python compiler
- a Python interpreter
- Python source code
Remember that pyc files are created by the Python interpreter using py files. They contain the compiled bytecode of the source code.
5. When a module is imported, its contents:
- may be executed (explicitly)
- are executed as many times as they are imported
- are ignored
- are executed once (implicitly)
When a module in Python is imported, the entire file is executed once implicitly in order to assure no errors are present in the module.
6. A predefined Python variable that stores the current module name is called:
- __modname__
- __mod__
- __module__
- __name__
7. The following statement:
from a.b import c
causes the import of:
- entity c from module b from package a
- entity a from module b from package c
- entity c from module a from package b
- entity b from module a from package c
When using the notation from a.b import c, c identifies the entity or function being imported, b identifies the module (source file), and a the package (folder).
8. What is the expected value assigned to the result variable after the following code is executed?
import math result = math.e != math.pow(2, 4) print(int(result))
- False
- True
- 1
- 0
Let’s analyze this code snippet:
- The math module is imported.
- The result variable will contain the logical result of != (the not equal operation).
- The math.e value is a constant that equals 2.718281828459045.
- The result of math.pow(2, 4) is 2 to the power of 4, which is 16.0. The result is a floating-point number.
- Since 2.718281828459045 is NOT equal to 16.0, the answer is True.
- Using the print function, the answer is shown in the console converted into an integer value, which is 1.
9. What is the expected output of the following code?
from random import randint for i in range(2): print(randint(1, 2), end='')
- 11, 12, 21, or 22
- 12, or 21
- 12
- There are millions of possible combinations, and the exact output cannot be predicted.
Let’s analyze this code snippet:
- The randint method is imported from the random module. It will return an integer number selected from the specified range.
- The loop for i in range(2) is initialized and will iterate twice.
- The print(randint(1, 2), end=”) invocation will output 1 or 2 in the console and will stay in the same line due to the end argument.
- The print(randint(1, 2), end=”) invocation is executed again because it is inside the loop and will output 1 or 2 in the console. The loop is exited.
- The possible outputs are 11, 12, 21, and 22.
10. Choose the true statements. (Select two answers)
- The version function from the platform module returns a string with your OS version.
- The version function from the platform module returns a string with your Python version.
- The processor function from the platform module returns an integer with the number of processes currently running in your OS.
- The system function from the platform module returns a string with your OS name.
11. During the first import of a module, Python deploys the pyc files in the directory called:
- Hashbang
- __pycache__
- __init__
- Mymodules
12. The digraph written as #! is used to:
- tell an MS Windows OS how to execute the contents of a Python file
- tell a Unix or Unix-like OS how to execute the contents of a Python file
- create a docstring
- make a particular module entity private
13. A list of package dependencies can be obtained from pip using a command named:
- dir
- show
- deps
- list
14. The pip list command presents a list of:
- all packages available at PyPI
- locally installed packages
- outdated local packages
- available pip commands
15. What is true about the pip search command? (Select two answers)
- All its searches are limited to locally installed packages.
- It searches through all PyPI packages.
- It needs a working Internet connection to work.
- It searches through package names only.
16. What is true about the pip install command? (Select two answers)
- It installs a package per user only when the –user option is specified.
- It installs a package system-wide only when the –system option is specified.
- It always installs the newest package version and it cannot be changed.
- It allows the user to install a specific version of the package.
17. What is true about updating already installed Python packages?
- It can be done only by uninstalling and installing the packages once again.
- It’s performed by the install command accompanied by the -U option.
- It can be done by reinstalling the package using the reinstall command.
- It’s an automatic process which doesn’t require any user attention.
18. Which command can you use pip to remove an installed package?
- pip install –uninstall package
- pip uninstall package
- pip –uninstall package
- pip remove package