Open In App

MCQ on Memory allocation and compilation process

Improve
Improve
Like Article
Like
Save
Share
Report

1. What will be the output of the following code? 
 

C




#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 = 20.60f;
    printf("%f", t->f);
    return 0;
}


(a) Garbage value 
(b) 20.600000 
(c) Syntax Error 
(d) 20.60
Explanation : 
Unions provide an efficient way of using the same memory location. All member of Union uses same memory location which has maximum space. 
Here, float f is used which has 

f = 20.60f = 20.600000

. So, option (b) is correct.
2. Which is the correct sequence of compilation process? 
(a) Assembler ? Compiler ? Preprocessor ? Linking 
(b) Compiler ? Assembler ? Preprocessor ? Linking 
(c) Preprocessor ? Compiler ? Assembler ? Linking 
(d) Assembler ? Compiler ? Linking ? Preprocessor
Explanation : 
Option (c) is correct.
3. During preprocessing, the code #include gets replaced by the contents of the file stdio.h. Which is true? 
(a) During linking the code #include replaces by stdio.h 
(b) Yes 
(c) During execution the code #include replaces by stdio.h 
(d) During editing the code #include replaces by stdio.h
Explanation : 
Preprocessing enlarges and boosts the C programming language by replacing preprocessing directive #include with the content of the file stdio.h. 
Option (b).
4. Why to use fflush() library function? 
(a) To flush all streams and specified streams 
(b) To flush only specified stream 
(c) To flush input/output buffer 
(d) Invalid library function
Explanation : 
As defined Use of fflush(stdin) in C : fflush() is typically used for output stream only. Its purpose is to clear (or flush) the output buffer and move the buffered data to console (in case of stdout) or disk (in case of file output stream). 
Option (a) is correct.
5. Point out the error ->
 

C




#include
#include
#include
int main()
{
    char* ptr;
    *ptr = (int*)malloc(30);
    strcpy(ptr, "RAM");
    printf("%s", ptr);
    free(ptr);
    return 0;
}


(a) Error: in strcpy() statement. 
(b) Error: in *ptr = (int *) malloc(30); 
(c) Error: in free(ptr); 
(d) No error
Explanation : 
Assignment makes integer from pointer without a cast. Option (b) is true.

6. Why is calloc() function used for? 
(a) allocates the specified number of bytes 
(b) allocates the specified number of bytes and initializes them to zero 
(c) increases or decreases the size of the specified block of memory and reallocates it if needed 
(d) calls the specified block of memory for execution.
Explanation : 
 

  • malloc() : allocates the specified number of bytes.
  • realloc() : increases or decrease the size of the specified block of memory. Reallocates it if needed.
  • calloc() : allocates the specified number of bytes and initializes them to zero.
  • free() : releases the specified block of memory back to the system.

So, option (b) is correct.

 



Last Updated : 08 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads