The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Memory Allocation Questions

In most of the MNC interview questions such as in ZOHO interview question, IVTL Infoview interview questions, Amazon interview questions, GOOGLE interview questions, Infosys interview questions and even in Voonik interview questions, We come across several Tricky C Questions about which 2:5 of the questions are from Memory Allocation in c. Solving that kind of tricky C questions is not an easy task for all C programmers. We need more practices to solve it with ease. So we provide 25+ interesting C questions in Memory Allocation to make your MNC interview very easy.

C Memory Allocation Questions and Answers

1. Among 4 header files, which should be included to use the memory allocation functions like malloc(), calloc(), realloc() and free()?



A. #include<string.h>

B. #include<memory.h>

C. #include<stdlib.h>

D. Both b and c

x

 

Option: C

Explanation

#include<stdlib.h> is a header filer, which contains the inbuilt functions for all memory allocation functions.

Answer


2. What will be the output of the C program?

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int i, numbers[1];
	numbers[0] = 9;
	free(numbers);
	printf("\nStored integers are ");
	printf("\nnumbers[%d] = %d ", 0, numbers[0]);
	return 0;
}

A. 9

B. 0

C. Compilation error

D. garbage value

x

 

Option: A

Explanation

The memory allocation function free() will not free or delete the content in the memory location, because free() can only free or delete the content in the memory, who's memory is allocated either by malloc() or calloc().

Answer


3. What will be the output of the C program?

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int *j = (int*)malloc(4 * sizeof(int));
	*j = 9;
	free(j);
	printf("%d", *j);
	return 0;
}

A. Compilation error

B. 0

C. Some Garbage value

D. Nothing prints

x

 

Option: C

Explanation

In this program, a pointer variable *j is declared and its memory space is allocated using malloc() and then an integer value 9 is set to a pointer variable *j.

Now free(j) is used to free or delete the memory space allocated to the pointer variable *j using malloc().

While freeing the memory space allocated using malloc(), all allocated space will be deleted and by default the deleted space will be denoted by some garbage value and is outputted.

Answer


4. What will be the output of the C program?

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int *numbers = (int*)calloc(4, sizeof(int));
	numbers[0] = 9;
	free(numbers);
	printf("\nStored integers are ");
	printf("\nnumbers[%d] = %d ", 0, numbers[0]);
	return 0;
}

A. Garbage value

B. 0

C. 9

D. Compilation error

x

 

Option: B

Explanation

In this program, a pointer variable *numbers is declared and its memory space is allocated using calloc() and then an integer value 9 is set an array of index 0 ie numbers[0].

Now free(numbers) is used to free or delete the memory space allocated to the pointer variable *numbers using calloc().

While freeing the memory space allocated using calloc() ,all the space in a variable numbers are deleted and by default deleted spaces are denoted as 0 which is outputted.

Answer


5. What will be the output of the C program?

#include<stdio.h>
int main()
{
	int *ptr = (int *)malloc(sizeof(int));
	*ptr = 10;
	free(ptr);
	p = 9;
	printf("%d", ptr);
}

A. Compilation error

B. 0

C. 9

D. Garbage value

x

 

Option: C

Explanation

free() will not deallocate the memory it just to delete all data's allocated to a variable (ptr).

In this program, first integer value 10 is assigned to the pointer variable *ptr and then its data is deleted using free(ptr) and then new integer value 9 is assigned to the variable ptr and then 9 is outputted.

Answer


Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report