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