Tricky and Buggy Questions & Answers on malloc, calloc, free and realloc Calls – 2

1.

What is the output of this program?

  1.    #include<stdio.h>
  2. 
     
  3.    int main()
  4.    {
  5.        int *ptr;
  6.        ptr = (int *)calloc(1,sizeof(int));
  7.        *ptr = 10;
  8.        printf("%d\n",*ptr);
  9.        return 0;
  10.    }

   A.) 0
   B.) -1
   C.) 10
   D.) none of these

Answer: Option 'D'

none of these

2.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3. 
     
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        *ptr = 10;
  8.        *ptr = 20;
  9.        printf("%d\n",*ptr);
  10.        return 0;
  11.    }

   A.) 10
   B.) 20
   C.) segmentation fault
   D.) none of these

Answer: Option 'C'

segmentation fault

3.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3. 
     
  4.    int main()
  5.    {
  6.        int *ptr1, *ptr2;
  7.        ptr1 = malloc(4);
  8.        *ptr1 = 10;
  9.        *ptr2 = free(ptr1);
  10.        printf("%d\n",*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: Option 'D'

it will give an error

4.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  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("Sanfoundry\n");
  13.            free(ptr1);
  14.        }		
  15.        return 0;
  16.    }

   A.) it will print “Sanfoundry” until the process has been stopeed by any signal
   B.) it will print nothing
   C.) segmentation fault
   D.) none of these

Answer: Option 'A'

it will print “Sanfoundry” until the process has been stopeed by any signal
 

5.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  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 these

Answer: Option 'C'

undefined behaviour

6.

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

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3. 
     
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int *)malloc(sizeof(int)*10);
  8.        if (ptr == NULL)
  9.            printf("Sanfoundry\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 these

Answer: Option 'A'

if the memory could not be allocated to the pointer “ptr”

7.

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

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  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 these

Answer: Option 'C'

8

8.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3. 
     
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int *)calloc(1,sizeof(int));
  8.        if (ptr != 0)
  9.            printf("%d\n",*ptr);
  10.        return 0;
  11.    }

   A.) 0
   B.) -1
   C.) garbage value
   D.) none of these

Answer: Option 'A'

0

9.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3. 
     
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int *)malloc(sizeof(int));
  8.        printf("%d\n",*ptr);
  9.        return 0;
  10.    }

   A.) 4
   B.) -1
   C.) undefined
   D.) none of these

Answer: Option 'C'

The content of the memory block allocated by malloc() is undefined.
Ouput:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
0
[root@localhost sanfoundry]#

10.

In this program the allocated memory block can store

  1.     #include<stdio.h>
  2.     #include<stdlib.h>
  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 these

Answer: Option 'D'

all of these


Tricky and Buggy Questions & Answers on malloc, calloc, free and realloc Calls – 2 Download Pdf

Recent Posts