250+ TOP MCQs on malloc, calloc, free and realloc Calls and Answers

Tricky and Buggy questions and answers focuses on malloc, calloc, free and realloc Calls.

1. What is the output of this program?

  1.    #include
  2. 
    
  3.    int main()
  4.    {
  5.        int *ptr;
  6.        ptr = (int *)calloc(1,sizeof(int));
  7.        *ptr = 10;
  8.        printf("%dn",*ptr);
  9.        return 0;
  10.    }

a) 0
b) -1
c) 10
d) none of the mentioned

Answer: d

2. What is the output of this program?

  1.    #include
  2.    #include
  3. 
    
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        *ptr = 10;
  8.        *ptr = 20;
  9.        printf("%dn",*ptr);
  10.        return 0;
  11.    }

a) 10
b) 20
c) segmentation fault
d) none of the mentioned

Answer: c

3. What is the output of this program?

  1.    #include
  2.    #include
  3. 
    
  4.    int main()
  5.    {
  6.        int *ptr1, *ptr2;
  7.        ptr1 = malloc(4);
  8.        *ptr1 = 10;
  9.        *ptr2 = free(ptr1);
  10.        printf("%dn",*ptr2);
  11.        return 0;
  12.    }

a) 10
b) it will print the address stored in ptr1
c) it will print the address stored in ptr2
d) it will give an error

Answer: d

4. What is the output of this program?

  1.    #include
  2.    #include
  3. 
    
  4.    int main()
  5.    {
  6.        int *ptr1;
  7.        while(1){
  8.            ptr1 = malloc(1024*1024);
  9.            if(ptr1 == 0)
  10.                break;
  11.            sleep(1);
  12.            printf("n");
  13.            free(ptr1);
  14.        }
  15.        return 0;
  16.    }

a) it will print “” until the process has been stopeed by any signal
b) it will print nothing
c) segmentation fault
d) none of the mentioned

Answer: a

5. What is the output of this program?

  1.    #include
  2.    #include
  3. 
    
  4.    int main()
  5.    {
  6.        int ret;
  7.        int *ptr;
  8.        ptr = (int *)malloc(sizeof(int)*10);
  9.        free(ptr);
  10.        free(ptr);
  11.        return 0;
  12.    }

a) it will print nothing
b) it will give segmentaion fault
c) undefined behaviour
d) none of the mentioned

Answer: c

6. In which condition this prgram will print the string “”?

  1.    #include
  2.    #include
  3. 
    
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int *)malloc(sizeof(int)*10);
  8.        if (ptr == NULL)
  9.            printf("n");
  10.        return 0;
  11.    }

a) if the memory could not be allocated to the pointer “ptr”
b) if the memory has been allocated to the pointer “ptr” successfully
c) it will never print
d) none of the mentioned

  250+ TOP MCQs on Stages of Compilation and Answers

Answer: a
Clarification: The malloc() returns NULL when the memory is not allocated.

7. This program will allocate the memory of ___ bytes for pointer “ptr”.

  1.    #include
  2.    #include
  3. 
    
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int*)malloc(sizeof(int)*4);
  8.        ptr = realloc(ptr,sizeof(int)*2);
  9.        return 0;
  10.    }

a) 2
b) 4
c) 8
d) none of the mentioned

Answer: c
Clarification: We can also use the realloc() to make memory block smaller.

8. What is the output of this program?

  1.    #include
  2.    #include
  3. 
    
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int *)calloc(1,sizeof(int));
  8.        if (ptr != 0)
  9.            printf("%dn",*ptr);
  10.        return 0;
  11.    }

a) 0
b) -1
c) garbage value
d) none of the mentioned

Answer: a

9. What is the output of this program?

  1.    #include
  2.    #include
  3. 
    
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int *)malloc(sizeof(int));
  8.        printf("%dn",*ptr);
  9.        return 0;
  10.    }

a) 4
b) -1
c) undefined
d) none of the mentioned

Answer: c

10. In this program the allocated memory block can store

  1.     #include
  2.     #include
  3. 
    
  4.     int main()
  5.     {
  6.         int *ptr;
  7.         ptr = malloc(10);
  8.         return 0;
  9.     }

a) int
b) char
c) float
d) all of the mentioned

Answer: d
Clarification: When the malloc() is used without typecasting the default type is void*.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top