Here we will see some MCQ questions on Memory Allocation and Compilation Processes.
Question 1 − What will be the output of the following code −
#include <stdio.h> #include <stdlib.h> 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; printf("%f", u->f); }
Options −
Explanation
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. So answer B is correct.
Question 2 − What is the correct sequence of the compilation Process −
Options −
Explanation −
The option C is correct, At first it preprocess the code, then compile it, after that it creates assembly level code, or object code, then the linking is taken place.
Question 3 − Which of the following statement is true?
Options −
Explanation −
The option B is correct. At first, it creates the preprocessed code, in that phase, it attaches the codes present in file mentioned in #include statements into the code then sent to the compiler.
Question 4 − Purpose of using fflush() function −
Options −
Explanation −
This function is used to flush output stream only. It clears the output buffer and send the output to the console. The option A is correct.
Question 5 − Point out the error of the following code −
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char* ptr; *ptr = (int*)malloc(30); strcpy(ptr, "ABC"); printf("%s", ptr); free(ptr); }
Options −
Explanation −
The option B is correct. Here this makes integer from pointer, without a cast