# MCQ on Memory allocation and compilation process in C

CServer Side ProgrammingProgramming

Here we will see some MCQ questions on Memory Allocation and Compilation Processes.

Question 1 − What will be the output of the following code −

Live Demo

#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

• Garbage Value
• 20.600000
• Syntax Error
• 20.6

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

• During Linking the Code #include replaces by stdio.h
• During Preprocessing the Code #include replaces by stdio.h
• During Execution the Code #include replaces by stdio.h
• During Editing the Code #include replaces by stdio.h

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

• To flush all streams and specified streams
• To flush only specified streams
• To flush input-output buffer
• This is invalid library function

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

• Error in strcpy() statement
• Error in *ptr = (int*)malloc(30);
• Error in free(ptr)
• No Error

Explanation

The option B is correct. Here this makes integer from pointer, without a cast

Published on 21-Oct-2019 16:18:14