This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1What happens if a programmer forgets to release memory that was allocated using malloc() in C?
Option C
If memory allocated using malloc() is not released using free(), the program causes a Memory Leak. A memory leak occurs when dynamically allocated heap memory is no longer needed but is not deallocated, and there is no pointer left to access it.
As a result, that memory remains reserved and cannot be reused during the program’s execution. Over time, repeated memory leaks can consume large amounts of RAM and may eventually Slow Down or Crash the system.
Q: 2Consider the following C program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *ptr = (int *)malloc(3*sizeof(int));
for(int i = 0; i < 3; i++)
ptr[i] = i + 1;
ptr = (int *)realloc(ptr,5*sizeof(int));
for(int i = 3; i < 5; i++)
ptr[i] = i + 1;
free(ptr);
free(ptr);
}
Option C
In the given program, memory is first allocated using malloc() for three integers and initialized correctly. Then realloc() is used to increase the memory size to store five integers, and the additional elements are assigned values properly. Up to this point, the program works correctly.
However, the problem occurs at the end where free(ptr); is called twice. The first free() correctly deallocates the dynamically allocated heap memory, but the second free() attempts to release the same memory again. This results in Undefined Behavior, meaning the program may crash, corrupt memory, or behave unpredictably.
Q: 3As soon as a pointer variable is freed its value ___________.
Option D
In C, when a pointer is freed using free(), the memory it was pointing to is deallocated, but the pointer itself is not automatically modified. It still holds the same address value as before. Therefore, the pointer’s value remains unchanged.
However, this pointer becomes a Dangling Pointer, because it points to memory that is no longer valid.
Q: 4What is the output of the following C code?
# include<stdio.h>
# include<stdlib.h>
void fun(int *a)
{
a = (int*)malloc(sizeof(int));
}
int main()
{
int *p;
fun(p);
*p = 9;
printf("%d",*p);
return(0);
}
Option C
Here, the pointer p in main() is declared but not initialized. When fun(p) is called, the pointer is passed by value, meaning a copy of p is sent to the function. Inside fun(), memory is allocated to the local parameter a, but this change does not affect the original pointer p in main() because only a copy was modified. Therefore, after returning from fun(), the pointer p in main() still contains a garbage address.
When the statement *p=9; is executed, it attempts to dereference an uninitialized pointer, which results in undefined behaviour. The program may crash, may print some garbage value, or may behave unpredictably or may work on some systems.
Q: 5
Match the following:
| Group-A | Group-B |
|---|---|
| (X) m=malloc(5); m= NULL; | (1) Using Dangling Pointers. |
| (Y) free(n); n->value=5; | (2) Using Uninitialized Pointers. |
| (Z) char *p; *p=’a’; | (3) Memory Lost. |
Option C
In statement (X), memory is allocated using malloc(5), but immediately the pointer m is assigned NULL without freeing the allocated memory. This results is memory leak because the allocated heap memory no longer has any reference.
In statement (Y), memory is first freed using free(n);, but after freeing, the pointer is still used (n->value=5;). This is situation of Dangling Pointer, because the pointer refers to memory that has already been deallocated.
In statement (Z), a pointer p is declared but not initialized to any valid memory location. When *p='a'; is executed, it attempts to access memory through an uninitialized pointer, which leads to Undefined Behavior.
Q: 6What 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);
}
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: 7Consider the following three C functions:
[P1]
int* fun(void)
{
int x=10;
return(&x);
}
[P2]
int* fun(void)
{
int *px;
*px=10;
return px;
}
[P3]
int* fun(void)
{
int *px;
px = (int *)malloc(sizeof(int));
*px=10;
return px;
}
Which of the above three functions are likely to cause problems with pointers?
Option B
[P1]
int* fun(void)
{
int x=10;
return(&x);
}
Here, x is a local variable stored on the stack. Once the function returns, x goes out of scope and its memory becomes invalid. Returning the address of such a local variable results in a Dangling Pointer, which can cause undefined behavior.
Hence, P1 causes a pointer problem.
[P2]
int* fun(void)
{
int *px;
*px=10;
return px;
}
In this case, px is an uninitialized pointer. No memory is allocated before dereferencing it using *px=10. This leads to undefined behavior and may cause a Segmentation Fault.
Hence, P2 causes a pointer problem.
[P3]
int* fun(void)
{
int *px;
px=(int *)malloc(sizeof(int));
*px=10;
return px;
}
Here, memory is correctly allocated using malloc(), the value is assigned properly, and the pointer returned is valid as long as the allocated memory is not freed.
Hence, P3 does NOT cause a pointer problem.
Q: 8What is the problem with following code?
#include<stdio.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
ptr=NULL;
free(ptr);
}
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: 9Consider the following code?
int *p=malloc(5*sizeof(int));
p=realloc(p,0);
What happens?
Option B
In C, when realloc(ptr, 0) is called, it behaves like free(ptr). This means the previously allocated memory block is deallocated, and the memory is returned to the heap.
According to the C standard, if the new size passed to realloc() is zero and the pointer is not NULL, the memory is released.
Now, consider the one more special case, if ptr is NULL and size is not 0, then realloc() behaves exactly like malloc(size). That means it allocates a new block of memory of the given size and returns a pointer to it.
| FUNCTION CALL | CONDITION | WHAT HAPPENS | EQUIVALENT TO |
|---|---|---|---|
| realloc(ptr, new_size); | ptr ≠ NULL and new_size > 0 | Resizes the existing memory block. Old data is preserved up to the minimum of old and new size. | Resize operation |
| realloc(ptr, 0); | ptr ≠ NULL | Frees the allocated memory block. | free(ptr); |
| realloc(NULL, size); | size > 0 | Allocates a new memory block of given size. | malloc(size); |
Q: 10What is returned by malloc() if the allocation fails?
Option C
In C, the malloc() function attempts to allocate the requested amount of memory from the heap. If the memory allocation is successful, it returns a pointer to the beginning (base address) of the allocated block. However, if the allocation fails for any reason, malloc() returns NULL.
The same rule applies to other dynamic memory allocation functions such as calloc() and realloc().
If calloc() fails to allocate memory, it also returns NULL. Similarly, realloc() returns NULL if it cannot resize the memory block.
Q: 11What happens if free() is called on a pointer that was never dynamically allocated?
Option C
In C, the free() function is used to release memory that was allocated using malloc(), calloc(), or realloc(). If free() is called on a pointer that was not dynamically allocated, the program results in Undefined Behavior.
Undefined behavior means there is no guarantee what will happen. The program may crash, corrupt memory, behave unpredictably, or sometimes even appear to work correctly. Since C gives full control to the programmer, it does not perform automatic safety checks in such situations.
Q: 12What is the return type of malloc() function?
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: 13Which of the following is used for static allocation of memory in C?
Option D
In C, Static Memory Allocation is done at compile time using fixed-size variables or arrays, whereas malloc(), calloc(), and realloc() are used for Dynamic Memory Allocation at runtime, so none of the given options represent static allocation.
Q: 14The heap is used for?
Option C
Memory allocation in programming divides into static (compile-time, fixed size) and dynamic (runtime, flexible size).
The heap is a region of memory used for dynamic memory allocation, where memory is allocated and deallocated at runtime as per program requirements.
In languages like C, functions such as malloc(), calloc(), realloc(), free() are used to manage heap memory.
Q: 15Consider the following C program. In which memory segments are the variables a, b, c, and *d stored?
int a;
int b=22;
void main()
{
int c;
int *d=(int *)malloc(sizeof(int));
}
Option C
In C, memory is divided mainly into following segments:
(1) Text or Code Segment.
(2) Data Segment.
(3) Heap Segment.
(4) Free Space Segment.
(5) Stack segment.
(6) Unmapped Segment.
The variable a is declared globally but not initialized. Global uninitialized variables are stored in the BSS (Block Started by Symbol) segment. Therefore, a is stored in the BSS part of the data segment.
The variable b is also global but initialized with value 22. Global initialized variables are stored in the DATA segment. Hence, b is stored in the DATA part of the data segment.
Inside the main() function, c is a local variable. Local variables are always stored in the stack segment. Therefore, c is stored in the stack.
The pointer variable d itself is stored in the stack because it is a local variable. However, the memory allocated using malloc() (*d) is stored in the heap segment.
Therefore, finally:
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.