Top

Memory Allocation

21.

What will be the output of the following code −

#include 
#include 
int main() {
   union my_union {
      int i;
      float f;
      char c;
   };
   union my_union* u;
   u = (union my_union*)malloc(sizeof(union my_union));
   u->f = 20.60f;
   prin

 

Answer: B

Using unions, we can use same memory location to hold multiple type of data. All member of union uses same memory location which has maximum space. Here float is used, which has 20.60f = 20.600000. 

Enter details here

Answer: B

The name malloc and calloc() are library functions that allocate memory dynamically. It means that memory is allocated during runtime(execution of the program) from heap segment.

Enter details here

23.

Which function is used to delete the allocated memory space?

Answer: B

free() is used to free the memory spaces allocated by malloc() and calloc().

Enter details here

24.

What is the output of this program?

#include 
#include  
int main()
{
  int i;
  char *ptr;
  char *fun();
  ptr = fun();
  printf(" %s", ptr);
  return 0;
}

char *fun()
{
  char disk[30];
  strcpy(disk, "letsfindcourse");
  printf("%s ",disk);
  return disk;
}

 

Answer: A

 

Here disk is an auto array, when array is used as return value, it will die when the control go back to main() function of a program. Thus memory in c alone prints.

Enter details here

25.

malloc() allocates memory from the heap and not from the stack.

Answer: A

No answer description available for this question

Enter details here

26.

Which of the following statement is correct prototype of the malloc() function in c ?

Answer: D

No answer description available for this question

Enter details here

27.

The function ____ obtains block of memory dynamically.

Answer: C

None

Enter details here

Answer: D

Heap allocation is needed for dynamic data structures like tree, linked list, etc.

Enter details here

29.

How will you free the memory allocated by the following program?

#include
#include
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int **p, i, j;
    p = (int **) malloc(MAXROW * sizeof(int*));
    return 0;
}

 

Answer: D

No answer description available for this question

Enter details here

30.

Output?

# include
# include
  
void fun(int *a)
{
    a = (int*)malloc(sizeof(int));
}
  
int main()
{
    int *p;
    fun(p);
    *p = 6;
    printf("%dn",*p);
    return(0);
}

 

Answer: A

No answer description available for this question.

Enter details here

Loading…
Tags: Memory Allocation Questions and Answers || Memory Allocation MCQ Questions and Answers || Memory Allocation GK Questions and Answers || Memory Allocation GK MCQ Questions || Memory Allocation Multiple Choice Questions and Answers || Memory Allocation GK || GK on Memory Allocation || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming