LearnFrenzy
Sign in Take a free test

Python Programming · Topic 15 of 25

Modules and Packages Questions and Concepts

  • 4 exercises
  • 50 questions
  • ~60 min
  • Answers explained

Interview Questions

Modules and Packages · Study material

Modules and Packages – Interview Questions

1. What is a module in Python?

A .py file containing definitions and statements that can be imported.

2. What is a package?

A directory of modules (usually with __init__.py) that can contain sub-packages.

3. Difference between import x and from x import y?

import x binds the module name and you write x.y; from x import y binds y directly in your namespace.

4. What happens when a module is imported twice?

Its code runs only the first time; afterwards the cached module from sys.modules is returned.

5. How does Python find a module?

It checks sys.modules, then searches the directories in sys.path in order (script directory, PYTHONPATH, standard library, site-packages).

6. What does if __name__ == "__main__" do?

Runs the code under it only when the file is executed directly, not when it is imported.

7. What is __init__.py for?

It marks a regular package and runs when the package is imported; it can initialise the package and re-export names.

8. What are relative imports?

Imports relative to the current package, such as from . import utils or from ..models import User; they only work inside packages.

9. What is __all__?

A list of public names that from module import * imports.

10. How do you reload a module?

importlib.reload(module).

11. What is a virtual environment?

An isolated Python environment with its own site-packages, created with python -m venv, so projects can use different package versions.

12. Why is from module import * discouraged?

It hides where names come from and can silently overwrite existing names, including built-ins.

13. What does dir(module) return?

A sorted list of the names defined in the module.

14. How do you import a module whose name is in a string?

importlib.import_module(name).

15. What is the difference between ImportError and ModuleNotFoundError?

ModuleNotFoundError (a subclass) means the module itself was not found; ImportError also covers a module that exists but lacks the requested name.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Python Programming

Why it matters

  • Python is the most popular first language and the default for data science, AI, automation and scripting
  • Campus coding rounds and technical interviews increasingly allow – or prefer – Python
  • "Predict the output" questions on lists, strings, dictionaries and mutable defaults are a favourite in written tests
  • Clean Python with comprehensions, generators and OOP is what product companies expect from freshers

Who it is for

  • School students (Class 11–12 computer science with Python) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, coding tests and Python interviews
  • Anyone moving into data science, AI or automation who needs strong Python fundamentals

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs: syntax errors, runtime exceptions and logical bugs
  • Coding problems with solutions and real output, every one run on Python 3 – open any example in the online Python compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture tests
  • Python interviews for freshers
  • Data science interviews
  • CBSE / university practicals
  • GATE CS

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, errors, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links