Python Essentials 2 – Module 1 Test Answers (Completion)

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
Explanation:

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()
Explanation:

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()
Explanation:

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
Explanation:

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)
Explanation:

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__
Explanation: The __name__ variable (two underscores before and after) is a special Python variable. When a module is imported, it will contain the module’s filename.

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
Explanation:

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
Explanation:

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.
Explanation:

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.
Explanation: The platform module recovers OS information from your system. The version function returns a string with your OS version, and the system function returns a string with the name of your OS.

11. During the first import of a module, Python deploys the pyc files in the directory called:

  • Hashbang
  • __pycache__
  • __init__
  • Mymodules
Explanation: Remember that __pycache__ is a directory that contains bytecode (.pyc) cache files that are automatically generated by Python.

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
Explanation: The #! helps specify the type of program to call to run the entire script file in Unix-type systems. It is always used on the first line of any file.

13. A list of package dependencies can be obtained from pip using a command named:

  • dir
  • show
  • deps
  • list
Explanation: The show command displays metadata information about a package such as version, description, author, license information, summary and dependencies.

14. The pip list command presents a list of:

  • all packages available at PyPI
  • locally installed packages
  • outdated local packages
  • available pip commands
Explanation: Remember that the pip list command returns the list of packages installed in the current local environment.

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.
Explanation: Remember that the pip search command is used to search the index and identify packages that match the search terms through all the PyPI packages. Therefore, it requires an Internet connection to work.

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.
Explanation: Remember that to install a specific version of a Python package you can use pip install package_name==package_version. The –user option changes the scope of the installation to the user’s home directory rather than the default system-wide installation.

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.
Explanation: Remember that the -U, –upgrade option upgrades all packages installed to the newest available version.

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
Explanation: Remember that the option removes the installed package from the environment.

Leave a Reply

Your email address will not be published. Required fields are marked *