0% found this document useful (0 votes)
31 views7 pages

Top Oops Questions

Object-Oriented Programming (OOP) is a programming paradigm that focuses on objects and their interactions, promoting code reusability, modularity, and maintainability. The four main pillars of OOP are encapsulation, abstraction, inheritance, and polymorphism, which help manage complexity in software design. Key concepts include classes and objects, constructors and destructors, and the importance of low coupling and high cohesion in software design.

Uploaded by

rockytan2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views7 pages

Top Oops Questions

Object-Oriented Programming (OOP) is a programming paradigm that focuses on objects and their interactions, promoting code reusability, modularity, and maintainability. The four main pillars of OOP are encapsulation, abstraction, inheritance, and polymorphism, which help manage complexity in software design. Key concepts include classes and objects, constructors and destructors, and the importance of low coupling and high cohesion in software design.

Uploaded by

rockytan2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1. What is Object-Oriented Programming (OOPs)?

🤔
OOPs is a programming paradigm or methodology based on the concept of "objects".
Instead of writing programs as a sequence of commands, we design software around data
(objects) and the functions (methods) that operate on that data. This approach helps in
creating more structured, reusable, and manageable code.

2. Why do we need OOPs? What are its main advantages? ✨


We use OOPs to manage the complexity of large software systems. Procedural programming
becomes difficult to maintain as projects grow.

Key Advantages:
●​ Reusability: Inheritance allows us to reuse code from existing classes.
●​ Modularity: Encapsulation keeps an object's state and behavior together, making code
modular and easy to work with.
●​ Maintainability: OOPs code is easier to understand, modify, and debug.
●​ Security: Data hiding (through Encapsulation) protects internal data from accidental
modification.
●​ Real-world Modeling: It allows us to model real-world entities (like a Car or Employee)
more effectively.

3. What are the four main pillars of OOPs? 🏛️


The four fundamental principles of OOPs are:
1.​ Encapsulation
2.​ Abstraction
3.​ Inheritance
4.​ Polymorphism

4. Explain a Class and an Object with an analogy. blueprints

●​ A Class is a blueprint for creating objects. It defines properties (variables) and


behaviors (methods) but doesn't hold any data itself. For example, the architectural plan
for a house is a class.
●​ An Object is a real-world instance of a class. It has its own state (data) and behavior.
Using the analogy, an actual house built from the plan is an object. You can build many
houses (objects) from one plan (class).

5. What is Encapsulation? Why is it important? 💊


Encapsulation is the bundling of data (attributes) and the methods that operate on that
data into a single unit (a class). It also involves data hiding, where the internal state of an
object is protected from outside access.

Why it's important: It provides security by preventing direct, uncontrolled access to data.
You interact with the data through controlled public methods (getters and setters), which can
include validation logic. Think of a bank account's balance—you can't change it directly; you
must use deposit() or withdraw().

6. What is Abstraction? How does it differ from Encapsulation? 🚗


Abstraction is the concept of hiding complex implementation details and showing only the
essential features to the user. It focuses on what an object does, not how it does it. A car's
dashboard is a perfect example: you use the steering wheel and pedals without needing to
know how the engine works.

Abstraction vs. Encapsulation:


| Feature | Abstraction | Encapsulation |
| :--- | :--- | :--- |
| Focus | Hiding complexity at the design level. | Hiding data at the implementation level. |
| Purpose | To simplify the interface for the user. | To protect an object's internal state. |
| How | Using abstract classes and interfaces. | Using access specifiers (private, public). |

7. What is Inheritance? 👪
Inheritance is a mechanism where a new class (child/derived class) inherits attributes and
methods from an existing class (parent/base class). This promotes code reusability and
creates an "Is-A" relationship (e.g., a Dog is an Animal).

8. What are the different types of inheritance?

1.​ Single Inheritance: A child class inherits from a single parent class. (A -> B)
2.​ Multilevel Inheritance: A class inherits from a child class, forming a chain. (A -> B -> C)
3.​ Hierarchical Inheritance: Multiple child classes inherit from a single parent class. (A ->
B, A -> C)
4.​ Multiple Inheritance: A child class inherits from multiple parent classes. (C++ supports
this, but Java uses interfaces to achieve a similar result).
5.​ Hybrid Inheritance: A combination of two or more inheritance types.

9. What is Polymorphism? 🎭
Polymorphism means "many forms." It's the ability of a method or object to take on different
behaviors. It allows us to perform a single action in different ways. For example, the + operator
can add integers or concatenate strings.

10. What are the types of Polymorphism?

There are two main types:


1.​ Compile-time (Static) Polymorphism: The decision is made by the compiler at compile
time.
○​ Method Overloading: Having multiple methods with the same name but different
parameters in the same class.
2.​ Run-time (Dynamic) Polymorphism: The decision is made at runtime.
○​ Method Overriding: A child class provides a specific implementation for a method
that is already defined in its parent class. This requires virtual functions (in C++).

11. Differentiate between Method Overloading and Method


Overriding.
Basis for Comparison Method Overloading Method Overriding

Purpose Use the same method Provide a specific


name for different tasks. implementation in a child
class.

Parameters Must be different (type or Must be the same.


number).

Scope Occurs within the same Occurs between a parent


class. and child class.

Polymorphism Type Compile-time (Static). Run-time (Dynamic).

Mechanism Just define multiple Requires inheritance and


methods with the same virtual functions (in C++).
name.

12. What is a Constructor? 🏗️


A constructor is a special method that's automatically called when an object of a class is
created. Its main purpose is to initialize the object's attributes. It has the same name as the
class and no return type.

13. What is a Destructor? Do all languages have them? 💥


A destructor is a special method that's automatically called when an object is destroyed or
goes out of scope. Its purpose is to release resources (like memory or file handles) that the
object was using.

Not all languages have explicit destructors.


●​ C++ has destructors (~ClassName()) for deterministic memory management.
●​ Java and C# use a Garbage Collector to automatically manage memory, so they don't
have explicit destructors. They have a finalize() method (Java) or finalizer (C#), but its
execution is not guaranteed.

14. What is the this keyword/pointer?

The this pointer is a keyword that refers to the current instance of the class. It's used to:
●​ Access member variables when there's a naming conflict with local variables (e.g.,
this->name = name;).
●​ Pass the current object as a parameter to another method.

15. What is an abstract class?

An abstract class is a class that cannot be instantiated (you can't create an object from it).
It's meant to be used as a base class for other classes. It can contain both regular methods
and abstract methods (methods with no implementation). Any class that inherits from an
abstract class must provide an implementation for all its abstract methods.

16. What is an interface? How is it different from an abstract class?

An interface is a completely abstract type that contains only abstract method signatures
and constants. It defines a "contract" that a class must follow.

Feature Abstract Class Interface

Methods Can have both abstract Can only have abstract


and non-abstract methods (by default in
(concrete) methods. older Java versions).

Variables Can have any type of Variables are public static


variable (final, non-final, final by default (constants).
static, etc.).
Inheritance A class can inherit from A class can implement
only one abstract class. multiple interfaces.

Purpose To provide a common base To define a contract for


with some default behavior that multiple
implementation. unrelated classes can
share.

17. What are virtual functions?

A virtual function (in C++) is a member function in a base class that you expect to be
redefined (overridden) in derived classes. When you call a virtual function through a base
class pointer pointing to a derived class object, the system uses dynamic dispatch to call the
correct derived class version at runtime. This is the key mechanism for achieving run-time
polymorphism.

18. What is a static keyword in OOPs?

The static keyword can be applied to both variables and methods.


●​ Static Variable (Class Variable): There is only one copy of this variable shared among
all objects of the class. It belongs to the class itself, not to any individual object.
●​ Static Method (Class Method): A method that belongs to the class rather than an
object. It can be called directly using the class name (ClassName::method()) without
creating an object. It can only access static variables.

19. Can you call a virtual function from a constructor?

You can, but it's a bad idea and doesn't work as you might expect. When a constructor is
executing, the object is not yet fully formed. Specifically, the derived class part has not been
constructed yet. Therefore, if you call a virtual function from a base class constructor, it will
always call the base class's version of the function, not the overridden version in the derived
class. This defeats the purpose of polymorphism.
20. Explain Coupling and Cohesion.

These are two important software design principles.


●​ Coupling refers to the degree of dependency between different modules or classes.
Low coupling is desirable, as it means changes in one class will have minimal impact on
others, making the system easier to maintain.
●​ Cohesion refers to how well the elements within a single module or class belong
together. High cohesion is desirable, as it means a class is designed to do one specific
job well, making it more understandable and reusable.

In short: Aim for Low Coupling and High Cohesion.

You might also like