- LPU MCA Semester 1
- CAB106
- 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.
| Aspect | Procedural | OOP |
|---|---|---|
| Main unit | Function | Class and object |
| Data | Often shared or global | Kept inside objects |
| Reuse | Call functions | Inheritance and composition |
| Safety | Any code can change data | Encapsulation 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
Many functions read and change one global data structure. What problem does OOP encapsulation mainly reduce?
- Any function can alter the data unexpectedly, so bugs are hard to trace to their source
- The program can no longer be split into more than one source file
- Variables lose their type information and cannot be printed at run time
- 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.
In a game, Enemy and Boss share health and attack code, but Boss adds special moves. Which OOP feature best removes the duplicated code?
- Inheritance, so the shared code lives in a parent class
- Garbage collection, which frees unused objects
- Static typing, which fixes variable types early
- 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.