sir is there any explanation mcq of pointers
Explaination for pointer mcq
which question Explanation you want
we will explain you.
4 What is the problem with the following code #include<stdio.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
ptr = NULL;
free(ptr);
}
A) Dangling Pointer
B) Memory leak
C) The program may crash as free() is called for NULL pointer.
D) Compiler Error
Q6 What will be the output of the following program? #include
using namespace std;
int main() {
int arr[]={0,1,2,3,4};
int i, ptr;
for(ptr= arr, i=0; ptr+i <= arr+4; ptr++, i++) cout<<(ptr+i);
return 0;
}
A) 01234
B) 024
C) 234
D) 12340
Q7 In the piece of code, arr[ ][ ] is a 2D array and assume that the
contents of the 2D array are already filled up. What is stored in the
variable sum at the end of the code segment?
int arr[3][3];
int i, sum=0; i = 0; while(i<3) {
sum += * ( * (arr+i)+(i++)); }
printf(“sum:%d”, sum);
A) Sum of all elements in the matrix
B) Sum of alternate elements in the matrix
C) Sum of the elements along the principal diagonal
D) None
Q8 Would the following program compile?
#include
using namespace std;
int main() {
int a=10, *j; void *k; j=k=&a; j++;
k++; cout<<j<<k; return 0;
}
in question
ptr=arr means it holds the address of array’s first element
and i=0;
ptr+0<=arr+4 /// here we are comparing the addreses condition true
so output is 0
now ptr and i both inc so ptr+i reach at 2
so output is 2
simillarly for others.
in 8th ans is no because you can’t do j=k as both have different data types