Practice Questions
OOP Lab
Spring
2025
10 March, 2025
1
Question 1
Question # 1: (20 marks) Implement the function compress, the compresses a 2D array to
size R x C, and compresses it to size (R-2) x (C-2). The compression is done as follows:
taking a slot and its 9 neighboring cells, and calculate their average. Store the average in
output.
int** compress(int **input, int rows, int cols);
1
Note: The input array will have minimum 3 x 3 rows and columns. The working of the
function is explained in the diagram below:
For this you have to: 1. Write a function int **Input (int &r, int &c); which allocates the
memory with r and c values after taking input from user. 2. Write a function void output (int
**matrix, int r, int c); Which print the matrix. 3. Write a function void deallocate (int
2
**matrix, int c); Which de-allocates the memory. Call the Compress function which will
return a compressed 2D array, you need to print that as well. And finally deallocate all the
memory.
Here you can see, the input array is 3 x 4 and output array is 1 x 2. Input (R x C) -> Output
(R-2 x C-2). In Output array, (first value is 2 which is average of first 9 elements and the
value 3 is average of second 9 elements)
Sample Run:
Enter number of rows (>= 3): 7
Enter number of columns (>= 3): 8
Enter the elements of the matrix (row by row):
2 3 2 3 4 5 7 8
2 2 1 8 3 4 5 2
3 4 5 7 2 1 4 6
9 1 2 3 1 5 3 4
7 8 8 8 9 4 6 65
7 6 11 2 1 3 5 6
3 4 5 66 7 1 9 0
Original Matrix:
Matrix (7 x 8):
2 3 2 3 4 5 7 8
2 2 1 8 3 4 5 2
3 4 5 7 2 1 4 6
9 1 2 3 1 5 3 4
7 8 8 8 9 4 6 65
7 6 11 2 1 3 5 6
3 4 5 66 7 1 9 0
Compressed Matrix:
Matrix (5 x 6):
2 3 3 4 3 4
3 3 3 3 3 3
5 5 5 4 3 10
6 5 5 4 4 11
6 13 13 11 5 11
3
Question 2
In this question, you have to implement a HugeInt class that behaves like the int type, but
can handle integers up to(maximum) 15 decimal digits. You will use dynamic arrays to store
digits of various Huge Integers.
Define appropriate constructors (Default,Parameterized and copy constructor) and
destructor required for this class.
Functions:
1. Arithmetic Function:
HugeInt addHugeInt (HugeInt obj2)
2. Function for comparing HugeInt numbers for:
bool isLessThan(HugeInt obj2)
3. Output Function for printing HugeInt number to the screen.
4. Input Function to take HugeInt number from the user:
Take input a HugeInt Number.
Hint: For our ease, you can take input in char array and then put every value in integer
array.To take number from character array is like:
char c = ‘5’;
int num = c - 48;
Note: The integers can be positive only. Prototype of HugeInt class and main function is
given below:
class HugeInt{ void main()
int *data, size; {
public: HugeInt obj1Int;
void input(); obj1Int.input();
4
int data[] =
{1,2,3,4,5,6,7,8,9,4,3,1}, size = 12;
HugeInt obj2Int(data,size);
HugeInt obj3Int =
obj1Int.add(obj2Int);
obj3Int.output();
void output(); cout<<" = ";
HugeInt(); HugeInt(int *d, int s); obj1Int.output();
HugeInt(const HugeInt& obj); cout<<" + ";
bool isLessThan(HugeInt& obj); obj2Int.output();
HugeInt add(HugeInt obj); cout<<endl;
~HugeInt() if(obj2Int.isLessThan(obj3Int)
}; {
cout<<"I am lesser\n";
}
else{
cout<<"I am bigger\n";
}
}
Output:
Input a huge number size: 4
Input a number: 5432 123456794863 = 5432 + 123456789431
I am lesser.
Press any key to continue...
SAMPLE RUN:
Input a huge number size: 3
Input a number: 123
123456789554 = 123 + 123456789431
I am lesser
5
Question 3
Write a C++ program that simulates a car’s fuel management system using object-oriented
programming. Create a class named Car with the following attributes:
● Variables:
○ fuelInTank (double): the current fuel level in litres.
○ budget (double): the current available budget in dollars.
○ mileage (double): the total number of miles driven.
● Constants:
○ maxFuelCapacity (double): the maximum fuel capacity of the car.
○ fuelPrice (double): the cost per litre of fuel.
○ fuelAvg (double): the fuel consumption rate (litres per mile).
The class should include the following methods:
1. void drive(int milesTraveled)
○ Increase the mileage by the value of milesTraveled.
○ Decrease fuelInTank by an amount equal to milesTraveled *
fuelAvg.
○ If there isn’t enough fuel to drive the requested miles, display an appropriate
message.
2. void refill(int litres = -1)
○ If no value is provided (or if litres is -1), refill the tank to
maxFuelCapacity.
○ Otherwise, add the specified number of litres, ensuring the fuel level does
not exceed maxFuelCapacity.
○ Before refilling, calculate the cost (litres * fuelPrice) and check if it
is within the available budget. If the budget is insufficient, display an error
message; otherwise, update the fuelInTank and deduct the cost from the
budget.
6
3. Getters and Setters
○ Provide methods to get and set the values of fuelInTank, budget, and
mileage (apply any needed validations).
Demonstrate the class functionality in the main function by creating a Car object and
performing a series of drive and refill operations.
SAMPLE RUN:
Initial Car State:
Fuel: 10 litres
Budget: $100
Mileage: 0 miles
-----------------------------
Drove 50 miles. Fuel consumed: 5 litres.
Refilled 20 litres. Cost: $40. Remaining budget: $60
Drove 100 miles. Fuel consumed: 10 litres.
Insufficient budget to refill 35 litres. Required: $70, Available: $60
Question 4
Write a C++ program that uses recursion to print a diamond pattern of stars. The program
should ask the user to input an odd number n which represents the maximum width (i.e.,
the number of stars in the middle row). Use n-1 as the size when an even number n is
entered. Write code for handling common errors. The diamond must be printed such that
the top half increases from 1 star up to n stars (in increments of 2), and then the bottom half
decreases back to 1 star.
For example, for n=5 the output should be:
*
***
*****
***
*
7
SAMPLE RUN:
Enter an odd number for diamond width: 7
*
***
*****
*******
*****
***
*
Enter an odd number for diamond width: 8
You entered an even number. Using 7 instead.
*
***
*****
*******
*****
***
*
Question 5
Write a C++ program that recursively calculates the sum of the digits of a given integer.
The function should correctly handle both positive and negative integers.
SAMPLE RUN:
Enter a number: 1234
The sum of the digits of 1234 is 10