Modules, Packages & Libraries MCQs for the NSCT
Modules, packages and libraries are how code is split into reusable units and shared: a module is one file of code, a package groups modules, and a library is a collection of ready-made code you import, installed by a package manager. This is the twelfth of 16 topics in the Programming (C++/Java/Python) area, which carries 10% of the NSCT according to HEC's Areas of Competencies.
Expect questions on what an import statement binds, how namespaces prevent name clashes, why a module runs only once, and what pip, npm and Maven do. Several traps come from Python's import system, so trace imports as carefully as you trace exceptions.
Imports
An import makes code from another module usable in the current file. The three languages do it very differently:
| Language | Unit | Statement | What happens |
|---|---|---|---|
| Python | module (.py file), package (folder) |
import math, from math import sqrt |
Module runs once, then is cached in sys.modules |
| Java | class inside a package | import java.util.ArrayList; |
Lets you use the short class name; java.lang is automatic |
| C++ | header file | #include <vector> |
Preprocessor pastes the header's text in before compiling |
In Python, import math binds one name, math. from math import sqrt binds sqrt directly, and import numpy as np binds an alias. A module's top-level code runs on its first import only; later imports reuse the cached module.
print("loading") # file counter.py
count = 0
import counter # file main.py: prints "loading"
import counter # cached: nothing printed
Exam angle: from config import level copies the current binding into your namespace. If config later rebinds level, your name still holds the old value, while config.level shows the new one.
Namespaces
A namespace maps names to objects, so the same name can mean different things in different places. Each Python module has its own global namespace. C++ creates namespaces explicitly, and Java uses packages.
namespace A { int v = 1; }
namespace B { int v = 2; }
int v = 3;
// A::v is 1, B::v is 2, ::v is 3
using namespace std; saves typing in a .cpp file but should never go in a header, because it affects every file that includes it. In Java, two wildcard imports that both supply a class called Date (from java.util and java.sql) make the name ambiguous; use a single-type import or the fully qualified name.
Python's from module import * dumps public names into your namespace and can silently overwrite your own. If the module defines __all__, only the names listed are imported.
Exam angle: a script named random.py or math.py shadows the standard library module, because Python searches the script's own folder first.
Packages and libraries
A Python package is a directory of modules. A regular package has an __init__.py, which runs on first import. Inside a package, a relative import such as from .pricing import tax works only when the file is imported as part of the package, not when it is run directly as a script.
A library is reusable compiled or source code. In C and C++ it is linked in one of two ways:
Static library (.lib, .a) |
Dynamic library (.dll, .so) |
|
|---|---|---|
| When joined | At link time, copied into the executable | Loaded at run time |
| Executable size | Larger | Smaller |
| Library update | Rebuild every program | Replace one shared file |
Exam angle: include guards stop a header being processed twice in one .cpp file. They do not stop two .cpp files from each defining the same non-inline function, which is a link error.
Package managers
A package manager downloads libraries, resolves their dependencies and records versions.
| Ecosystem | Tool | Dependency file | Lock or pin |
|---|---|---|---|
| Python | pip (plus venv) |
requirements.txt, pyproject.toml |
Exact == pins |
| JavaScript | npm | package.json |
package-lock.json |
| Java | Maven, Gradle | pom.xml, build.gradle |
Exact versions |
| C++ | vcpkg, Conan | vcpkg.json, conanfile.txt |
Lockfiles |
Most use semantic versioning, MAJOR.MINOR.PATCH, where MAJOR changes break compatibility. In npm, ^1.4.2 allows anything below 2.0.0, while ~1.4.2 allows anything below 1.5.0. A virtual environment gives each Python project its own installed packages, so two projects can use different versions.
Exam angle: pip install -r requirements.txt installs from the file; pip freeze > requirements.txt creates it.
Common traps
- Believing a module's code runs every time it is imported. It runs once per process.
- Thinking
__all__makes other names private. It only affectsimport *. - Assuming include guards fix multiple-definition linker errors across
.cppfiles. - Confusing the caret (
^) and tilde (~) ranges in npm. - Assuming a Java member with no modifier is public. It is package-private.
- Running
python pkg/module.pyon a file that uses relative imports. Usepython -m pkg.module.
Worked examples
1. Understanding. Which Java package's classes can be used without any import statement?
Answer: java.lang, which includes String, Math and System.
2. Application. In npm, does the range ^0.2.3 accept version 0.3.0?
Answer: no. With a leading zero the caret locks the first non-zero part, so ^0.2.3 means at least 0.2.3 and below 0.3.0.
3. Analysis. a.py starts with import b, and b.py contains from a import helper, where helper is defined lower down in a.py. Why does import a fail?
Answer: it is a circular import. When b runs, a is only partly executed and helper does not exist yet, so Python raises ImportError. Move the shared code into a third module.
Practise
The quiz below has 100 questions on this topic, filterable by tier and difficulty.
Practise: 100 questions
FAQs
What is the difference between a module, a package and a library?
A module is one file of code. A package is a folder that groups related modules under one name. A library is the broader idea: reusable code written by someone else that you call, which may be one module, many packages, or compiled .dll/.so files. A framework is a library that calls your code.
What is the difference between import and #include?
Python's and Java's imports work on names: they bind a module or class name in your file, and the module itself is compiled separately. C++'s #include is plain text substitution by the preprocessor, so the header's declarations become part of your source file before compilation. C++20 adds modules with import, but most course code still uses headers.
Why does my import work in the IDE but fail in the terminal?
Python finds modules through sys.path, which starts with the script's folder and then includes the active environment's site-packages. If the terminal uses a different interpreter or virtual environment, or you run from a different folder, the package may be missing. Check python -m pip list in the same terminal.
Should I commit package-lock.json?
Yes, for applications. The lockfile records the exact version of every installed package, including indirect ones, so npm ci reproduces the same tree on every machine. Without it, a caret range can resolve to a newer version months later. Our software development practices page covers version control habits.
Next step
Move on to Advanced Programming Concepts MCQs, see all topics on the Programming MCQs hub, or test yourself in an NSCT mock test.