MCQ on Memory allocation and compilation process - GeeksforGeeks

08 Dec, 2021
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.60Explanation : 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 → PreprocessorExplanation : 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.hExplanation : 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 functionExplanation : 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#includeint 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 errorExplanation : 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.This article is contributed by Nikita Tiwari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
rkbhola5
C Language
Compiler Design
Compiler Design
GATE
GATE CS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in C/C++
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
std::sort() in C++ STL
Bitwise Operators in C/C++
Multidimensional Arrays in C / C++
Compiler Design | Syntax Directed Definition
Predictive Parser in Compiler Design
Difference between Source Code and Object Code
Lex program to identify the identifier
Lex program to take input from file and remove multiple spaces, lines and tabs