Q: 1 What is the output of following program?
#include<stdio.h>
#include<stdlib.h>
void main()
{
char *ptr=(char*)calloc(100,1);
ptr="Suraku Academy";
printf("%s",ptr);
}
Suraku
Suraku Academy
Address of ptr
Compile Time Error
[ Option B ]
Here first memory is allocated dynamically using calloc(100,1) and the returned address is stored in the pointer ptr.
However, in the next statement, ptr = "Suraku Academy"; the pointer is reassigned to point to a string literal instead of the allocated memory. This means the previously allocated memory becomes unused, but the pointer now correctly points to the string "Suraku Academy".
When printf("%s",ptr); is executed, it prints the string stored at the address pointed to by ptr, which is "Suraku Academy".
Q: 2 What is the problem with following code?
#include<stdio.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
ptr=NULL;
free(ptr);
}
Memory Leak.
Compile Time Error.
Dangling Pointer.
Wild Pointer.
[ Option A ]
In the given code, memory is dynamically allocated using malloc() and its address is stored in the pointer ptr. Immediately after allocation, the pointer is set to NULL using ptr=NULL;. This causes the original memory address returned by malloc() to be lost, meaning there is no way to access or free that allocated memory anymore.
As a result, the allocated memory remains unused and cannot be reclaimed, which leads to a Memory Leak.
Calling free(ptr) after setting ptr to NULL has no effect, because freeing a NULL pointer is safe but does nothing.
Q: 3 What is the return type of malloc() function?
void*
int*
Pointer of allocated memory type
Depends on compiler
[ Option A ]
In C, the malloc() function returns a pointer of type void*, which represents a generic pointer to the allocated memory block. This pointer can be typecast to any data type pointer as required.
| DMA FUNCTION | DESCRIPTION | RETURN TYPE |
|---|---|---|
| malloc() | Allocates a single block of memory and uninitialized. | void* |
| calloc() | Allocates multiple blocks and initializes to zero. | void* |
| realloc() | Resizes previously allocated memory. May return a new address. | void* |
| free() | Deallocates allocated memory. Does not return any value. | void |
Q: 4 Which function is used to delete the allocated memory space?
delete();
free();
dealloc();
remove();
[ Option B ]
In C, the free() function used to deallocate dynamically allocated memory. It releases the memory that was previously allocated using malloc(), calloc(), or realloc(), making it available for future use.
Q: 5 What is the output of the below code?
void main()
{
int *ptr;
ptr=(int *)calloc(1,sizeof(int));
*ptr=101;
printf("%d",*ptr);
}
10
0
101
5
[ Option C ]
Here, memory for one integer is allocated dynamically using calloc(1, sizeof(int)). Although calloc() initializes the allocated memory to zero, the next statement assigns the value 101 to the allocated memory using *ptr = 101;.
Therefore, when printf("%d",*ptr); is executed, it prints the value stored at that memory location, which is 101.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.