OOP - Short - Questions Full
OOP - Short - Questions Full
What is OOP?
ANS: It is a technique in which we visualize our programming problems in the form of objects and their
interactions as happens in real life.
1. Encapsulation: This characteristic refers to bundling data (attributes) and the methods
(behaviors) that operate on the data into a single unit or object. Encapsulation allows
objects to hide their internal state and only expose necessary functionalities through a
public interface.
2. Abstraction: This is the process of hiding complex implementation details and showing
only the essential features of an object. It helps in managing complexity by reducing the
details that the user needs to focus on.
3. Inheritance: This allows one class (the derived or child class) to inherit properties and
behaviors from another class (the base or parent class). Inheritance promotes code reuse
and establishes a natural hierarchy between classes.
4. Polymorphism: It allows objects of different classes to respond to the same method call
in different ways. Polymorphism enables flexibility and dynamic behavior in a program.
1. Objects: Fundamental units that represent real-world entities or concepts. Objects have
attributes (data) and behaviors (methods).
2. Classes: Blueprints or templates for creating objects. A class defines the structure and
behavior that the objects created from it will have.
3. Encapsulation: The bundling of data (attributes) and methods (behaviors) that
manipulate the data within a single unit, i.e., an object. It hides the internal details of an
object and only exposes the necessary parts.
4. Abstraction: The process of simplifying complex systems by focusing on the relevant
features and hiding unnecessary details.
5. Inheritance: A mechanism by which one class (child class) inherits attributes and
methods from another class (parent class). This promotes code reuse and establishes
hierarchical relationships.
6. Polymorphism: The ability of different objects to respond to the same method call in
their own way, providing flexibility and dynamic behavior.
2. Something conceptual (that can be apprehended intellectually for example time, date and so
on…).
An object has,
1. State (attributes)
What Is A Class?
In OOP we create a general sketch for each kind of objects and then we create different instances
using this sketch we call this sketch or prototype or map as “class”. All objects of same kind exhibit
identical characteristics (information structure and behavior) however they have data of their
own.
What Is A Meta-Class?
A Meta-Class in Object-Oriented Programming (OOP) is a class that defines the behavior and
structure of other classes. In simple terms, just like classes define the blueprint for objects, a
meta-class defines how classes behave.
What Is Inheritance?
Inheritance is a key feature of object-oriented programming that allows a class (called a derived or
child class) to inherit attributes and behaviors from another class (called a base or parent class).
The derived class can have additional attributes and behaviors that are specific to it.
What is Abstraction?
Abstraction in Object-Oriented Programming is the process of simplifying complex systems by
focusing only on the essential details relevant to the problem at hand, while ignoring the
irrelevant ones. It helps manage complexity by presenting only necessary information and hiding
the rest.
What is Inheritance?
Inheritance in Object-Oriented Programming is a mechanism where a subclass acquires properties
and behaviors (attributes and methods) from a parent (base) class, promoting code reuse and
hierarchical relationships.
What is a constructor?
A constructor is a special method in a class that is automatically called when an object of the class
is created. It initializes the object's data members and sets the object to a valid state
What is a destructor?
A destructor is a special method in a class that is automatically called when an object is destroyed.
It is used to release resources, such as memory or file handles, that were allocated to the object
during its lifetime
What is a pointer?
A pointer is a variable that stores the memory address of another variable. In programming, it is
used to directly access and manipulate the value stored at a specific memory location
Explain what type of copy the default assignment operator "=" does when
applied to objects. (shallow copy or deep copy)
The default assignment operator "=" in C++ performs a shallow copy. It copies the values of an
object's data members, but for pointers, it copies only the addresses, not the data they point to.
This can lead to multiple objects sharing the same memory location
int meters;
public:
Distance(int m) : meters(m) {}
operator int() {
};
int main() {
Distance d(5);
int cm = d;
return 0;
Simple Association: A basic relationship between two objects where both can exist
independently.
Aggregation: A special form of association where one object (the whole) contains or is
made up of other objects (the parts), but the parts can still exist independently.
Explanation:
Simple Association is a weak relationship between objects. For example, a Student and
a Classroom have a simple association, meaning a student can exist without a specific
classroom, and the classroom can exist without a specific student.
Aggregation is a stronger relationship. It represents a "whole-part" relationship. For
instance, a Library and Books have an aggregation relationship where a library contains
books, but the books can exist outside the library as well. The parts (books) are not
destroyed when the whole (library) is destroyed
class Student {
int rollNo;
public:
Student(int r) : rollNo(r) {}
};
int main() {
Student s1(101);
s1.showRollNo();
return 0;
public:
Complex result;
return result;
};
Simplification: It helps to manage complexity by hiding unnecessary details and showing only
the relevant aspects of the object for the task at hand.
Problem-solving: Abstraction allows us to focus on the features of an object that are directly
related to the problem we are solving. For example, when building a school system, we only care
about a student's academic information (e.g., roll number, grades) and not about unrelated
details like their personal life.
Modularity: Abstraction helps to create a clear separation of concerns, where different parts of
the system only interact through well-defined interfaces without worrying about how other
parts work internally.
What is IS-A relationship, show how it is implemented using c++ code (you
do not have to give the detailed code simply show in a single line or two
how it will be implemented).
The IS-A relationship is demonstrated through inheritance in C++. For instance:
class Animal {}; // Base class
Describe the way to declare a template class as a friend class of any other
class?
The syntax for this;
class MyClass {
friend class FriendClass<int>;
What is the purpose of template parameter?
A template parameter allows classes or functions to operate with any data type, making the code
flexible and reusable. It enables writing generic code that can be applied to multiple types without
modification.
Can we use compiler generated default assignment operator in case our class
is using dynamic memory? Justify your answer.
No, we cannot use the compiler-generated default assignment operator when our class uses
dynamic memory because it performs a shallow copy, leading to issues like double deletion or
memory leaks. A custom assignment operator is needed for deep copying.
1. Exception Handling.
2. Error Codes
3. Assertions
Here's a table indicating which member functions of the Base and Derived classes are accessible
using a Derived pointer:
Given are two classes A and B. class B is inherited from class A. Write a
code snippet(for main function) that polymorphically call the method of
class B. Also what changes do you suggest in the given code segment
that are required to call the class B method polymorphically.
class A
{
public:
void method() { cout"A's method \n"; }
};
class B : public A
{
public:
void method() { cout"B's method\n"; }
};