0% found this document useful (0 votes)
11 views9 pages

Python Module Import Test Questions

Uploaded by

Khánh Ngân
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Python Module Import Test Questions

Uploaded by

Khánh Ngân
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Sample Test Questions for Chapter 13

1) A programmer will typically write a(n) _____ and pass it to the interpreter.

a. script

b. method

c. object

d. variable

2) Identify the correct syntax for importing modules from the script [Link]?

a. import [Link]

b. import readFile

c. Import ReadFile

d. import READFILE

3) If the module urllib has not been loaded, which of the following statements causes the new module
object to be created and added to [Link]?

a. import urllib

b. def urllib

c. class urllib

d. run urllib

4) Which of the following statements isnottrue for importing a module:

a. If the module has already been loaded, then the existing module object is used.

b. If a module is not found in [Link], a new module object is created.

c. If the module has already been loaded, then a new namespace is created and its codes are executed.

d. If a module is not found in [Link], then the module is added and its codes are executed.

5) A _____ is a namespace that contains definitions from the module.


a. module function

b. module class

c. module script

d. module object

6) Which of the following directories is contained in [Link]?

a. All available directories on your computer.

b. Directories of all local files used by the script.

c. The directory where Python is installed.

d. The root directory on your computer.

7) Which of the following is a good name for a new custom module?

a. [Link]

b. [Link]

c. [Link]

d. [Link]

8) _____ can be set to specify optional directories where modules are located.

a. [Link]

b. [Link]

c. SYSPATH

d. PYTHONPATH

9) Which of the following is true for an environment variable?

a. Its value might change during execution or its constant value might be reassigned

b. It is stored in the root folder of Python

c. It can be accessed by every program running on the computer

d. Its name can be any user-defined name, which is not case sensitive
10) Identify the code that generates "The value of pi is: 3.14" as output.

a. from math import pi


print(f"The value of pi is: {pi:.2f}")

b. import pi from math


print(f"The value of pi is: {pi:.2f}")

c. import math
print(f"The value of pi is: {pi:.2f}")

d. import pi
print(f"The value of pi is: {[Link]:.2f}")

11) Identify the correct import statement for the following code:

print(f"The sum of values is: {[Link](10, 20)}")

a. import sum

b. from calculate import sum

c. from * import sum

d. import calculate

12) Consider a module file named Rectangle containing functions calc_area() and length(). Which option
is the right way to import only these functions?

a. import calc_area(5, 10)


calc_area(5, 10)

b. import Rectangle
print(calc_area(5, 10))

c. from Rectangle import (calc_area, length)


print(calc_area(5, 10))

d. import calc_area, length


calc_area(5, 10)

13) Which of the following statements imports an entire module named ABC?

a. from ABC import All

b. from ABC import print_val

c. from ABC import as all


d. import ABC

14) Which of the following is added into the global namespace after running thefrom ABC import
(length, breadth)import statement?

a. The entire module ABC.

b. All the variables defined in ABC.

c. Only the length and breadth attributes of ABC.

d. All attributes except length and breadth attributes of ABC

15) What is output?

from math import (pi, exp, log)


import urllib
print(__name__)

a. __urllib__

b. __name__

c. __math.pi__

d. __main__

16) Identify the code that generates "os" as output.

a. import os
print(path.__name__)

b. import os
print(os.__name__)

c. import os
print(__name__)

d. import os
print([Link])

17) Identify the module file [Link] for the given code that will generate the following output.

Radius is 6

Area: 113.10
from circle import (area, radius)

if __name__ == "__main__":
a = area(6)
print(f"Area: {a:.2f}")

a. import math
def area(r):
print(radius(r))
return [Link] * r * r
def radius(r):
return(f"Radius is {r}")
print(radius, area)

b. import math
def area(r):
print(radius(r))
return [Link] * r * r
def radius(r):
print(radius, area)
return(f"Radius is {r}")

if __name__ == "__main__":
print(radius, area)

c. import circle
import math

print("radius", "area")
def area(r):
print(radius(r))
return [Link] * r * r
def radius(r):
return(f"Radius is {r}")

d. import math
def area(r):
print(radius(r))
return [Link] * r * r
def radius(r):
return(f"Radius is {r}")

if __name__ == "__main__":
print(radius, area)
18) If__name__ == "__main__"is true, then ______.

a. the file is being executed as a script

b. the file is being executed as a module

c. the file is being imported

d. the function is being defined as class module

19) Which of following modules would warrant the use of the reload() function in a script?

a. A module that appends a new line to an existing MS Excel file.

b. A module that scrapes off data from a list of websites which are updated every day.

c. A module that calculates the Earth's orbital speed using constants.

d. A module that generates random numbers between two values and appends the number to a list.

20) The reload() function returns the _____.

a. namespace of the module with updated definitions

b. pre-existing namespace

c. attributes corresponding to a module

d. variables defined in the module

21) Which of the following statements provides the path to a module named [Link]?

a. file(circle)

b. [Link]()

c. __file__(circle)

d. circle.__file__

22) For a function print_names() imported in a code, which of the following statements is the correct
syntax to reload that function in the same code?

a. print_names reload

b. reload.print_names()

c. print_names.reload()
d. reload(print_names)

23) A(n) _____ is a directory that gives access to all of the modules stored in a directory when imported.

a. function

b. package

c. init

d. group

24) Consider a package, Shapes, that contains the modules [Link], [Link], and [Link].
Which of the following statements should be used to import [Link]?

a. import [Link]

b. import [Link]

c. import [Link]

d. import Shapes
import rectangle

25) Which could be the name of the function for the import statementfrom
[Link] import area?

a. Shapes

b. Square

c. area

d. dimensions

26) Identify the correct package import statement forprint(f"Area =


{[Link](a)}").

a. from [Link] import

b. import Shapes

c. from [Link] import dimensions

d. import [Link]
27) Which of the following standard libraries can be used to convert time zones?

a. datetime

b. time

c. math

d. random

28) Which of the following standard libraries is used to access a file stored on a website?

a. sys

b. os

c. pdb

d. urllib

29) Replace x and y with the correct module to generate the following output.

Calculation done on: *actual date*


36.0

from x import pow


import y

today_date = [Link]()
base = 6
exponent = 2

print(f"Calculation done on: {today_date}")

print(pow(base, exponent))

a. random, math

b. time, math

c. math, datetime

d. exp, datetime

30) Which of the following statements pauses code execution for 5 seconds?

a. import time
[Link](5)
b. import time
[Link](5)

c. import datetime
[Link](5)

d. import datetime
[Link](5)

31) Which of the following libraries is used to retrieve the current working directory?

a. sys

b. os

c. pdb

d. urllib

Common questions

Powered by AI

The reload() function is necessary when a module dynamically updates often, such as a data scraping module or when changes in the module code need to be reflected without starting a new session .

The correct syntax to import a module named 'example' is 'import example' without including the '.py' file extension .

'import ABC' imports the entire module ABC, allowing access to its content through 'ABC.x'. 'from ABC import x, y' imports only the specified elements 'x' and 'y' directly into the caller's namespace .

A package in Python, which is a directory containing a special '__init__.py' file, provides a namespace to access all modules stored in it. This enables organized and modular code imports from within the package .

sys.path is a list of directory names where Python looks for modules to import. PYTHONPATH is an environment variable that can override sys.path, specifying additional directories to search for modules .

Incorrect module file naming may lead to namespace collisions with standard libraries or other modules, causing unintended behavior and making maintenance difficult. It disrupts expected import functionality and can introduce bugs due to improper module resolution .

The '__name__' variable determines whether a module is run as a standalone script or imported elsewhere. When '__name__' is '__main__', it indicates that the module is executed as the primary script, allowing conditional execution of code blocks .

Importing specific attributes using 'from module import x, y' introduces 'x' and 'y' directly into the global namespace, which can prevent namespace clutter compared to importing the entire module, thus improving code readability and efficiency .

If a module has already been loaded, Python uses the existing module object and does not execute the module's code again. If a module is not found in sys.modules, Python creates a new module object, adds it to sys.modules, and executes its code .

Correct naming conventions are crucial to avoid conflicts with Python's standard library and other modules. Names like 'sys.py', 'math.py', and 'time.py' should be avoided as they clash with existing standard libraries .

You might also like