0% found this document useful (0 votes)
13 views8 pages

OOP Questions 1 To 20 Notes

The document covers various fundamental concepts of C++ programming, including exception handling, virtual functions, polymorphism, file operations, operator overloading, inheritance, constructors, destructors, and access specifiers. It provides code examples for each concept, illustrating their implementation and usage. Additionally, it discusses the differences between related concepts such as function overloading and overriding, as well as static and dynamic binding.

Uploaded by

sarariguri09
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)
13 views8 pages

OOP Questions 1 To 20 Notes

The document covers various fundamental concepts of C++ programming, including exception handling, virtual functions, polymorphism, file operations, operator overloading, inheritance, constructors, destructors, and access specifiers. It provides code examples for each concept, illustrating their implementation and usage. Additionally, it discusses the differences between related concepts such as function overloading and overriding, as well as static and dynamic binding.

Uploaded by

sarariguri09
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/ 8

1. How is Exception Handling performed in C++? Write a program to handle divide by zero exception.

C++ uses try, catch, and throw for exception handling.

Example:

#include <iostream>

using namespace std;

int main() {

int a = 10, b = 0;

try {

if(b == 0)

throw "Division by zero!";

cout << a / b;

} catch(const char* msg) {

cout << "Error: " << msg;

return 0;

2. Explain the concept of virtual function and pure virtual function with the help of example.

Virtual Function: A member function that can be overridden in derived classes using the 'virtual' keyword.

Pure Virtual Function: A virtual function with no definition, used in abstract classes.

Example:

class Base {

public:

virtual void show() { cout << "Base"; }

virtual void pure() = 0; // Pure virtual

};
3. What is polymorphism? Explain its two types and difference between compile-time and run-time polymorphism.

Polymorphism allows functions or objects to behave differently based on input.

Types:

- Compile-time (Function/Operator Overloading)

- Run-time (Virtual Functions)

Example:

Compile-time:

int add(int a, int b);

float add(float a, float b);

Run-time:

class Base { virtual void show(); };

class Derived: public Base { void show(); };

4a. How will you create a text file and perform read/write operations on it?

Use fstream library.

Example:

ofstream fout("file.txt");

fout << "Hello";

fout.close();

ifstream fin("file.txt");

string s;

fin >> s;

fin.close();

4b. Explain operator overloading with programming example.

It allows redefining operators.


Example:

class Complex {

int a;

public:

Complex(int x) { a = x; }

Complex operator+(Complex obj) {

return Complex(a + obj.a);

};

5. What is polymorphism? Explain the different ways in which we can implement compile-time and run-time

polymorphism.

Already explained in Q3.

6. Create a base class DMA and a class that uses DMA to store strings. Use deep copy constructor and destructor.

class DMA {

char* str;

public:

DMA(const char* s) {

str = new char[strlen(s)+1];

strcpy(str, s);

~DMA() { delete[] str; }

DMA(const DMA& d) {

str = new char[strlen(d.str)+1];

strcpy(str, d.str);

}
};

7. Create a class and inherit two derived classes. Use base class pointer. Explain runtime polymorphism.

class Base {

public:

virtual void show() { cout << "Base"; }

};

class Derived1 : public Base {

public:

void show() override { cout << "Derived1"; }

};

class Derived2 : public Base {

public:

void show() override { cout << "Derived2"; }

};

8. What is function overloading and how is it different from function overriding?

Function Overloading: Same function name, different parameters.

Function Overriding: Derived class redefines base class function using virtual keyword.

9. Write a C++ program to implement single inheritance.

class A {

public: void display() { cout << "A"; }

};

class B : public A {

public: void show() { cout << "B"; }

};
10. What is Constructor and Destructor? How are they used in inheritance?

Constructor initializes an object. Destructor releases memory.

Constructors in derived classes call base class constructors first.

11. What is Inheritance? Explain all its types with examples.

Inheritance allows a class to acquire properties and behaviors of another class.

Types:

- Single: One base, one derived.

- Multiple: Multiple bases, one derived.

- Multilevel: Derived from derived.

- Hierarchical: One base, multiple derived.

- Hybrid: Combination of above.

12. What do you mean by Constructor in C++? Explain all its types and also explain parameterized & copy constructor

with examples.

Constructor: Special function to initialize objects.

Types:

- Default

- Parameterized

- Copy

Example:

class A {

public:

A() {} // Default

A(int x) {} // Parameterized
A(A &obj) {} // Copy

};

13. Write down 10 applications of OOP.

1. Game Development

2. GUI Applications

3. Real-Time Systems

4. Database Systems

5. Web Development

6. Network Simulations

7. CAD/CAM Systems

8. Embedded Systems

9. Robotics

10. AI Applications

14. Differentiate between static binding and dynamic binding.

Static Binding:

- Resolved at compile-time.

- Uses function overloading.

Dynamic Binding:

- Resolved at run-time.

- Uses virtual functions.

15. What are stream manipulators? Explain.

Stream manipulators format output/input.

Examples:

- endl: Ends line.


- setw(): Set width.

- setprecision(): Set decimal precision.

- fixed: Fixed-point notation.

16. Define class? How member function define inside and outside the class? Give example.

Class: Blueprint for object.

Inside class:

class A {

public: void show() { cout << "Inside"; }

};

Outside class:

class A {

public: void show();

};

void A::show() { cout << "Outside"; }

17. What is the role of this pointer in C++? Explain it with programming example.

This pointer stores address of current object.

Example:

class A {

int a;

public:

void set(int a) { this->a = a; }

};

18. Differentiate between structure and class.

Structure:
- Members are public by default.

- Cannot have member functions in C.

Class:

- Members are private by default.

- Supports member functions.

19. What are the different features and applications of OOP? Explain in detail.

Features:

- Encapsulation

- Abstraction

- Inheritance

- Polymorphism

Applications: Game dev, Web dev, OS, Simulation, etc.

20. Define Accessifiers in C++ with programming example.

Access Specifiers: Define access control.

- public: Accessible everywhere.

- private: Accessible within class.

- protected: Accessible within class and derived class.

Example:

class A {

private: int x;

public: int y;

protected: int z;

};

You might also like