Sunteți pe pagina 1din 5

1. What is the output of this program?

#include <stdio.h>
#include <stdlib.h>
int main()
{
int *j = (int*)malloc(4 * sizeof(int));
*j = 15;
free(j);
printf("%d", *j);
return 0;
}
A. Compilation error
B. 0
C. Some Garbage value
D. Nothing prints
B. View Answer
Ans : C

Explanation: In this program, a pointer variable *j is declared and its memory space is
allocated using malloc() and then an integer value 15 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.

2. What is the output of this program?

#include <stdio.h>
#include <stdlib.h>
int main()
{
int *numbers = (int*)calloc(4, sizeof(int));
numbers[0] = 2;
free(numbers);
printf("\nStored integers are ");
printf("\nnumbers[%d] = %d ", 0, numbers[0]);
return 0;
}
A. Garbage value
B. 0
C. 2
D. Compilation error

Ans : B

Explanation: In this program, a pointer variable *numbers is declared and its memory
space is allocated using calloc() and then an integer value 2 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.

3. What is the output of this program?

#include <stdio.h>
void main()
{
int *ptr = (int *)malloc(sizeof(int));
*ptr = 10;
free(ptr);
p = 5;
printf("%d", ptr);
}
A. Compilation error
B. 0
C. 5
D. Garbage value

Ans : 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 5 is assigned to the variable
ptr and then 5 is outputted.
4. Which statment is true about the given code ?

#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a[5]; a = (int*) malloc(sizeof(int)*5); free(a);
return 0;
}
A. Error: unable to allocate memory
B. Error: We cannot store address of allocated memory in a
C. Error: unable to free memory
D. No error

Ans : B

Explanation: None

5. Consider the following program, where are i, j and k are stored in


memory?

#include <stdio.h>
#include <stdlib.h>
int i;
int main()
{
int j;
int *k = (int *) malloc (sizeof(int));
}
A. i, j and *k are stored in stack segment
B. i and j are stored in stack segment. *k is stored on heap.
C. i is stored in BSS part of data segment, j is stored in stack segment. *k is
stored on heap.
D. j is stored in BSS part of data segment, i is stored in stack segment. *k is
stored on heap

Ans : C

Explanation: i is global variable and it is uninitialized so it is stored on BSS part of Data


Segment, j is local in main() so it is stored in stack frame, *k is dynamically allocated so
it is stored on Heap Segment.
6. How many bytes (16 bit platform) of memory will the following code reserve?
#include<stdio.h>
#include<stdlib.h>

int main()
{
int *p;
p = (int *)malloc(256 * 256);
if(p == NULL)
printf("Allocation failed");
return 0;
}

Ans-b - Hence 256*256 = 65536 is passed to malloc() function which can allocate upto 65535.
So the memory allocation will be failed in 16 bit platform (Turbo C in DOS).If you compile the
same program in 32 bit platform like Linux (GCC Compiler) it may allocate the required memory.

7. #include<stdio.h>
#include<stdlib.h>

int main()
{
union test
{
int i;
float f;
char c;
};
union test *t;
t = (union test *)malloc(sizeof(union test));
t->f = 10.10f;
printf("%f", t->f);
return 0;
}
A. 10

B. Garbage value

C. 10.100000

D. Error

Answer: Option C

S-ar putea să vă placă și