0% found this document useful (0 votes)
5 views5 pages

Practice Problems 3

Problems OOP

Uploaded by

f2204048
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Practice Problems 3

Problems OOP

Uploaded by

f2204048
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Write the output of the following programs (if any).

If there is an error in the program,


mention the error and move on.
Tip: Use python tutor (https://pythontutor.com/cpp.html#mode=edit) for line-by-line
execution of programs for a better understanding, however, first try to solve by yourself.

Code Output

void foo( int* arr1, const int size, int val, int* pos) { 10 10 10 10 10
if(*pos == size - 1)
*arr1 = val;
else {
*arr1 = val;
++*pos;
foo(arr1 + 1, size, val, pos);
}
}

int main() {
const int size = 5;
int arr[size] = {10, 20, 33, 0, 1};
int pos = 0;
foo( arr, size, 10, &pos);
for(int i = 0; i < size; ++i)
cout<<arr[i]<<" ";

return 0;
}

void make2(int *arr, int cols) { Segmentation fault in the inner


arr = new int[cols]; most loop in the main.
} Reason, memory is being
void make1(int **arr, int rows, int cols) { allocated within the function,
arr = new int*[rows]; however, pointers are being
make2(*arr, cols); passed by value, thus the value of
} pointer in the main as well in the
void make(int ***arr, int pages, int rows, int cols) { functions didn’t update after
arr = new int**[pages]; returning back.
make1(*arr, rows, cols);
}

int main() {

int*** arr = NULL;


make(arr, 4, 4, 4);
for(int i = 0; i < 4; ++i) {
for(int j = 0; j < 4; ++j) {
for(int k = 0; k < 4; ++k)
arr[i][j][k] = i + j + k;
}
}

return 0;
}

int main() { 2 30 40 30
int num[5]= {1,2,3,4,5};
int* p;
p = num;
*p = 20;
p = &num[1];
*(++p) = 30;
p = num + 4;
*p = 30;
p = num;
*(p + 3) = 40;
for (int i = 1; i < 5; i++)
cout << num[i] << " ";
return 0;
}

int main() { Pakistan


P
char name[5][10] = { "Pakistan", "China", "Turkiye", "Korea", Korea
"Japan"}; o
char* ptr1 = name[0]; rea
cout<<ptr1<<endl; T
cout<<*ptr1<<endl; China
ptr1 = name[3];
cout<<ptr1<<endl;
cout<<*(ptr1 + 1)<<endl;
cout<<ptr1+2<<endl;
ptr1 = name[1];
cout<<*(ptr1 + 10)<<endl;
ptr1 = name[2];
cout<<ptr1-10<<endl;

return 0;
}
int main() {
Syntax error
char* alpha, beta; beta is of type char. It cannot
beta = new char[5]; allocate memory.

return 0;
}

int main() {
Address of beta variable
int alpha = 100, beta = 200; 200
int *p = &alpha, *q = &beta;
p = q;
cout<<p<<endl;
cout<<*p<<endl;

return 0;
}

int main() { Address of b

int a = 5, b = 10, c = 15;


int *arr[] = {&a, &b, &c};
cout << arr[1];

return 0;
}

int main() { EF
int i, j, var = 'A'; C
for (i = 3; i >= 1; i--) { B
for (j = 0; j < i; j++) {
if(((i+var + j))%4==0)
continue;
cout<<char (i+var + j);
}
cout<<endl;
}
return 0;
}

int main() { ABCDEFGHIJ


char arr[20];
int i;
for (i = 0; i < 10; i++)
*(arr + i) = 65 + i;
*(arr + i) = '\0';
cout << arr;

return 0;
}

int main() { Logical Error


Dynamic Memory deletion done
int*** arr = new int**[5]; incorrectly resulting in memory
for(int i = 0; i < 5; ++i) { leak.
arr[i] = new int*[5];
for(int j = 0; j < 5; ++j)
arr[i][j] = new int[5] {1, 2, 3, 4, 5};
}

for(int i = 0; i < 5; ++i) {


for(int j = 0; j < 5; ++j) {
for(int k = 0; k < 5; ++k)
cout<<arr[i][j][k]<<" ";
cout<<endl;
}
}

delete arr;
arr = NULL;

return 0;
}

int main() { 2121

int a[2][4] = {3, 6, 9, 12, 15, 18, 21, 24};


cout << *(a[1] + 2) << * (*(a + 1) + 2);

return 0;
}

int main() { 46 66 78 68

int* scores; Segmentation Fault


scores = new int[4]{45, 65, 77, 67}; Deleting unallocated memory

if(scores)
cout<<++*scores++<<" "<<++*scores++<<" "<<++*scores++<<"
"<<++*scores++;
if(scores)
delete scores;
scores = NULL;

return 0;
}

You might also like