LearnFrenzy
Sign in Take a free test

Python Programming · Topic 18 of 25

OOP in Python Questions and Concepts

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

Programs & Examples

OOP in Python · Study material

A small library system

Two cooperating classes: Book stores its state and describes itself with __repr__; Library keeps books in a dict and applies the borrowing rules.

class Book:
    def __init__(self, title, author):
        self.title, self.author = title, author
        self.borrowed_by = None

    def __repr__(self):
        status = f"borrowed by {self.borrowed_by}" if self.borrowed_by else "available"
        return f"{self.title} ({status})"

class Library:
    def __init__(self):
        self.books = {}

    def add(self, book):
        self.books[book.title] = book

    def borrow(self, title, member):
        book = self.books.get(title)
        if book is None:
            return f"{title}: not in the library"
        if book.borrowed_by:
            return f"{title}: already borrowed"
        book.borrowed_by = member
        return f"{title}: issued to {member}"

lib = Library()
lib.add(Book("Python 101", "Rao"))
lib.add(Book("SQL Basics", "Iyer"))
for title, member in [("Python 101", "Asha"), ("Python 101", "Ravi"), ("Java", "Dev")]:
    print(lib.borrow(title, member))
print(list(lib.books.values()))

Output

Python 101: issued to Asha
Python 101: already borrowed
Java: not in the library
[Python 101 (borrowed by Asha), SQL Basics (available)]

Properties with validation

The encapsulation example on the Introduction tab protects a balance with a property setter and shows name mangling of a __pin attribute.

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