Linux Debugging Questions & Answers on malloc, calloc, free and realloc Calls – 1

DigitalOcean Referral Badge DigitalOcean Referral Badge

1.

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 = realloc(0,sizeof(int)*10);
  8.        return 0;
  9.    }

   A.) 0
   B.) 10
   C.) 40
   D.) none of these

Answer: Option 'C'

40

DigitalOcean Referral Badge

2.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3. 
     
  4.    int main()
  5.    {
  6. 	   char *ptr;
  7.        free(ptr);
  8.        return 0	   
  9.    }

   A.) this program will print nothing after execution
   B.) segmentation fault
   C.) Aborted (core dumped)
   D.) none of these

Answer: Option 'C'

Aborted (core dumped)

DigitalOcean Referral Badge

3.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    struct st{
  4.        int a;
  5.        char b;
  6.    };
  7. 
     
  8.    int main()
  9.    {
  10.        struct st *st_ptr;
  11.        st_ptr = malloc(sizeof(struct st));
  12.        printf("%d\n",sizeof(struct st));
  13.        return 0;
  14.    }

   A.) 8
   B.) 5
   C.) 0
   D.) none of these

Answer: Option 'A'

8

DigitalOcean Referral Badge

4.

What is the output of this program?

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

   A.) sanfoundry
   B.) segmentation fault
   C.) syntax error
   D.) none of these

Answer: Option 'B'

segmentation fault

DigitalOcean Referral Badge
    DigitalOcean Referral Badge DigitalOcean Referral Badge

5.

What is the output of this program?

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

   A.) sanfoudry
   B.) segmentation fault
   C.) syntax error
   D.) none of these

Answer: Option 'B'

segmentation fault

DigitalOcean Referral Badge

6.

What is the output of this program?

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

   A.) s
   B.) sanfoundry
   C.) 115
   D.) segmentation fault

Answer: Option 'C'

115

DigitalOcean Referral Badge

7.

Which one of the following in true about this program?

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

   A.) this program will give segmentation fault
   B.) this program will print two same values
   C.) this program has some syntax error
   D.) none of these

Answer: Option 'D'

none of these

DigitalOcean Referral Badge

8.

In this program the two printed memory locations has the difference of ___ bytes.

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

   A.) 1
   B.) 4
   C.) can not be determined
   D.) none of these

Answer: Option 'B'

4

DigitalOcean Referral Badge

9.

What is the output of this program?

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

   A.) 4
   B.) 8
   C.) the compiler will give the error
   D.) segmentaion fault

Answer: Option 'C'

the compiler will give the error

DigitalOcean Referral Badge
    DigitalOcean Referral Badge DigitalOcean Referral Badge

10.

What is the output of this program?

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

   A.) syntax error
   B.) segmentaion fault
   C.) run time error
   D.) none of these

Answer: Option 'D'

none of these

DigitalOcean Referral Badge

Linux Debugging Questions & Answers on malloc, calloc, free and realloc Calls – 1 Download Pdf

Recent Posts