UNIT 5 MCQs
UNIT 5 MCQs
a) Pointer
b) String
c) Array
d) None of the mentioned
3. In _____________, the value of each of the actual argument in the calling function is copied into
the corresponding formal arguments of the called function.
a) Call by value
b) Call by reference
c) Pass by value
d) Pass by reference
#include <stdio.h>
void main( )
{
int a,x,z;
int *ptr, *b, *y;
a=10;
x=1/a;
ptr=&x;
b=&a;
y=&ptr;
printf("%d, %d, %d,%d,%d",x,a,ptr,b,y);
}
#include<stdio.h>
void main( )
{
int man;
int *ptr, **pptr;
man=100;
ptr=&man;
pptr=&ptr;
printf("\n %d", man);
printf("\n %d", *ptr);
printf("\n %d", **pptr);
}
a) 100
100
100
b) 100
Random address
Random address
c) 100
Random address
Random value
d) Compiler error
#include<stdio.h>
int main()
{
int i,j = 10 ;
void *vptr;
if (i==j)
{ vptr = &i;
printf("\nValue of iptr = %d ", *vptr);
}
else
{
printf("\nValue of iptr = %d ", &vptr);
}
return 0;
}
Explanation
Void pointer cannot be dereferenced without typecasting.
10. What is the output for the following C program?
#include<stdio.h>
int main ()
{
int a = 25, b, x=11, z= 50;
int *ptr, *ptr1, *ptr2, ptr3;
ptr = &a;
ptr1 = &b;
ptr2 = &x;
ptr3 = &z;
b = 36;
{
if (x<z)
{
printf("%d %d",*ptr, *ptr1);
}
else
printf("%d %d",*ptr2, *ptr1);
}
return 0;
}
a) 25 36
b) Random address Random address
c) Compiler Error
d) Prints garbage value
11. What is the output for the following C program?
#include<stdio.h>
int main()
{
int a, b, c, x, xa, xb, xc;
char *p = 0;
int *q = 0;
double *r = 0;
a = (int)(p++);
b = (int)(q + 1);
c = (int)(r + 1);
xa = (a+1+p);
xb = (b+1+q);
xc = (c+1+r);
a) 0,4,8,2,20,72
b) 0,0,0,0,0,0
c) 1,1,1,2,2,2
d) Compiler error
Explanation
• Initializing pointer variable to zero is possible. Since initial address of any data type is zero, so its
next address will be size of data type.
• Here a pointer variable *p belongs to char data type, thus a = (int) (p + 1); increase its address by
1 as it belongs to char datatype. Thus, variable a = 1.
• Here a pointer variable *q belongs to int data type, thus b = (int) (q + 1); increase its address by 4
as it belongs to int datatype. Thus, variable b = 4.
• Here a pointer variable *r belongs to double data type, thus c = (int) (r + 1); increase its address
by 8 as it belongs to double datatype. Thus, variable c = 8.
12. What will be the output of the C program?
#include<stdio.h>
void function(char**);
int main()
{
char *arr[] = { "NIT", "IIT", "IIIT", "IIITDM", "MIT", "SIT" };
function(arr);
return 0;
}
void function(char **ptr)
{
char *ptr1;
ptr1 = (ptr += sizeof(int))[-2];
printf("%s\n", ptr1);
}
A. IIIT
B. NIT
C. IIT
D. MIT
Explanation
Here function () gives the address of first value("NIT") in an arr. This address is summation
by a sizeof(int) which is 4. now the address in ptr points the value eggs, which is then reduced by
2. now it points to the value IIIT which is finally displayed.
13. What is the output of the following C program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr1;
while(1)
{
ptr1 = (int*)malloc(1024*1024);
if(ptr1 == 1)
break;
printf("NITPY\n");
free(ptr1);
}
return 0;
}
a) it will print “NITPY” until the process has been stopped by any signal
b) it will print nothing
c) segmentation fault
d) it will print “NITPY” ONCE
14. What is output of the following program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,*ptr;
ptr = (int *)calloc(1,sizeof(int));
if (ptr != 0 )
if (*ptr == 0)
printf("%d\n",*ptr);
return 0;
}
a) Compiler error
b) 0
c) 80
d) 10
Explanation: The memory allocated by calloc() contains 0 until process does not make
any change to it.
#include <stdio.h>
int main() {
int i, * ptr, sum = 0;
ptr = (int*)calloc(4, sizeof(int));
if (ptr == NULL) {
printf("Error");
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, sum = 100;
int *ptr;
ptr = (int *) malloc(10);
ptr = &100;
printf(" %d, %u\n", *ptr, ptr);
if (ptr == NULL)
{
printf("Error occurred!");
exit(0);
}
else
{
ptr = realloc(ptr, 20);
printf(" %s, %u\n", *ptr, ptr);
free(ptr);
}
return 0;
}
a) 100, 100
b) Random address, random address
c) Compiler error
d) 100, random address
Explanation:
100, 100
17. What is the output for the following C program?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int p = 100;
int *ptr = &p;
ptr = (int *) malloc(10);
printf(" %d\n", p);
ptr = realloc(ptr, 20);
printf(" %d\n", *ptr);
free(ptr);
return 0;
a) 100 0
b) 0 0
c) 100 random address
d) Compiler error
18. What is the size of parr after successful execution of the following C program?
#include <stdio.h>
#include <stdlib.h>
int main()
{
if (parr == NULL)
{
printf("Couldn't allocate memory");
}
else
{
printf("Memory allocation successful\n");
for (int i = 0; i < NUMBER_OF_ELEMENTS; i++)
{
parr[i] = rand();
}
parr = realloc(parr, (NUMBER_OF_ELEMENTS / 2) * sizeof(int32_t));
if (parr == NULL)
{
printf("Memory reallocation fails");
}
else
{
printf("Memory reallocation successful\n");
}
}
printf("\nsizeof parr = %d", sizeof(parr));
free(parr);
parr = NULL;
return 0;
}
a) 8
b) 10
c) 6
d) Compiler error
Explaination:
regardless of how much bytes the whole array pointed to by parr is
taking, sizeof(parr) equals to the size of a pointer variable in your platform.
#include<stdio.h>
#include<stdlib.h>
int main (void)
{
Int p
int ptr =&p;
if (p != NULL)
{
printf("Out of memory!\n");
exit(1);
}
else
{
*p = 10;
printf("%d\n",*pr);
}
free(p);
return 0;
}
a) 10
b) Out of memory
c) Prints nothing
d) Compiler error
#include<stdio.h>
#include<stdlib.h>
int main (void)
{
int p = 10;
int *q,*r = &p;
*r = (int *)malloc(sizeof(int) * 20);
if(*q==*r)
{
printf("%d\n",*r);
}
else
{
printf("%d\n",0);
}
free(p);
}
a) 0
b) Random Address Value
c) 10
d) Compiler error