Modules, Packages & Libraries MCQs for the NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. In Python, what is a module?

    1. A folder of .py files that must contain setup.py
    2. A single .py file whose functions, classes and variables can be imported by other code
    3. A compiled binary that only the interpreter can load
    4. Any function defined with the def keyword
    Show answer

    Answer: B. A single .py file whose functions, classes and variables can be imported by other code

    A module is one Python source file (or an extension module); its name is the file name without .py. A package is a directory of modules. setup.py relates to distributing a package, not to what a module is.

  2. Which Java statement lets you refer to java.util.ArrayList simply as ArrayList?

    1. import java.util.ArrayList;
    2. #include <java/util/ArrayList>
    3. using java.util.ArrayList;
    4. package java.util.ArrayList;
    Show answer

    Answer: A. import java.util.ArrayList;

    Java's import statement brings a class name from another package into scope so you can use its simple name. #include is the C/C++ preprocessor, using is C++ and C#, and package declares which package the current file belongs to.

  3. In C++, what does `#include <vector>` do?

    1. It downloads the vector library from the internet
    2. The preprocessor copies the contents of the vector header into the source file before compilation
    3. It links the compiled vector library at run time
    4. It imports only the names used later in the file, like Python's from-import
    Show answer

    Answer: B. The preprocessor copies the contents of the vector header into the source file before compilation

    #include is a preprocessor directive: it textually inserts the named header into the translation unit before the compiler proper runs. Linking is a separate, later step, and C++ has no package download built into the language.

  4. Which tool installs third-party Python packages from the Python Package Index (PyPI)?

    1. npm
    2. Maven
    3. pip
    4. javac
    Show answer

    Answer: C. pip

    pip is Python's standard package installer, for example `pip install requests`. npm manages JavaScript packages, Maven manages Java dependencies and builds, and javac is the Java compiler.

  5. Which Java package is imported automatically into every source file?

    1. java.util
    2. java.io
    3. java.base
    4. java.lang
    Show answer

    Answer: D. java.lang

    Classes in java.lang, such as String, Math, Integer and System, can be used without an import. java.util (ArrayList, HashMap) and java.io must be imported explicitly. java.base is a module name in the Java module system, not a package.

  6. What is the main purpose of a namespace in C++?

    1. To group names under a named scope so identifiers from different libraries do not collide
    2. To allocate memory for global variables
    3. To make a class's members private
    4. To speed up compilation by caching headers
    Show answer

    Answer: A. To group names under a named scope so identifiers from different libraries do not collide

    A namespace such as std or a library's own namespace qualifies names (std::sort versus mylib::sort), so two libraries can use the same identifier without a clash. Access control is done with private/public, not namespaces.

  7. What does the `if __name__ == "__main__":` check do in a Python file?

    1. Runs the guarded code only when the file is imported
    2. Runs the guarded code only when the file is executed directly, not when it is imported as a module
    3. Marks the file as the package's entry point for pip
    4. Prevents the module from being imported twice
    Show answer

    Answer: B. Runs the guarded code only when the file is executed directly, not when it is imported as a module

    When a file is run as a script, Python sets its __name__ to "__main__"; when it is imported, __name__ is the module name. The guard lets a file serve both as a reusable module and as a script. Duplicate imports are prevented by the sys.modules cache, not by this check.

  8. In Python, which file turns a directory into a regular (non-namespace) package and runs when the package is first imported?

    1. __main__.py
    2. setup.py
    3. __init__.py
    4. requirements.txt
    Show answer

    Answer: C. __init__.py

    A regular package is a directory containing __init__.py, which runs on first import and can define the package's public names. Since Python 3.3 a directory without it can still be imported as a namespace package. __main__.py runs with `python -m package`; setup.py and requirements.txt are packaging files.

  9. What is the difference between a static library and a dynamic (shared) library in C/C++?

    1. A static library is loaded at run time; a dynamic library is copied in at compile time
    2. Static libraries can only contain variables, dynamic libraries only functions
    3. A static library's code is copied into the executable at link time; a dynamic library (.dll/.so) is loaded at run time and can be shared by programs
    4. There is no difference except the file extension
    Show answer

    Answer: C. A static library's code is copied into the executable at link time; a dynamic library (.dll/.so) is loaded at run time and can be shared by programs

    Linking a static library (.lib/.a) embeds the used object code, making a larger, self-contained executable. A dynamic library (.dll on Windows, .so on Linux) stays a separate file loaded when the program starts or on demand, so several programs share one copy and it can be updated independently.

  10. In a Java Maven project, what is pom.xml used for?

    1. Storing the compiled .class files
    2. Listing the Python packages the project needs
    3. Configuring the operating system's environment variables
    4. Declaring the project's dependencies (by groupId, artifactId and version) and its build configuration
    Show answer

    Answer: D. Declaring the project's dependencies (by groupId, artifactId and version) and its build configuration

    Maven reads pom.xml (Project Object Model) to download dependencies from repositories such as Maven Central and to run build phases like compile, test and package. Gradle plays the same role with build.gradle. Python's equivalents are requirements.txt or pyproject.toml.

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.