Data Structure
Data Structure
1) What is Encapsulation?
Ans= Encapsulation is an OOP principle where the implementation details of a class are hidden from
the outside world. It ensures that the data (attributes) and the methods (functions) are bundled
together and the data can only be accessed or modified through well-defined methods, providing
data protection.
Ans= i) Early Binding: Early Binding (or Static Binding) is the process where method calls are resolved
at compile-time. The method address is determined before the program is run.
ii) Late Binding: Late Binding (or Dynamic Binding) occurs when the method call is resolved at
runtime, not at compile-time. This is typically used with virtual functions in C++.
Ans= An inline function is a function where the compiler attempts to insert the function’s code
directly into the place where it is called, instead of performing a normal function call. This can help
reduce the overhead of function calls.
Ans= get(): Used to read input from streams (such as from cin or files).
5) What is stream?
Ans= A stream is an abstraction that represents a flow of data, either from input to output (like
reading from a file or keyboard) or from output to input (like writing to a file or display). Streams are
used for handling I/O operations in C++.
Ans= A friend function in C++ is a function that is not a member of a class but has access to its private
and protected members. It is defined outside the class but declared as a friend inside the class.
Ans= perator, state the syntax: The new operator is used to allocate memory dynamically on the
heap. It returns a pointer to the allocated memory.
Ans= he virtual keyword is used to tell the compiler to support dynamic (or late) binding. It ensures
that the correct method is called for an object, even when you use a pointer or reference to a base
class.
9) State user defined data types in C++.
Union: A data structure that allows storing different data types in the same memory location.
Ans= the scope resolution operator :: is used to define a method or variable outside the class. It is
also used to access global variables and functions when there is a local variable with the same name.
Ans= Extraction Operator (>>): Used to read data from input streams (e.g., cin).
Insertion Operator (<<): Used to write data to output streams (e.g., cout).
Ans= endl: Used to insert a newline character and flush the output buffer.
Ans= A constructor is a special member function of a class that is called automatically when an object
of the class is created. It initializes the object with default or user-defined values.
Ans=A reference variable is an alias for an already existing variable. It is used to directly access the
memory location of another variable. The major use is to avoid copying large objects and to modify
the original variable through the reference
Ans=Compile-time polymorphism is the method resolution that happens during the compilation
process, and it's generally achieved using method overloading and operator overloading.
protected: Members are accessible within the class and by derived classes
4 mark
1) List different types of constructor. Explain any one constructor with example.
Copy Constructor: A constructor that creates a new object as a copy of an existing object.
Example:
class Box {
private:
public:
// Default Constructor
Box() {
length = 0;
width = 0;
// Parameterized Constructor
Box(int l, int w) {
length = l;
width = w;
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
};
int main() {
box1.display();
box2.display();
return 0;}
public:
void print(int i) {
void print(double d) {
};
int main() {
Printer p;
return 0;
Multiple Inheritance: A class inherits from more than one base class.
Multilevel Inheritance: A class inherits from another class, which in turn inherits from
another class.
Hierarchical Inheritance: Multiple classes inherit from the same base class.
/ \
Derived1 Derived2
\ /
Derived3
Ans= File manipulators are used in C++ to modify the behavior of input and output streams,
especially when working with files.
#include <iostream>
#include <iomanip>
int main() {
Syntax:
*p = 10;
7) Explain memory allocation for objects with non-static data member and static data member.
Ans= In C++, memory allocation for non-static data members occurs separately for each object of a
class. Each time an object is created, new memory is allocated for its non-static members.
However, static data members are shared across all objects of a class. They are allocated only once,
at class level, and not per object. Memory for static members is allocated in the data segment, not
with object memory.
int a; // non-static
};
8) When do we make a class virtual base class? Explain it with suitable example.
Ans=A class is made a virtual base class to avoid duplication of data members during multiple
inheritance when a common base class is inherited multiple times.
Example:
class A {
public:
int x;
};
int main() {
D obj;
public:
int id;
void getData() {
void showData() {
};
int main() {
s[i].getData();
s[i].showData();
int main() {
int a, b;
try {
if (b == 0)
return 0;
class Demo {
public:
Demo() {
~Demo() {
};
int main() {
Demo d;
return 0;
25) Explain various stream classes used to perform console input/output (I/O) operations.
Ans:
C++ uses stream classes for I/O:
These classes provide functions and operators for formatted and unformatted I/O.
26) What is class Template? Explain syntax of class template with suitable example.
Ans:
Templates allow creation of classes or functions that work with any data type.
Syntax:
class MyClass {
T a;
public:
void set(T x) { a = x; }
};
int main() {
MyClass<int> obj1;
obj1.set(10);
obj1.show();
MyClass<string> obj2;
obj2.set("Hello");
obj2.show()
return 0;
3-Mark Questions:
b) Data abstraction
Hides internal details and shows only essential features.
c) Default Argument
a) Exception Handling
b) Operator Overloading
class A {
public:
};
int main() {
ptr->show();