OOPS QUESTION PAPER
SECTION-A (SHORT ANSWER TYPE QUESTIONS)
a. What is the use of super keyword in C++?
Answer:
C++ does not have a super keyword like Java. Instead, it uses the scope
resolution operator (::) to access base class members in case of inheritance.
For example:
class Base { public: void show() { cout << "Base"; } };
class Derived : public Base { public: void show() { Base::show(); } };
b. Compare and Contrast late binding and early binding.
Answer:
Early Binding: Function call is resolved at compile time (e.g. normal function
calls).
Late Binding: Function call is resolved at runtime (e.g. virtual functions).
Comparison:
Early binding is faster; late binding is more flexible.
Early binding uses normal function calls; late binding uses virtual functions
with vtables.
c. Which operators are used for memory allocation and deallocation in C++?
Answer:
Allocation: new operator.
Deallocation: delete operator.
Example:
int *p = new int;
delete p;
d. Write a statement for allocation of memory of ten integer elements.
Answer:
int *arr = new int[10];
e. What are inline functions in C++?
Answer:
Inline functions are functions whose code is expanded at the point of call
instead of performing a normal function call, reducing overhead. Declared
using the inline keyword.
f. Define Friend Function. Give an example to illustrate it.
Answer:
A friend function is a function that is not a member of a class but has access to
its private and protected members.
Example:
class Test {
private: int a;
public:
friend void show(Test t);
};
void show(Test t) { cout << t.a; }
g. What is the need of abstract class in C++?
Answer:
An abstract class (with at least one pure virtual function) cannot be
instantiated but serves as a base class for derived classes. It enforces
implementation of pure virtual functions in derived classes.
h. What is virtual inheritance in C++?
Answer:
Virtual inheritance solves the "diamond problem" in multiple inheritance,
ensuring that only one copy of the base class is inherited by the derived class.
Example:
class A {};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};
i. What is the value of p in the given C++ code snippet?
#include <iostream>
using namespace std;
int main() {
int p;
bool a = true;
bool b = false;
int x = 10;
int y = 5;
p = ((x | y) + (a + b));
cout << p;
return 0;
Answer:
x | y = 10 | 5 = 15 (binary OR operation).
a + b = 1 + 0 = 1.
p = 15 + 1 = 16.
So, p = 16.
j. Which of the following C++ code will give an error on compilation?
Code 1
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
cout << "Welcome";
return 0;
Code 2
#include <iostream>
int main(int argc, char const *argv[])
std::cout << "Students";
return 0;
Answer:
Neither code gives an error; both are syntactically correct. However, if asked
to choose one, both are valid C++ code.
k. What will be the output if the following program is executed in C++?
#include <stdio.h>
void func(void)
printf("Example");
void main()
show();
show(2);
Answer:
Error: show() function is not defined anywhere in the code. Hence, the
compiler will throw an error "undefined function show()".
SECTION – B (LONG ANSWER TYPE QUESTIONS)
Q2. Discuss various features of Object-Oriented Programming Language in details.
Answer:
OOP (Object-Oriented Programming) revolves around objects and classes. The main features
are:
1. Class and Object:
A class is a user-defined data type, and objects are instances of classes.
2. Encapsulation:
Bundling of data and methods operating on the data into one unit. It helps in data
hiding.
3. Abstraction:
Hides complex internal implementation and shows only relevant details.
4. Inheritance:
Mechanism of deriving a new class from an existing class. Supports code reusability.
5. Polymorphism:
Ability to use the same function name for different types. It is of two types: Compile-
time (function overloading) and Runtime (virtual functions).
6. Data Hiding:
Using access specifiers (private, protected, public) to restrict direct access to class
members.
7. Dynamic Binding:
At runtime, the code to be executed is decided based on the object.
8. Message Passing:
Objects communicate with one another by sending and receiving information.
Q3. What do you understand by dynamic memory allocation in C++?
Answer:
Dynamic memory allocation is the process of allocating memory at runtime using operators
like new and delete.
• Syntax:
cpp
CopyEdit
int* ptr = new int; // allocates memory for an integer
*ptr = 10;
delete ptr; // frees the memory
• For Arrays:
cpp
CopyEdit
int* arr = new int[5]; // allocates memory for array of size 5
delete[] arr; // deallocates the memory
• Advantages:
o Allocates exact memory needed
o Supports creation of complex data structures (linked lists, trees)
Q4. Differentiate between the following:
a) Copy Constructor and Parameterized Constructor
Aspect Copy Constructor Parameterized Constructor
Initializes object with user-defined
Purpose Creates a copy of an object
values
Takes a reference to an object of the same
Parameters Takes user-defined parameters
class
Example ClassName(const ClassName &obj) ClassName(int x, int y)
b) Implicit and Explicit Type Conversion
Type Implicit Conversion Explicit Conversion
Triggered by Compiler Programmer
Syntax Automatic Using cast operator
Example int a = 5.5; → a = 5 int a = (int)5.5;
Q5. What is inheritance in C++? Explain various types of inheritance with examples.
Answer:
Inheritance allows one class (derived class) to inherit properties from another (base class).
Types:
1. Single Inheritance
cpp
CopyEdit
class A { };
class B : public A { };
2. Multilevel Inheritance
cpp
CopyEdit
class A { };
class B : public A { };
class C : public B { };
3. Multiple Inheritance
cpp
CopyEdit
class A { };
class B { };
class C : public A, public B { };
4. Hierarchical Inheritance
cpp
CopyEdit
class A { };
class B : public A { };
class C : public A { };
5. Hybrid Inheritance
A mix of multiple and multilevel. Can lead to ambiguity, resolved using virtual base
classes.
Q6. What is the need of overloading operators and functions?
Answer:
Overloading provides flexibility to use operators/functions in different ways based on
arguments.
Function Overloading Example:
cpp
CopyEdit
void print(int i);
void print(double d);
void print(string s);
Operator Overloading Example:
cpp
CopyEdit
class Complex {
int real, imag;
public:
Complex(int r, int i): real(r), imag(i) {}
Complex operator+(Complex const &obj) {
return Complex(real + [Link], imag + [Link]);
}
};
Q7. What is an exception in C++? How is it different from error?
Answer:
• Exception: An unexpected situation that occurs during program execution.
• Error: Broader term; may include syntax, logical, or runtime problems.
Syntax:
cpp
CopyEdit
try {
// Code that may throw
throw 10;
}
catch (int e) {
cout << "Exception: " << e;
}
Difference:
Exception Error
Can be handled Often not handled
Use try-catch Handled by compiler/interpreter
Specific to logic May occur at compilation or runtime
Q8. What are file streams? Explain the process of open, read, write and close files.
Answer:
C++ uses fstream, ifstream, ofstream for file operations.
Opening:
cpp
CopyEdit
ofstream fout("[Link]");
ifstream fin("[Link]");
Writing:
cpp
CopyEdit
fout << "Hello";
Reading:
cpp
CopyEdit
string line;
getline(fin, line);
Closing:
cpp
CopyEdit
[Link]();
[Link]();
Q9. Explain in brief:
1) Run Time Polymorphism
Achieved using virtual functions and inheritance.
cpp
CopyEdit
class Base {
public:
virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived"; }
};
2) Function Overloading
Multiple functions with the same name but different parameters.
cpp
CopyEdit
int add(int a, int b);
float add(float a, float b);