250+ TOP MCQs on DMA Functions, Memory Leak, Dangling Pointers and Answers

C Multiple Choice Questions & Answers (MCQs) on “DMA Functions, Memory Leak, Dangling Pointers – 1”.

1. What will be the output of the following C code if the input entered as first and second number is 5 and 6 respectively?

#include
#include
main()
{
    int *p;
    p=(int*)calloc(3*sizeof(int));
    printf("Enter first numbern");
    scanf("%d",p);
    printf("Enter second numbern");
    scanf("%d",p+2);
    printf("%d%d",*p,*(p+2));
    free(p);
}

a) 56
b) Address of the locations where the two numbers are stored
c) 57
d) Error
Answer: d
Clarification: The above code results in an error. This is because the syntax of the function calloc() is incorrect. In order to rectify this error, we must write: calloc(3,sizeof(int));

2. In the function malloc(), each byte of allocated space is initialized to zero.
a) True
b) False
Answer: b
Clarification: In the function malloc(), allocated space is initialized to junk values. In calloc(), each byte of allocated space is initialized to zero.

3. Which of the following functions allocates multiple blocks of memory, each block of the same size?
a) malloc()
b) realloc()
c) calloc()
d) free()
Answer: c
Clarification: malloc() allocates a single block of memory whereas calloc() allocates multiple blocks of memory, each block with the same size.

4. A condition where in memory is reserved dynamically but not accessible to any of the programs is called _____________
a) Memory leak
b) Dangling pointer
c) Frozen memory
d) Pointer leak
Answer: a
Clarification: If we allocate memory dynamically in a function (malloc, calloc, realloc), the allocated memory will not be de-allocated automatically when the control comes out of the function. This allocated memory cannot be accessed and hence cannot be used. This unused inaccessible memory results in a memory leak.

5. What will happens if the statement free(a) is removed in the following C code?

#include
#include
main()
{
    int *a;
    a=(int*)malloc(sizeof(int));
    *a=100;
    printf("*a%d",*a);
    free(a);
    a=(int*)malloc(sizeof(int));
    *a=200;
    printf("a%p",a);
    *a=200;
    printf("a%d",*a);
}

a) Error
b) Memory leak
c) Dangling pointer arises
d) 200 is printed as output
Answer: b
Clarification: The pointer ‘a’ points to the recent value 200, making the memory allocated earlier inaccessible. Hence, the memory where the value 100 is inaccessible and cannot be freed. Therefore, memory leak occurs.

6. The incorrect statement with respect to dangling pointers is ___________
a) Pointer pointing to non-existent memory location is called dangling pointer
b) When a dynamically allocated pointer references the original memory after it has been freed, a dangling pointer arises
c) If memory leak occurs, it is mandatory that a dangling pointer arises
d) Dangling pointer may result in segmentation faults and potential security risks
Answer: c
Clarification: Memory leak and dangling pointers are not inter dependent. Hence, when memory leak occurs, it is not mandatory that a dangling pointer arises

7. What will be the output of the following C code?

#include
#include
void main()
{
    char *p = calloc(100, 1);
    p = "welcome";
    printf("%sn", p);
}

a) error
b) welcome
c) memory location stored by the pointer
d) junk value
Answer: b
Clarification: There is no error in the above code. The format specifier being %s, address is not returned. Hence, welcome is the output.

8. In the function realloc(), if the new size of the memory block is larger than the old size, then the added memory ___________
a) is initialized to junk values
b) is initialized to zero
c) results in an error
d) is not initialized
Answer: d
Clarification: The function realloc() changes the size of a particular memory block. If the new size is larger than the old size, the added memory is not initialized.

9. The free() function frees the memory state pointed to by a pointer and returns ___________
a) the same pointer
b) the memory address
c) no value
d) an integer value
Answer: c
Clarification: The free() function frees the memory state pointed by a pointer and returns no value.

10. The following C code is an example of __________

#include
#include
#include
main()
{
    char *p,*q;
    p=(char*)malloc(3*sizeof(char));
    q=p;
    strcpy(p,"hello");
    printf("p=%s",p);
    printf("q=%s",q);
    free(q);
    q=NULL;
    gets(p);
    gets(q);
    printf("%s",p);
    printf(%s”,q);
}

a) Memory leak
b) Dangling pointer
c) Static memory allocation
d) Linked list
Answer: b
Clarification: In the above code, the pointer p, points to a memory location which has been freed. Hece the above code is an example of dangling pointer.

Leave a Reply

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