LearnFrenzy
Sign in Take a free test

Modules and Packages · Exercise 3 of 4

Modules and Packages Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Python program has an import mistake: an exception at run time or a logical bug such as missing a __main__ guard or relying on a stale cached module. Choose the option that names the real problem.

Question 1 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
import maths
print(maths.sqrt(25))
A AttributeError: sqrt
B ModuleNotFoundError: there is no module called maths; the standard module is math
C It prints 5.0
D SyntaxError
Question page & discussion
Question 2 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
from math import square_root
print(square_root(49))
A ModuleNotFoundError: math
B ImportError: math has no name square_root; the function is sqrt
C It prints 7.0
D NameError
Question page & discussion
Question 3 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
import math
print(sqrt(16))
A It prints 4.0
B NameError: import math binds only math; call math.sqrt(16) or use from math import sqrt
C ImportError
D AttributeError
Question page & discussion
Question 4 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
import math
print(math.squareroot(4))
A ImportError
B NameError: math
C It prints 2.0
D AttributeError: module math has no attribute squareroot; use math.sqrt
Question page & discussion
Question 5 Medium

The program below stops with an error. Which statement correctly describes the problem?

main.py
import math
math = 3.14                  # "the value of pi"
print(math.sqrt(4))
A It prints 2.0
B AttributeError: the name math now refers to a float, which has no sqrt; use another variable name
C ImportError
D TypeError
Question page & discussion
Question 6 Hard

The program below stops with an error. Which statement correctly describes the problem?

main.py
from os import *

f = open("notes.txt", "w")
f.write("hello")
A It writes hello to notes.txt
B PermissionError
C ModuleNotFoundError: os
D TypeError: import * brought in os.open, which takes integer flags and has replaced the built-in open(); import only what you need
Question page & discussion
Question 7 Medium

The program below stops with an error. Which statement correctly describes the problem?

main.py
import math.sqrt
print(math.sqrt(9))
A It prints 3.0
B ModuleNotFoundError: import a.b expects b to be a module inside a package; math is not a package and sqrt is a function – use from math import sqrt
C SyntaxError
D AttributeError
Question page & discussion
Question 8 Hard

This file is run directly with python main.py. It stops with an error. Which statement correctly describes the problem?

main.py
from . import helpers

print(helpers.greet())
A ModuleNotFoundError: helpers
B SyntaxError: . is not a name
C It prints a greeting
D ImportError: a script run directly has no parent package, so relative imports cannot work; use import helpers, or run it as part of a package with python -m
Question page & discussion
Question 9 Medium

The program should print only 10. Which statement correctly identifies the bug in the module it imports?

main.py
import sys, tempfile, pathlib

folder = pathlib.Path(tempfile.mkdtemp())
(folder / "lf_calc.py").write_text(
    "def add(a, b):\n    return a + b\n"
    "print('Testing add:', add(2, 3))\n")      # the module's own quick test
sys.path.insert(0, str(folder))

from lf_calc import add
print(add(4, 6))
A from … import runs every function
B add is called twice by Python
C print inside a module is always shown twice
D The module's test line is top-level code, so it runs on import; put it under if __name__ == "__main__":
Question page & discussion
Question 10 Hard

The settings file is changed to VALUE = 2 and imported again, but the program still prints 1 twice. Which statement correctly identifies the bug?

main.py
import sys, tempfile, pathlib

folder = pathlib.Path(tempfile.mkdtemp())
settings = folder / "lf_settings.py"
settings.write_text("VALUE = 1\n")
sys.path.insert(0, str(folder))

import lf_settings
print(lf_settings.VALUE, end=" ")
settings.write_text("VALUE = 2\n")       # edit the module file
import lf_settings                       # "import it again"
print(lf_settings.VALUE)
A write_text does not change the file
B VALUE is a constant and cannot change
C The second import returns the module cached in sys.modules without reading the file again; call importlib.reload(lf_settings)
D The folder is removed from sys.path
Question page & discussion
Up next · Exercise 4Modules and Packages Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links