Top

Memory Allocation

61.

Consider the following three C functions :

[PI] int * g (void) 
{ 
  int x= 10; 
  return (&x); 
}  
  
[P2] int * g (void) 
{ 
  int * px; 
  *px= 10; 
  return px; 
} 
  
[P3] int *g (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?

Answer: C

No answer description available for this question.

Enter details here

62.

What is the output of this program?

#include 
#include  
int main()
{
  int i, numbers[1];
  numbers[0] = 15;
  free(numbers);
  printf("Stored integers are ");
  printf("numbers[%d] = %d ", 0, numbers[0]);
  return 0;
}

 

Answer: A

he 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().

Enter details here

63.

Point out the correct statement which correctly free the memory pointed to by 's' and 'p' in the following program?

#include
#include

int main()
{
    struct ex
    {
        int i;
        float j;
        char *s
    };
    struct ex *p;
    p = (struct ex *)malloc(sizeof(struct ex));
    p->s = (char*)malloc(20);
    return 0;
}

 

Answer: B

No answer description available for this question

Enter details here

64.

What is the return type of malloc() or calloc()

Answer: A

No answer description available for this question.

Enter details here

65.

malloc() returns a NULL if it fails to allocate the requested memory.

Answer: A

No answer description available for this question

Enter details here

66.

What will be the output of the program?

#include
#include

int main()
{
    char *s;
    char *fun();
    s = fun();
    printf("%s\n", s);
    return 0;
}
char *fun()
{
    char buffer[30];
    strcpy(buffer, "RAM");
    return (buffer);
}

 

Answer: B

The output is unpredictable since buffer is an auto array and will die when the control go back to main. Thus s will be pointing to an array , which not exists.

Enter details here

67.

What is the output of this program?

#include 
void main()
{
   int *ptr = (int *)malloc(sizeof(int));
   *ptr = 10;
   free(ptr);
   p = 5;
   printf("%d", ptr);
}

 

Answer: B

 

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.

Enter details here

68.

What is the problem with following code?

#include
int main()
{
    int *p = (int *)malloc(sizeof(int));
 
    p = NULL;
 
    free(p);
}

 

Answer: B

No answer description available for this question.

Enter details here

69.

Which statment is true about the given code ?

#include 
#include  
int main()
{
    int *a[5];
    a = (int*) malloc(sizeof(int)*5);
    free(a);
    return 0;
}

 

Answer: B

None

Enter details here

70.

Where does the uninitialized data gets stored in memory?

Answer: C

BSS- Block started by symbol the uninitialized data gets stored in memory.

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