OOP Mid I Solution Spring-2024
OOP Mid I Solution Spring-2024
____________________________ _____________________
Vetted by Vetter Signature
IMPORTANT INSTRUCTIONS: Answer in the space provided. Answers written on rough sheet will not be
marked. Do not use pencil or red ink to answer the questions. In case of confusion or ambiguity make a
reasonable assumption.
Page 1 of 8
National University of Computer and Emerging Sciences
Part (b): Write output of the code segment below. If there is any error, clearly mention the error. (There is no syntax
error in this code.)
#include <iostream> Output/Error:
using namespace std;
void main()
{
int* ptr1 = SomeFunction();
cout<<"Data = ";
cout<<*ptr1<<endl;
}
Part (c) Write the output of the code segment given below. (There is no syntax error in this code.)
#include <iostream> int main() {
using namespace std; int nums[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int* ptr = nums;
void SomeFunction(int* arr, int size) { SomeFunction(ptr, 10);
int* ptr1 = arr; for(int i = 0; i < 10; ++i) {
int* ptr2 = arr + size - 1; cout << nums[i] << " ";
while(ptr1 < ptr2) { }
*ptr1 = *ptr2; return 0;
ptr1 = ptr1+2; }
ptr2--;
}
}
Output:
10,2,9,4,8,6,7,8,9,10
Page 2 of 8
National University of Computer and Emerging Sciences
Part (d) For the code segment given below, write output/error. In case of crash, highlight the line where program will
crash. (There is no syntax error in this code.)
[THIS QUESTION IS NOT FOR BCS-2C]
#include <iostream> int main() {
using namespace std; int* array1[10];
for(int i=0 ; i<10 ; i++)
int* GetData(int xyz) {
{ array1[i] = GetData(i);
int* ptr = 0; }
if(xyz%2 == 0) for(int i=0; i<10; i++)
{ {
ptr = new int[5]; for(int j=0; j<5 ; j++)
for(int i=0; i<5; i++) {
ptr[i] = i+1; array1[i][j] = array1[i][j] *2;
} cout<<array1[i][j]<<" ";
return ptr; }
cout<<endl;
} }
//Assume we have Deallocation code here that
//successfully deallocates the memory.
}
Output/Error:
2,4,6,8,10
Null Exception
Page 3 of 8
National University of Computer and Emerging Sciences
void FilterData(int**& ListOfIntArrays, int*& LenghtsOfArrays, int*& ArrayToFind, int& SizeOfArrayToFind, int&
TotalIntArrays)
Sample run below shows the values of required variables and arrays’ content before and after the function call for
ArrayToFind = {6,7,8} and SizeOfArrayToFind = 3.
Functionality Explanation:
Row 1, {1,2,3,4,5,6,7,8}: Not Removed, as ArrayToFind {6,7,8} found at the end.
Row 2, {6,7,8}: Removed, as ArrayToFind {6,7,8} found at end but there wasn’t any other data in this array.
Row 3, {1,2,3,4,5}: Removed, as ArrayToFind {6,7,8} NOT Found at the end.
Row 4, {1,1,1,2,2,2,2,6,7,8}: Not Removed, as ArrayToFind {6,7,8} found at the end.
Row 5, {6,7,8,6,6,8}: Removed, as ArrayToFind {6,7,8} NOT Found at the end.
Note that the data of ArrayToFind {6,7,8} has also been removed from original data arrays (ListOfIntArrays).
Make sure that arrays do not consume extra space. Also there should not be any memory leakage or dangling pointer.
Page 4 of 8
National University of Computer and Emerging Sciences
void FilterData(int**& ListOfIntArrays, int*& LenghtsOfArrays, int*& ArrayToFind, int& SizeOfArrayToFind, int&
TotalIntArrays)
{
//Start your code here…
// Function to filter arrays based on whether they end with a specified subarray
void FilterData(int**& arr, int*& arrLenghts, int& totalArrays, int* subArr, int
sizeOfSubArray)
{
int required_arrays = 0; // Count of arrays that meet the condition
int** result1 = new int* [totalArrays]; // Array to store filtered arrays
Page 5 of 8
National University of Computer and Emerging Sciences
}
}
delete[] arrLenghts; // Deallocate memory for the original array of array lengths
arrLenghts = temp_arr_lengths; // Update arrLenghts with the updated array lengths
totalArrays = required_arrays; // Update the total number of arrays
}
// Check if the entire array has been iterated (no remaining elements)
if (j == -1)
return false;
Page 6 of 8
National University of Computer and Emerging Sciences
Page 7 of 8
National University of Computer and Emerging Sciences
Page 8 of 8