MCQ on Memory allocation and compilation process in C

Here we will see some MCQ questions on Memory Allocation and Compilation Processes in C programming language.

Question 1: Union Memory Allocation

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("%.6f", u->f);
    free(u);
    return 0;
}

Options

  • A) Garbage Value
  • B) 20.600000
  • C) Syntax Error
  • D) 20.6

Explanation − Option B is correct. In unions, all members share the same memory location. When we assign 20.60f to the float member, printf with %.6f format specifier displays it as 20.600000 (with 6 decimal places by default).

Question 2: Compilation Process Sequence

What is the correct sequence of the C compilation process −

Options

  • 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. The compilation process follows this sequence: First, preprocessing handles directives like #include and #define. Then compilation converts source code to assembly code. Next, the assembler creates object code. Finally, linking combines object files into an executable.

Question 3: Preprocessor Directive Processing

Which of the following statement is true?

Options

  • A) During Linking the Code #include replaces by stdio.h
  • B) During Preprocessing the Code #include replaces by stdio.h
  • C) During Execution the Code #include replaces by stdio.h
  • D) During Editing the Code #include replaces by stdio.h

Explanation − Option B is correct. During the preprocessing phase, the preprocessor replaces #include <stdio.h> with the actual contents of the stdio.h header file, then passes the expanded code to the compiler.

Question 4: fflush() Function Purpose

What is the purpose of using fflush() function −

Options

  • A) To flush all streams and specified streams
  • B) To flush only specified streams
  • C) To flush input-output buffer
  • D) This is invalid library function

Explanation − Option A is correct. The fflush() function forces any buffered output to be written immediately. When called with a specific stream, it flushes that stream. When called with NULL, it flushes all open output streams.

Question 5: Memory Allocation Error

Point out the error in 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);
    return 0;
}

Options

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

Explanation − Option B is correct. The line *ptr = (int*)malloc(30); attempts to dereference an uninitialized pointer and assign it an incompatible type (int* to char). The correct syntax should be ptr = (char*)malloc(30); to allocate memory and assign the address to ptr.

Conclusion

These MCQs cover essential concepts including union memory sharing, compilation process stages, preprocessor operations, buffer management functions, and proper dynamic memory allocation syntax in C programming.

Updated on: 2026-03-15T12:20:41+05:30

578 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements