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

Linux Debugging questions and answers focuses on malloc, calloc, free and realloc Calls.1. 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 = realloc(0,sizeof(int)*10);
  8.        return 0;
  9.    }

a) 0
b) 10
c) 40
d) none of the mentioned

Answer: c
Clarification: If the first argument of realloc() is NULL, then it behaves just like malloc().

2. What is the output of this program?

  1.    #include
  2.    #include
  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 the mentioned

Answer: c
Clarification: This prgram is trying to free the memory which is not available in the heap segment.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san
*** glibc detected *** ./san: free(): invalid pointer: 0x4a77cff4 ***
======= Backtrace: =========
/lib/libc.so.6[0x4a6489f2]
./san[0x80483c9]
/lib/libc.so.6(__libc_start_main+0xf3)[0x4a5e96b3]
./san[0x8048321]
======= Memory map: ========
08048000-08049000 r-xp 00000000 fd:01 394194 /home//san
08049000-0804a000 rw-p 00000000 fd:01 394194 /home//san
09233000-09254000 rw-p 00000000 00:00 0 [heap]
4a5ab000-4a5cc000 r-xp 00000000 fd:01 785334 /lib/ld-2.14.90.so
4a5cc000-4a5cd000 r–p 00020000 fd:01 785334 /lib/ld-2.14.90.so
4a5cd000-4a5ce000 rw-p 00021000 fd:01 785334 /lib/ld-2.14.90.so
4a5d0000-4a77a000 r-xp 00000000 fd:01 789110 /lib/libc-2.14.90.so
4a77a000-4a77b000 —p 001aa000 fd:01 789110 /lib/libc-2.14.90.so
4a77b000-4a77d000 r–p 001aa000 fd:01 789110 /lib/libc-2.14.90.so
4a77d000-4a77e000 rw-p 001ac000 fd:01 789110 /lib/libc-2.14.90.so
4a77e000-4a781000 rw-p 00000000 00:00 0
4a7e0000-4a7fc000 r-xp 00000000 fd:01 789128 /lib/libgcc_s-4.6.2-20111027.so.1
4a7fc000-4a7fd000 rw-p 0001b000 fd:01 789128 /lib/libgcc_s-4.6.2-20111027.so.1
b7724000-b7725000 rw-p 00000000 00:00 0
b773d000-b773f000 rw-p 00000000 00:00 0
b773f000-b7740000 r-xp 00000000 00:00 0 [vdso]
bfc83000-bfca4000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)
[[email protected] ]#

3. What is the output of this program?

  1.    #include
  2.    #include
  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("%dn",sizeof(struct st));
  13.        return 0;
  14.    }

a) 8
b) 5
c) 0
d) none of the mentioned

Answer: 8
Clarification: Maximum size of the data type is 4 byte(int) in the structure.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san
8
[[email protected] ]#

4. What is the output of this program?

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

a)
b) segmentation fault
c) syntax error
d) none of the mentioned

Answer: b
Clarification: value of a string can not be assign to a pointer.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san
Segmentation fault (core dumped)
[[email protected] ]#

5. What is the output of this program?

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

a) sanfoudry
b) segmentation fault
c) syntax error
d) none of the mentioned

Answer: b
Clarification: Memory must be allocated to pointer “ptr”.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san
Segmentation fault (core dumped)
[[email protected] ]#

6. What is the output of this program?

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

a) s
b)
c) 115
d) segmentation fault

Answer: c
Clarification: This program will print the equivalent decimal value at location pointed by “ptr”.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san
115
[[email protected] ]#

7. Which one of the following in true about this program?

  1.    #include
  2.    #include
  3.    #include                              
  4. 
    
  5.    int main()
  6.    {
  7.        char *ptr;
  8.        printf("%pn",ptr);
  9.        ptr = (char *)malloc(sizeof(char));
  10.        printf("%pn",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 the mentioned

Answer: d

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

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

a) 1
b) 4
c) can not be determined
d) none of the mentioned

Answer: b

9. What is the output of this program?

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

a) 4
b) 8
c) the compiler will give the error
d) segmentaion fault

Answer: c

10. What is the output of this program?

  1.     #include
  2.     #include
  3.     #include                              
  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 the mentioned

Answer: d
  250+ TOP MCQs on File Management 2 and Answers

Leave a Comment

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

Scroll to Top