Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views15 pages

OOP Question Bank Answer

The document is a question bank on Object-Oriented Programming (OOP) covering various topics such as the differences between Procedural and Object-Oriented Programming, applications of OOP, features like encapsulation and inheritance, and concepts of classes and objects. It includes example programs in C++ for practical understanding, along with explanations of constructors, destructors, access specifiers, and inheritance types. Additionally, it discusses static vs non-static members and provides a program for managing staff data.

Uploaded by

amangaud25
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)
7 views15 pages

OOP Question Bank Answer

The document is a question bank on Object-Oriented Programming (OOP) covering various topics such as the differences between Procedural and Object-Oriented Programming, applications of OOP, features like encapsulation and inheritance, and concepts of classes and objects. It includes example programs in C++ for practical understanding, along with explanations of constructors, destructors, access specifiers, and inheritance types. Additionally, it discusses static vs non-static members and provides a program for managing staff data.

Uploaded by

amangaud25
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/ 15

OOP Question Bank Answer

Q1. Differentiate between POP and OOP.(4 M)


Ans.

Q2. Write any four Apllications of OOP.(2 M)


Ans.
1. Game Development: OOP allows for the creation of game elements (characters,
items) as objects, each with properties and methods, simplifying complex game logic
and enabling code reuse.
2. Graphical User Interfaces (GUIs): OOP is used to design interactive components
(buttons, text boxes) as objects, making it easier to manage and update the user
interface.
3. Web Development: OOP helps in building dynamic web applications by organizing
data models, views, and controllers as objects, enhancing code maintainability and
scalability.
4. Simulation and Modeling: OOP is applied in simulations (like weather forecasting)
where real-world entities are represented as objects, allowing for accurate and
flexible modeling of complex systems.

Contact Details:
7972423160
Q3. State any four Features of OOP .(4M)
Ans.
1. Encapsulation:
• Encapsulation is the concept of bundling data (attributes) and methods
(functions) that operate on that data into a single unit called an object. This
helps in hiding the internal state of the object and only exposing a controlled
interface for interaction. It enhances data security and reduces complexity by
preventing external interference.
2. Inheritance:
• Inheritance allows a new class (called a subclass or derived class) to inherit
properties and methods from an existing class (called a superclass or base
class). This promotes code reuse and establishes a hierarchical relationship
between classes. For example, a "Car" class can inherit from a "Vehicle" class,
gaining its attributes and behaviors while adding its own specific features.
3. Polymorphism:
• Polymorphism enables objects of different classes to be treated as objects of
a common superclass. It allows methods to be defined in multiple forms. For
instance, a function can accept different types of objects and execute the
appropriate method based on the object's class. This flexibility simplifies code
and enhances its extensibility.
4. Abstraction:
• Abstraction is the process of simplifying complex systems by modeling classes
based on essential properties and behaviors while hiding unnecessary details.
It allows programmers to focus on high-level functionalities without worrying
about the underlying implementation. For example, a "BankAccount" class
can provide methods like "deposit" and "withdraw" without exposing the
internal workings of how transactions are processed.

Q4. Define class and object.(2M)


Ans.
Class:
• A class is a blueprint or template for creating objects. It defines a set of attributes
(data) and methods (functions) that the objects created from the class will have.
Think of a class as a recipe that outlines how to make something, specifying the
ingredients (attributes) and the steps to prepare it (methods).

Contact Details:
7972423160
Example: If you have a class called Dog, it might have attributes like breed, color, and age,
and methods like bark() and fetch().
Object:
• An object is an instance of a class. It is a specific realization of the class, containing
actual values for the attributes defined in the class. When you create an object, you
are using the class as a template to create something tangible that can perform
actions and hold data.
Example: If you create an object named myDog from the Dog class, myDog might have the
attributes breed = "Labrador", color = "Yellow", and age = 3. You can then call methods
on myDog, like myDog.bark(), which would execute the barking behavior defined in
the Dog class.

Q5. Explain the acess Specifiers in c++.(2M)


Ans.
Access specifiers in C++ are keywords that determine the accessibility of class members
(attributes and methods). They control how and where the members of a class can be
accessed. The three main access specifiers in C++ are:
1. Public:
• Members declared as public can be accessed from anywhere in the program.
This means other classes and functions can use these members freely.
• Example: If a class has a public method, you can call that method from
outside the class.
2. Private:
• Members declared as private can only be accessed within the class itself. This
means no outside code can access these members directly, which helps keep
the data safe and secure.
• Example: If a class has a private variable, you cannot access it from outside
the class.
3. Protected:
• Members declared as protected can be accessed within the class and by
classes that inherit from it (subclasses). This is useful for sharing data with
derived classes while keeping it hidden from the rest of the program.
• Example: If a base class has a protected variable, subclasses can access it, but
outside code cannot.

Contact Details:
7972423160
Q6. Write a c/c++ program to accept a number and display its square.(2M)
Ans.

#include <iostream>
using namespace std;
int main() {
// Declare a variable to store the number as an integer
int number;
// Prompt the user for input
cout << "Enter an integer: ";
cin >> number;
// Calculate the square of the number
int square = number * number;
// Display the result
cout << "The square of " << number << " is " << square << endl;
return 0;
}

Q7.Explain inline function? Write its syntax and example.(4M)


Ans.
An inline function is a special type of function in C++ that is designed to be faster than
regular functions.
When you call an inline function, the compiler replaces the function call with the actual
code of the function. This means that instead of jumping to a different part of the program
to execute the function, the code runs right where it is called.

#include <iostream>
using namespace std;
// Define an inline function

Contact Details:
7972423160
inline int square(int x) {
return x * x;
}
int main() {
int num;
// Prompt user for input
cout << "Enter a number: ";
cin >> num;
// Call the inline function
cout << "The square of " << num << " is " << square(num) << endl;
return 0;
}

Q8. Define constructor. List its types of constructor.(2M)


Ans.
A constructor is a special member function in a class that is automatically called when an
object of that class is created. Its primary purpose is to initialize the object's attributes and
allocate resources if necessary. Constructors have the same name as the class and do not
have a return type.
Types of Constructors
1. Default Constructor
• What It Is: A default constructor is a special type of constructor that does not take
any parameters (inputs).
• Purpose: It initializes the object with default values, which are usually set inside the
constructor.
• When to Use: Use a default constructor when you want to create an object without
needing to provide any specific values.
2. Parameterized Constructor
• What It Is: A parameterized constructor is a constructor that takes one or more
parameters (inputs) to set specific values for the object's attributes.

Contact Details:
7972423160
• Purpose: It allows you to create an object with custom values right when you create
it.
• When to Use: Use a parameterized constructor when you want to initialize an object
with specific data.

Q9. Difference between constructor and Destructor.(4M)


Ans.

S.
No. Constructor Destructor

Constructor helps to initialize the object of Whereas destructor is used to


1.
a class. destroy the instances.

It is declared as className( arguments if Whereas it is declared as ~


2.
any ){Constructor's Body }. className( no arguments ){ }.

Constructor can either accept arguments


3. While it can't have any arguments.
or not.

A constructor is called when an instance or It is called while object of the class


4.
object of a class is created. is freed or deleted.

Constructor is used to allocate the memory While it is used to deallocate the


5.
to an instance or object. memory of an object of a class.

6. Constructor can be overloaded. While it can't be overloaded.

Here, its name is also same as the


The constructor's name is same as the class
7. class name preceded by the tiled (~)
name.
operator.

In a class, there can be multiple While in a class, there is always a


8.
constructors. single destructor.

Contact Details:
7972423160
S.
No. Constructor Destructor

There is a concept of copy constructor


While here, there is no copy
9. which is used to initialize an object from
destructor concept.
another object.

They are often called in reverse


10. They are often called in successive order.
order of constructor.

Q10. Explain parameterized constructor with help of program.(4M)


Ans.
Parameterized Constructor
A parameterized constructor is a special type of constructor that allows you to initialize an
object with specific values at the time of its creation. This is useful when you want to create
objects with different initial states.

#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
// Parameterized constructor
Rectangle(int w, int h) {
width = w; // Initialize width with parameter w
height = h; // Initialize height with parameter h
}
// Function to calculate area
int area() {

Contact Details:
7972423160
return width * height; // Return the area of the rectangle
}
};
int main() {
// Create objects of Rectangle with different dimensions
Rectangle rect1(10, 5); // rect1 with width 10 and height 5
Rectangle rect2(7, 3); // rect2 with width 7 and height 3
// Output the areas of the rectangles

Q11. Write a program to add two complex numbers using a friend function.(2M)
Ans.

#include <iostream>
using namespace std;

class Complex {
private:
float real, imag;

public:
// Constructor to initialize complex number
Complex(float r = 0, float i = 0) {
real = r;
imag = i;
}

// Friend function to add two complex numbers


friend Complex addComplex(Complex c1, Complex c2);

Contact Details:
7972423160
// Function to display complex number
void display() {
cout << real << " + " << imag << "i" << endl;
}
};

// Definition of friend function


Complex addComplex(Complex c1, Complex c2) {
Complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}

int main() {
Complex c1(2.5, 3.5), c2(1.5, 2.5);

Complex sum = addComplex(c1, c2);

cout << "Sum of complex numbers: ";


sum.display();

return 0;
}

Contact Details:
7972423160
Q12.Compare static and non-static data members.(4M)
Ans.

Static variable Non static variable

Non static variables can


Static variables can be accessed using class name be accessed using
instance of a class

Non static variables


Static variables can be accessed by static and non static
cannot be accessed
methods
inside a static method.

Non static variables do


Static variables reduce the amount of memory used by a not reduce the amount
program. of memory used by a
program

In non Static variable


In Static variable Memory is allocated only once, at the time Memory is allocated
of class loading. each time an instance of
the class is created.

Non Static variables Can


Static variables Can be accessed from any part of the
be accessed only within
program.
the class or its instance.

Non Static variables


Static variables Exists for the entire lifetime of the program. Exists for the lifetime of
the object.

Non Static variables


Static variables Default value is assigned automatically. Default value is not
assigned automatically.

Contact Details:
7972423160
Static variable Non static variable

Non static variables are


Static variables are shared among all instances of a class. specific to that instance
of a class.

Non static variable is like


Static variable is like a global variable and is available to all a local variable and they
methods. can be accessed through
only instance of a class.

Q13.Write a program to declare a class ‘staff’ having data members as name and
department.Accept this data for 10 staffs and display names of staff that are in cm
department.(4M)
Ans.

#include <iostream>
using namespace std;

class Staff {
public:
string name;
string department;
};

int main() {
Staff s[10];

// Input staff details

Contact Details:
7972423160
for (int i = 0; i < 10; i++) {
cout << "Enter name of staff " << i + 1 << ": ";
cin >> s[i].name;
cout << "Enter department of staff " << i + 1 << ": ";
cin >> s[i].department;
}

// Display names of staff in 'CM' department


cout << "\nStaff in CM department:\n";
for (int i = 0; i < 10; i++) {
if (s[i].department == "CM" || s[i].department == "cm") {
cout << s[i].name << endl;
}
}

return 0;
}

Q14.What is inheritance? Why inheritance used in c++?(2M)\


Ans.
nheritance is a fundamental concept in object-oriented programming (OOP) that allows a
new class (called the derived or child class) to inherit properties and behaviors (attributes
and methods) from an existing class (called the base or parent class). This mechanism
promotes code reusability and establishes a hierarchical relationship between classes.
Why is Inheritance Used in C++?
1. Code Reusability: It allows the child class to use code from the parent class, reducing
duplication.
2. Extensibility: You can create new classes based on existing ones, adding new features
without changing the original class.

Contact Details:
7972423160
3. Polymorphism: It enables methods to be overridden in child classes, allowing for
dynamic behavior.
4. Organization: It helps organize classes in a hierarchical structure, making the code
easier to manage.

Q15.Give syntax for constructor in derived class.(2M)


Ans.

class Base {
public:
Base() {
// Base class constructor
}
};

class Derived : public Base {


public:
Derived() : Base() {
// Derived class constructor
}
};

Contact Details:
7972423160
Q16.What is hybrid inheritance? Give one example.(4M)
Ans.
Hybrid inheritance is a type of inheritance in C++ that combines two or more types of
inheritance. It can involve a mix of single inheritance, multiple inheritance, and multi-level
inheritance. This allows for a more flexible and complex class hierarchy, where a derived
class can inherit from multiple base classes and also have its own derived classes.

#include <iostream>
using namespace std;

class A {
public:
void displayA() {
cout << "Class A" << endl;
}
};

class B : public A {
public:
void displayB() {
cout << "Class B (from A)" << endl;
}
};

class C {
public:
void displayC() {
cout << "Class C" << endl;

Contact Details:
7972423160
}
};

// Hybrid Inheritance: D inherits from both B and C


class D : public B, public C {
public:
void displayD() {
cout << "Class D (from B and C)" << endl;
}
};

int main() {
D obj;
obj.displayA(); // from A via B
obj.displayB(); // from B
obj.displayC(); // from C
obj.displayD(); // from D
return 0;
}

Contact Details:
7972423160

You might also like