1. LPU MCA Semester 1
  2. CAB106
  3. Unit 1

CAB106 · Unit 1: Python and OOP introduction

Programming paradigms: procedural versus object-oriented programming and advantages of OOP

9 practice questions

Key points

  • Procedural code is functions acting on separate data
  • OOP bundles state and behaviour into objects
  • The pillars are encapsulation, abstraction, inheritance, polymorphism
  • OOP aids reuse and maintenance, not speed

In procedural programming a program is a sequence of procedures (functions) that operate on data held separately, often in shared variables. In object-oriented programming (OOP) data and the functions that act on it are bundled into objects created from classes. Other paradigms exist too, such as functional programming (pure functions) and logic programming (rules and facts). Python is multi-paradigm and lets you mix these styles.

AspectProceduralOOP
Main unitFunctionClass and object
DataOften shared or globalKept inside objects
ReuseCall functionsInheritance and composition
SafetyAny code can change dataEncapsulation controls access

The four pillars of OOP are encapsulation (hiding data behind methods), abstraction (exposing only what is needed), inheritance (a subclass reuses a parent class) and polymorphism (one interface, many behaviours). Advantages include reuse, easier maintenance and a closer match to real-world entities such as Book, Account or Player. Each object keeps its own state, so two objects of one class are independent.

Exam trap: OOP does not make code run faster or remove the need for design. Its benefit is organisation, reuse and maintainability.

Try two questions

Question 1 · Tier 4, Analyse

Many functions read and change one global data structure. What problem does OOP encapsulation mainly reduce?

  1. Any function can alter the data unexpectedly, so bugs are hard to trace to their source
  2. The program can no longer be split into more than one source file
  3. Variables lose their type information and cannot be printed at run time
  4. Functions cannot call each other, so recursion becomes impossible
Show the answer

A. Encapsulation limits who can change the data, so faults are easier to locate.

Why not B: Global data does not stop code being split across files.

Why not C: Types and printing are not affected by global state.

Why not D: Recursion works with or without global data.

Question 2 · Tier 3, Apply

In a game, Enemy and Boss share health and attack code, but Boss adds special moves. Which OOP feature best removes the duplicated code?

  1. Inheritance, so the shared code lives in a parent class
  2. Garbage collection, which frees unused objects
  3. Static typing, which fixes variable types early
  4. Recursion, which lets functions call themselves
Show the answer

A. A subclass reuses and extends the parent's attributes and methods.

Why not B: Garbage collection manages memory, not shared code.

Why not C: Python is not statically typed, and typing does not share code.

Why not D: Recursion repeats a function, not shared class behaviour.