OOP&DSA Lab Final Questions
OOP&DSA Lab Final Questions
1. Write a program to take student information and display it using the student class and
its object.
#include<iostream>
class Student {
public:
string name;
int age;
string id;
void display() {
cout << "Name: " << name << "\nAge: " << age << "\nID: "
<< id << endl;
};
int main() {
Student s;
s.display();
return 0;
2. Write a program to take two inputs and find the maximum, minimum, sum, division,
subtraction, multiplication, and GCD of these two values using a class named
operations.
#include<iostream>
using namespace std;
class Operation {
public:
int a, b;
void calculate() {
cout << "Sum: " << a + b << "\nDifference: " << a - b <<
"\nProduct: " << a * b << "\nDivision: " << a / b << "\nGCD: " <<
gcd(a, b) << endl;
private:
while (y != 0) {
int temp = y;
y = x % y;
x = temp;
return x;
};
int main() {
Operation op;
op.calculate();
return 0;
}
3. Write a program to reverse a number by defining the class named reverse.
#include<iostream>
class Reverse {
public:
int reverseNumber(int n) {
int rev = 0;
while (n > 0) {
n /= 10;
return rev;
};
int main() {
Reverse r;
int num;
return 0;
4. Write a program to copy the content of one object to another using the copy
constructor function.
#include <iostream>
class Example {
private:
int value;
public:
// Parameterized Constructor
Example(int val) {
value = val;
// Copy Constructor
value = obj.value;
};
int main() {
obj1.display();
obj2.display();
return 0;
5. Write a program to copy one object to another using the concept of a copy
constructor.
#include <iostream>
class Example {
private:
int value;
public:
// Parameterized Constructor
Example(int val) {
value = val;
// Copy Constructor
};
int main() {
Example obj1(20);
obj1.display();
obj2.display();
return 0;
6. Write a program to find the maximum and minimum value of an array using an inline
function.
#include<iostream>
return (a < b) ? a : b;
int main() {
int x, y;
cout << "Maximum: " << findMax(x, y) << "\nMinimum: " <<
findMin(x, y) << endl;
return 0;
7. Write a program to calculate the average of some integer values by defining a static
member function in a class.
#include<iostream>
class Average {
public:
int sum = 0;
sum += arr[i];
};
int main() {
int n;
cin >> n;
int arr[n];
return 0;
#include<iostream>
class Triangle {
public:
};
double calculateArea(Triangle t) {
int main() {
cout << "Area of the triangle: " << calculateArea(t) << endl;
return 0;
9. Write a program to display the month of birth by using an enumerated data type.
#include<iostream>
int main() {
int monthNumber;
cout << "You were born in month number: " << birthMonth <<
endl;
return 0;
}
10. Write a program to display a pyramid of “*" using manipulators.
#include <iostream>
int main() {
int rows;
cout << "Enter the number of rows for the pyramid: ";
return 0;
}
11. Write a program to find the square root of a number by passing an object to a
function.
#include <iostream>
#include <cmath>
class Number {
private:
double value;
public:
};
int main() {
Number num(16.0);
calculateSquareRoot(num);
return 0;
}
12. Write a program to find the square root of a number by returning an object from a
function.
#include <iostream>
#include <cmath>
class Number {
private:
double value;
public:
return Number(sqrt(value));
};
int main() {
Number num(25.0);
return 0;
13. Write a program to overload the unary operator to increment or decrement an integer
number.
class UnaryOperator {
int value;
public:
UnaryOperator(int v) : value(v) {}
};
class BinaryOperator {
int value;
public:
BinaryOperator(int v) : value(v) {}
};
Int main() {
UnaryOperator u(5);
++u;
--u;
std::cout << "Unary operator value: " << u.getValue() << "\
n";
Return 0;
14. Write a program to overload the binary operator to sum two integer numbers.
class BinaryOperator {
int value;
public:
BinaryOperator(int v) : value(v) {}
};
Int main() {
BinaryOperator b3 = b1 + b2;
std::cout << "Binary operator value: " << b3.getValue() << "\n";
15. Write a program to overload a function named calculateArea to measure the area of
a triangle, rectangle, and circle.
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
public:
};
int main() {
Shape shape;
cout << "Triangle Area: " << shape.calculateArea(5.0, 10.0) << endl;
cout << "Rectangle Area: " << shape.calculateArea(4.0, 6.0) << endl;
return 0;
16. Write a program to overload the comparator operator to compare two strings.
#include <iostream>
#include <string>
class CompareStrings {
public:
};
int main() {
CompareStrings cs;
return 0;
17. Write a program to inherit a daughter/son class to show the common properties of
her/his father and mother.
#include <iostream>
class Father {
public:
};
class Mother {
public:
};
public:
void displayJobs() {
Father::job();
Mother::job();
};
int main() {
Daughter daughter;
daughter.displayJobs();
return 0;
18. Write a program to store bank account information using file I/O in C++.
class BankAccount {
private:
string accountHolderName;
int accountNumber;
double balance;
public:
void input() {
getline(cin, accountHolderName);
void display() {
void saveToFile() {
outFile.close();
void loadFromFile() {
ifstream inFile("bank_account.txt");
if (inFile) {
display();
inFile.close();
} else {
cout << "File not found." << endl;
};
int main() {
BankAccount account;
account.input();
account.saveToFile();
account.loadFromFile();
return 0;
19. Write a program to store the following shape in a file using manipulators:
Programming
Programmin
Programmi
Programm
Program
Progra
Progr
Prog
Pro
Pr
P
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
ofstream file("shape_output.txt");
if (file.is_open()) {
string shape = "Programming";
for (int i = shape.length(); i > 0; --i) {
file << setw(shape.length()) << shape.substr(0, i) <<
endl;
}
file.close();
cout << "Shape stored in file successfully!" << endl;
} else {
cout << "Error opening file!" << endl;
}
return 0;
}
20. Write a program to calculate the area of a triangle, rectangle, and circle by declaring
a virtual function calculateArea.
#include <iostream>
#include <cmath>
class Shape {
public:
};
public:
};
public:
cout << "Area of Rectangle: " << length * width << endl;
};
double radius;
public:
Circle(double r) : radius(r) {}
cout << "Area of Circle: " << M_PI * radius * radius <<
endl;
};
int main() {
Shape *shapes[3];
shapes[0] = new Triangle(10, 5);
shapes[i]->calculateArea();
delete shapes[i];
return 0;
#include <iostream>
#include <vector>
sort(arr.begin(), arr.end());
int n = arr.size();
swap(arr[i], arr[j]);
int main() {
sortAscending(ascArray);
rearrangeDescending(descArray);
return 0;
5. Take a two-dimensional array of size n×nn \times n. Write a program to remove the
column and row where zero (0) is found.
#include <iostream>
#include <vector>
#include <set>
if (matrix[i][j] == 0) {
rowsWithZero.insert(i);
colsWithZero.insert(j);
vector<vector<int>> result;
vector<int> newRow;
newRow.push_back(matrix[i][j]);
}
if (!newRow.empty()) {
result.push_back(newRow);
matrix = result;
removeRowColWithZero(matrix);
return 0;
swapped = true;
if (!swapped)
break;
2. Write a program to sort a group of elements using the selection sort. Display the
results for each step.
int minIndex = i;
minIndex = j;
swap(arr[minIndex], arr[i]);
}
3. Write a program to sort a group of elements using the insertion sort. Show the results
for each step.
int j = i;
j--;
4. Write a program to sort a group of elements using the merge sort. Display the results
for each step.
}
while (i <= mid) temp[k++] = arr[i++];
5. Write a program to sort a group of elements using the quick sort. Show the results for
each step.
i++;
swap(arr[i], arr[j]); // Place the smaller element
at left
while (l <= r)
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
l = mid + 1;
else
r = mid - 1;
return -1;
Linked List:
1. Write a program to implement a linear linked list. The program should include
functions to insert a node at the beginning, at the end, and at a specified position.
Additionally, displays all the elements in the list.
#include <iostream>
// Node structure
struct Node {
int data;
Node* next;
};
head = newNode;
}
// Function to insert at the end
if (!head) {
head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
if (position == 0) {
insertAtBeginning(head, data);
return;
temp = temp->next;
if (!temp) {
return;
newNode->next = temp->next;
temp->next = newNode;
while (head) {
head = head->next;
int main() {
insertAtBeginning(head, 3);
insertAtEnd(head, 7);
insertAtPosition(head, 5, 1);
displayList(head);
return 0;
}
2. Write a program to reverse a linear linked list. The program should take a linked list
as input, reverse the order of its nodes, and output the reversed list.
#include <iostream>
struct Node {
int data;
Node* next;
};
if (!head) {
head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
head = prev;
while (head) {
head = head->next;
int main() {
insertAtEnd(head, 1);
insertAtEnd(head, 2);
insertAtEnd(head, 3);
displayList(head);
reverseList(head);
displayList(head);
return 0;
3. Write a program to delete a node from a linear linked list; i.e., here, we shall not
consider the deletion process of the first node and the last node of the list.
Additionally, handle edge cases such as attempting to delete from an empty list or
deleting the head node.
#include <iostream>
struct Node {
int data;
Node* next;
};
if (!head) {
head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
}
void deleteNode(Node*& head, int position) {
if (!head) {
return;
if (position == 0) {
head = head->next;
delete temp;
return;
temp = temp->next;
if (!temp || !temp->next) {
return;
temp->next = toDelete->next;
delete toDelete;
while (head) {
head = head->next;
}
int main() {
insertAtEnd(head, 1);
insertAtEnd(head, 2);
insertAtEnd(head, 3);
displayList(head);
deleteNode(head, 1);
displayList(head);
return 0;
#include <iostream>
struct Node {
int data;
Node* next;
};
if (!head) {
head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
while (head) {
head = head->next;
return false;
int main() {
insertAtEnd(head, 2);
insertAtEnd(head, 3);
int key = 2;
if (searchElement(head, key)) {
} else {
return 0;
5. Write a program to sort the elements (in ascending order) of a linked list.
#include <iostream>
#include <vector>
#include <algorithm>
struct Node {
int data;
Node* next;
};
head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
vector<int> values;
while (temp) {
values.push_back(temp->data);
temp = temp->next;
sort(values.begin(), values.end());
temp = head;
temp->data = val;
temp = temp->next;
while (head) {
cout << head->data << " -> ";
head = head->next;
int main() {
insertAtEnd(head, 3);
insertAtEnd(head, 1);
insertAtEnd(head, 2);
displayList(head);
sortList(head);
displayList(head);
return 0;
6. Write a program to create a doubly linked list and display the data items (nodes).
#include <iostream>
int data;
Node* next;
Node* prev;
};
if (!head) {
head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
while (head) {
head = head->next;
}
int main() {
insertAtEnd(head, 1);
insertAtEnd(head, 2);
insertAtEnd(head, 3);
displayList(head);
return 0;
#include <iostream>
class Stack {
int* arr;
int top;
int capacity;
public:
Stack(int size) {
capacity = size;
top = -1;
~Stack() {
delete[] arr;
void push(int x) {
if (top == capacity - 1) {
return;
arr[++top] = x;
int pop() {
if (top == -1) {
return -1;
return arr[top--];
int peek() {
if (top == -1) {
cout << "Stack is empty\n";
return -1;
return arr[top];
bool isEmpty() {
};
int main() {
Stack stack(5);
stack.push(10);
stack.push(20);
stack.push(30);
stack.pop();
cout << "After popping, top element: " << stack.peek() << endl;
return 0;
}
2. Write a program to check if a given expression has balanced parentheses using a
stack. The program should support different types of parentheses: (), {}, and [].
#include <iostream>
#include <stack>
stack<char> s;
s.push(ch);
s.pop();
return false;
return s.empty();
int main() {
} else {
return 0;
3. Write a program to implement a linked list-based stack and perform push and pop
operations.
#include <iostream>
struct Node {
int data;
Node* next;
};
class Stack {
Node* top;
public:
Stack() : top(nullptr) {}
void push(int x) {
top = newNode;
}
int pop() {
if (!top) {
return -1;
top = top->next;
delete temp;
return data;
int peek() {
if (!top) {
return -1;
return top->data;
bool isEmpty() {
};
int main() {
Stack stack;
stack.push(10);
stack.push(20);
stack.push(30);
stack.pop();
cout << "After popping, top element: " << stack.peek() << endl;
return 0;
#include <iostream>
#include <stack>
return 0;
stack<char> s;
if (isalnum(ch)) {
s.push(ch);
postfix += s.top();
s.pop();
} else {
postfix += s.top();
s.pop();
s.push(ch);
while (!s.empty()) {
postfix += s.top();
s.pop();
}
return postfix;
int main() {
return 0;
#include <iostream>
class Queue {
int* arr;
public:
Queue(int size) {
capacity = size;
front = 0;
rear = -1;
count = 0;
~Queue() {
delete[] arr;
void enqueue(int x) {
if (count == capacity) {
return;
arr[rear] = x;
count++;
int dequeue() {
if (count == 0) {
return -1;
count--;
return data;
}
int peek() {
if (count == 0) {
return -1;
return arr[front];
bool isEmpty() {
return count == 0;
};
int main() {
Queue queue(5);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.dequeue();
cout << "After dequeuing, front element: " << queue.peek() << endl;
return 0;
}
Tree and Graph:
1. Write a program to create a tree. Include functions to traverse the tree in in-order,
pre-order, and post-order.
2. Write a program to implement a binary search tree; in addition, traverse the tree in in-
order, pre-order, and post-order.
3. Write a program to find the height of a binary tree (MAX or MIN).
4. Write a program to perform breadth-first search (BFS) on a graph.
5. Write a program to perform depth-first search (DFS) on a graph.