“Program Structure, Compilation, and Execution” in C Language, specially designed for GATE-level difficulty.
Each question includes:
- 4 answer options (A, B, C, D)
- Correct answer
- Detailed explanation (with reasoning + conceptual insight)
- Changed numerical values (where applicable)
1.
Which of the following is the correct order of phases in the C program compilation process?
A) Preprocessing → Linking → Compilation → Assembling
B) Compilation → Assembling → Linking → Execution
C) Preprocessing → Compilation → Assembling → Linking
D) Linking → Compilation → Preprocessing → Execution
✅ Answer: C
Explanation:
The compilation process in C consists of:
- Preprocessing – Handles directives (
#include,#define) - Compilation – Converts source code to assembly
- Assembling – Converts assembly to object code
- Linking – Combines object files and libraries
- Execution – Runs the executable file
2.
In C, the file extension of the preprocessed output is usually:
A) .exe
B) .o
C) .i
D) .asm
✅ Answer: C
Explanation:
The preprocessor outputs a file with .i extension before compilation. Example:gcc -E main.c -o main.i → generates the preprocessed file.
3.
Which of the following statements about the C compilation stages is false?
A) Linking resolves external symbols.
B) Assembler translates assembly code to machine code.
C) Compiler directly generates the executable file.
D) Preprocessor handles macros and header inclusion.
✅ Answer: C
Explanation:
The compiler produces an object file, not an executable. The linker creates the final executable.
4.
Which component in C replaces macros and includes header files?
A) Compiler
B) Linker
C) Preprocessor
D) Loader
✅ Answer: C
Explanation:
The preprocessor runs before compilation. It expands macros and includes header contents.
5.
What is the correct command to compile test.c into an executable output using GCC?
A) gcc -E test.c -o output
B) gcc -S test.c -o output
C) gcc test.c -o output
D) gcc -c test.c -o output
✅ Answer: C
Explanation:gcc test.c -o output compiles, assembles, and links in one go to generate an executable file named output.
6.
Which of these phases produces an object file?
A) Compilation
B) Linking
C) Assembling
D) Preprocessing
✅ Answer: C
Explanation:
The assembler converts assembly language output from the compiler into an object file (.o or .obj).
7.
The entry point of a C program is determined by:
A) The first user-defined function
B) The linker
C) The main() function
D) The compiler
✅ Answer: C
Explanation:
Every C program begins execution from the main() function, irrespective of its position in the file.
8.
What is the purpose of the return 0; statement in main()?
A) Indicates successful program termination
B) Frees allocated memory
C) Ends all threads
D) Sends output to the screen
✅ Answer: A
Explanation:
Returning 0 signals successful completion to the operating system. Non-zero values indicate errors.
9.
What is the output of the following program?
#include <stdio.h>
int main() {
printf("GATE");
return 0;
}
A) GATE
B) gate
C) Compiler error
D) No output
✅ Answer: A
Explanation:
A simple program structure with #include <stdio.h> and a valid main() prints “GATE”.
10.
In the C compilation process, what happens during the linking stage?
A) Syntax checking
B) Macro expansion
C) Object file combination
D) Code optimization
✅ Answer: C
Explanation:
The linker merges object files, resolves external references, and creates an executable file.
11.
Which of the following C statements is executed first by the compiler?
A) #include<stdio.h>
B) int main()
C) printf("Hello");
D) return 0;
✅ Answer: A
Explanation:
Preprocessor directives like #include are executed before actual compilation begins.
12.
Which of the following phases checks for syntax errors?
A) Compiler
B) Preprocessor
C) Linker
D) Assembler
✅ Answer: A
Explanation:
The compiler parses code to check for grammar/syntax violations before converting it to assembly.
13.
Which of the following tools performs symbol resolution?
A) Compiler
B) Assembler
C) Linker
D) Loader
✅ Answer: C
Explanation:
The linker resolves undefined symbols (like function calls) by connecting them to their definitions in libraries or other object files.
14.
If a program includes #define PI 3.14, when does this substitution occur?
A) During execution
B) During linking
C) During preprocessing
D) During compilation
✅ Answer: C
Explanation:
Macros are replaced before compilation by the preprocessor.
15.
Which of the following is not a valid stage of C program execution?
A) Linking
B) Debugging
C) Preprocessing
D) Assembling
✅ Answer: B
Explanation:
Debugging is a post-compilation activity; it’s not part of the compilation/execution pipeline.
16.
Which of the following GCC commands will stop after generating assembly code?
A) gcc -E file.c
B) gcc -S file.c
C) gcc -c file.c
D) gcc file.c -o file
✅ Answer: B
Explanation:-S stops the process after producing assembly output (file.s).
Other flags:
-E→ preprocessing only-c→ generates object file-o→ specifies executable name
17.
During program execution, local variables are stored in which memory segment?
A) Heap
B) Stack
C) Code segment
D) Data segment
✅ Answer: B
Explanation:
Local variables are stored in the stack, which grows and shrinks during function calls.
18.
What happens if the main() function in a C program does not return any value?
A) Compiler error
B) Implicitly returns zero
C) Undefined behavior
D) Linker warning
✅ Answer: B
Explanation:
If main() is declared as int main() and omits a return value, C99 standard assumes return 0;.
19.
Which of these files is not directly generated during compilation?
A) .o
B) .exe
C) .h
D) .s
✅ Answer: C
Explanation:.h is a header file written manually; not generated during compilation.
20.
Which stage translates assembly code into object code?
A) Preprocessor
B) Assembler
C) Compiler
D) Linker
✅ Answer: B
Explanation:
The assembler converts .s → .o (machine code) before linking.
21.
What will the following code output?
#include <stdio.h>
int main() {
printf("%d", 5 + 3 * 2);
}
A) 11
B) 16
C) 13
D) Compilation error
✅ Answer: A
Explanation:
Operator precedence applies → 3*2=6, then 5+6=11. Program runs successfully.
22.
Which of the following is a valid structure of a minimal C program?
A)
int main {}
B)
main();
C)
int main() {}
D)
void main[] {}
✅ Answer: C
Explanation:
A valid main() requires parentheses and curly braces.
23.
Which statement about header files is true?
A) Header files contain compiled code.
B) Header files contain function declarations.
C) Header files are linked by the assembler.
D) Header files are loaded at runtime.
✅ Answer: B
Explanation:
Header files contain declarations, not definitions, and are included by the preprocessor.
24.
If you compile a C file using gcc -c file.c, what will you get?
A) Assembly code
B) Object file
C) Executable
D) Preprocessed file
✅ Answer: B
Explanation:
The -c flag stops compilation after generating an object file (.o).
25.
Which tool links object files into a single executable?
A) Preprocessor
B) Linker
C) Compiler
D) Loader
✅ Answer: B
Explanation:
The linker combines multiple .o files and resolves external references.
26.
The loader is responsible for:
A) Checking syntax
B) Loading executable into memory
C) Optimizing machine code
D) Generating assembly
✅ Answer: B
Explanation:
The loader loads the executable into main memory for execution.
27.
Which phase detects undeclared variables?
A) Preprocessor
B) Compiler
C) Linker
D) Loader
✅ Answer: B
Explanation:
The compiler checks for declaration errors during syntax and semantic analysis.
28.
What is the default return type of main() if not specified?
A) void
B) int
C) float
D) undefined
✅ Answer: B
Explanation:
By default, main() is assumed to return int, though explicitly declaring it is best practice.
29.
Which of the following statements about linking is incorrect?
A) It resolves external references.
B) It removes unused variables.
C) It combines multiple object files.
D) It generates the executable file.
✅ Answer: B
Explanation:
Linking does not remove unused variables. That’s an optimization job.
30.
Which phase uses relocation tables?
A) Linker
B) Assembler
C) Compiler
D) Loader
✅ Answer: A
Explanation:
Relocation tables help the linker adjust addresses when combining object files.
31.
Which of the following is not a valid storage class in C?
A) static
B) extern
C) local
D) register
✅ Answer: C
Explanation:local is not a valid keyword; “local variable” is a concept, not a storage class.
32.
Which option produces only the preprocessed output?
A) gcc -E file.c
B) gcc -S file.c
C) gcc -c file.c
D) gcc file.c
✅ Answer: A
Explanation:-E option stops after preprocessing. It shows macros and included headers.
33.
In a multi-file program, missing function definitions cause an error during:
A) Compilation
B) Linking
C) Execution
D) Preprocessing
✅ Answer: B
Explanation:
The linker reports unresolved external symbols if a definition is missing.
34.
The printf() function is resolved during which phase?
A) Compilation
B) Linking
C) Preprocessing
D) Execution
✅ Answer: B
Explanation:printf() is in the C standard library, resolved during the linking stage.
35.
Which of the following is true for static functions?
A) They are globally accessible.
B) They are accessible within a single file only.
C) They cannot have local variables.
D) They must return int.
✅ Answer: B
Explanation:static functions have file scope — accessible only within their translation unit.
36.
What is the correct order for C program memory segments during execution?
A) Stack → Heap → Code → Data
B) Code → Data → Heap → Stack
C) Heap → Stack → Data → Code
D) Data → Code → Heap → Stack
✅ Answer: B
Explanation:
Program memory layout:
Code (text) → Data → Heap (grows upward) → Stack (grows downward)
37.
The .text segment of a program contains:
A) Initialized data
B) Source code
C) Executable instructions
D) Global variables
✅ Answer: C
Explanation:.text contains machine instructions (compiled code).
38.
Uninitialized global variables are stored in:
A) Heap
B) Stack
C) .data segment
D) .bss segment
✅ Answer: D
Explanation:
The .bss section stores uninitialized global/static variables.
39.
Which part of memory is dynamically allocated at runtime?
A) Heap
B) Stack
C) Data segment
D) Code segment
✅ Answer: A
Explanation:
Dynamic memory (via malloc(), calloc()) is from the heap.
40.
What is produced after successful linking?
A) Executable file
B) Object file
C) Assembly code
D) Symbol table
✅ Answer: A
Explanation:
The linker produces the final executable (.exe or a.out).
41.
Which of the following segments is shared among multiple processes?
A) Stack segment
B) Heap segment
C) Text segment
D) BSS segment
✅ Answer: C
Explanation:
The text segment (code section) is read-only and can be shared among processes to save memory.
42.
What does the linker do when the same symbol is defined in multiple files?
A) Ignores the duplicates
B) Generates a multiple-definition error
C) Overwrites the first symbol
D) Randomly selects one definition
✅ Answer: B
Explanation:
If two global symbols have the same name, the linker reports a multiple definition error.
43.
If you compile with gcc -Wall file.c, the flag -Wall means:
A) Compile without linking
B) Enable all standard warnings
C) Optimize for speed
D) Display assembly
✅ Answer: B
Explanation:-Wall enables all common compiler warnings for better code quality and debugging.
44.
Which command line argument in GCC compiles but does not link?
A) -E
B) -S
C) -c
D) -o
✅ Answer: C
Explanation:
The -c flag stops compilation after producing the object file.
45.
In the C compilation pipeline, which stage converts high-level code to assembly?
A) Preprocessor
B) Compiler
C) Linker
D) Assembler
✅ Answer: B
Explanation:
The compiler converts C source code into assembly language.
46.
The .bss section in a program contains:
A) Initialized global variables
B) Uninitialized global variables
C) Function code
D) Constant literals
✅ Answer: B
Explanation:.bss = “Block Started by Symbol” → holds uninitialized global/static variables.
47.
What is the output of the following code?
#include <stdio.h>
int a;
int main() {
printf("%d", a);
return 0;
}
A) Garbage value
B) 0
C) Compiler error
D) Undefined behavior
✅ Answer: B
Explanation:
Global variables are automatically initialized to zero.
48.
Which type of linking occurs at program load time?
A) Static linking
B) Dynamic linking
C) Relocatable linking
D) Partial linking
✅ Answer: B
Explanation:
Dynamic linking loads external libraries at runtime using the loader (e.g., .dll, .so).
49.
Which of these occurs before linking?
A) Syntax check
B) Symbol resolution
C) Relocation
D) Library binding
✅ Answer: A
Explanation:
Syntax checking happens during compilation, before linking begins.
50.
What is the output?
#include <stdio.h>
int main() {
int a;
printf("%d", a);
}
A) 0
B) Garbage value
C) Compiler error
D) Undefined output
✅ Answer: B
Explanation:
Local variables in C are not initialized by default — they contain garbage values.
51.
Which of the following is responsible for binding symbolic names to addresses?
A) Compiler
B) Linker
C) Assembler
D) Loader
✅ Answer: D
Explanation:
The loader maps symbols to physical memory addresses during program loading.
52.
When does the preprocessor remove comments from code?
A) During compilation
B) During preprocessing
C) During linking
D) During assembling
✅ Answer: B
Explanation:
Comments are stripped out by the preprocessor before compilation.
53.
Which of these is not handled by the linker?
A) Relocation
B) Symbol resolution
C) Macro expansion
D) Library inclusion
✅ Answer: C
Explanation:
Macro expansion is a preprocessor job, not linker’s.
54.
What is the function of the loader?
A) Converts C code to object code
B) Combines object files
C) Loads program into main memory
D) Optimizes executable
✅ Answer: C
Explanation:
The loader places the executable in memory and initializes the stack and heap for execution.
55.
Which flag in GCC enables optimization?
A) -O
B) -E
C) -Wall
D) -c
✅ Answer: A
Explanation:-O, -O1, -O2, -O3 enable different optimization levels during compilation.
56.
The .data section contains:
A) Constant literals
B) Initialized global/static variables
C) Function code
D) Uninitialized variables
✅ Answer: B
Explanation:
The .data segment stores initialized global/static variables.
57.
The C library function printf() is found in which phase?
A) Compilation
B) Linking
C) Execution
D) Preprocessing
✅ Answer: B
Explanation:printf()’s definition resides in libc and is linked during the linking phase.
58.
Which of the following is not a valid step in compilation?
A) Preprocessing
B) Linking
C) Parsing
D) Debugging
✅ Answer: D
Explanation:
Debugging occurs after compilation — not part of the compilation pipeline.
59.
What is the result of compiling without a main() function?
A) Compilation error
B) Linking error
C) Run-time error
D) Undefined behavior
✅ Answer: B
Explanation:
The compiler passes, but the linker fails since it cannot find the entry point (main()).
60.
When #include <stdio.h> is processed, its contents are:
A) Compiled directly
B) Copied into source file
C) Linked from library
D) Ignored by compiler
✅ Answer: B
Explanation:
During preprocessing, the file’s contents are literally copied into the source code.
61.
Which command compiles and links in one step?
A) gcc -E
B) gcc -S
C) gcc file.c -o output
D) gcc -c
✅ Answer: C
Explanation:
Without -E, -S, or -c, GCC runs all phases: preprocessing, compilation, assembling, linking.
62.
What is the role of the symbol table?
A) Stores runtime data
B) Maps identifiers to attributes
C) Contains object file addresses
D) Stores executable code
✅ Answer: B
Explanation:
A symbol table holds identifiers, their types, memory addresses, and scope.
63.
What does the following code print?
#include <stdio.h>
int main() {
static int x;
printf("%d", x);
}
A) Garbage value
B) 0
C) Compiler error
D) Undefined
✅ Answer: B
Explanation:
Static variables (even local) are initialized to zero by default.
64.
What happens if you compile without including required header files?
A) Compilation succeeds
B) Linking fails
C) Syntax error
D) Undefined symbols at linking
✅ Answer: D
Explanation:
Compiler assumes implicit declarations, but linking fails due to undefined external references.
65.
What is the first function executed before main() in C?
A) __start()
B) _init()
C) _start()
D) begin()
✅ Answer: C
Explanation:
The OS starts execution at _start() (defined by CRT) before calling main().
66.
Which of these is a valid comment in C?
A) // Comment
B) /* Comment */
C) Both A and B
D) # Comment
✅ Answer: C
Explanation:
C supports both single-line (//) and multi-line (/…/) comments.
67.
Which phase performs syntax tree generation?
A) Preprocessor
B) Compiler
C) Assembler
D) Linker
✅ Answer: B
Explanation:
The compiler parses code into an Abstract Syntax Tree (AST) for semantic checking.
68.
What does the loader initialize during program load?
A) Code section
B) Stack, heap, and data sections
C) Only text segment
D) Symbol table
✅ Answer: B
Explanation:
The loader initializes stack, heap, and data segments before execution.
69.
Which phase resolves function calls between different source files?
A) Compiler
B) Linker
C) Loader
D) Assembler
✅ Answer: B
Explanation:
Inter-file references are resolved by the linker.
70.
Which command shows all compilation stages in GCC?
A) gcc -v file.c
B) gcc -Wall file.c
C) gcc -E file.c
D) gcc -O2 file.c
✅ Answer: A
Explanation:-v flag displays verbose output, including all compilation stages.
71.
The extern keyword in C indicates:
A) Local variable
B) Variable defined elsewhere
C) Global static variable
D) Constant variable
✅ Answer: B
Explanation:extern declares a variable defined in another file or translation unit.
72.
What is the output?
#include <stdio.h>
int main() {
printf("Size of main: %d", sizeof(main));
}
A) Compiler error
B) Size of function pointer
C) 0
D) Undefined behavior
✅ Answer: B
Explanation:
Functions decay to pointers, so it prints the size of a pointer (e.g., 8 bytes on 64-bit).
73.
Which of the following occurs at runtime?
A) Parsing
B) Linking
C) Memory allocation
D) Optimization
✅ Answer: C
Explanation:
Memory allocation (malloc, calloc) happens during execution, not compilation.
74.
What does gcc -o do?
A) Optimize code
B) Specify output file name
C) Preprocess only
D) Display errors
✅ Answer: B
Explanation:-o allows you to name the output file (e.g., gcc file.c -o program).
75.
Which component ensures that the OS can start executing the program?
A) Compiler
B) Linker
C) Loader
D) Preprocessor
✅ Answer: C
Explanation:
The loader maps the program into memory and hands control to the OS for execution.
76.
Which of the following correctly describes the order of compilation stages in C?
A) Preprocessing → Compilation → Assembling → Linking
B) Compilation → Preprocessing → Assembling → Linking
C) Assembling → Preprocessing → Linking → Compilation
D) Linking → Assembling → Preprocessing → Compilation
✅ Answer: A
Explanation:
C programs go through Preprocessing → Compilation → Assembling → Linking before execution.
77.
Which file extension typically represents an object file after compilation?
A) .exe
B) .obj or .o
C) .asm
D) .h
✅ Answer: B
Explanation:
After compilation, each source file is translated into an object file with .o (Linux) or .obj (Windows).
78.
Which of the following is NOT part of the translation unit in C?
A) Source file
B) Included header files
C) Macro expansions
D) Object file
✅ Answer: D
Explanation:
A translation unit is formed by the source code + headers + macros after preprocessing.
The object file is generated after compilation.
79.
If two source files define the same global variable without extern, what happens?
A) Compilation error
B) Linking error
C) Runtime error
D) No error
✅ Answer: B
Explanation:
Duplicate global symbols cause a multiple definition linking error.
80.
What will the following code print?
#include <stdio.h>
int x = 10;
void func() { x = 20; }
int main() {
func();
printf("%d", x);
return 0;
}
A) 10
B) 20
C) Garbage value
D) Compile-time error
✅ Answer: B
Explanation:x is a global variable modified by func(), so it prints 20.
81.
What is the output?
#include <stdio.h>
int main() {
const int a = 5;
int *p = (int *)&a;
*p = 10;
printf("%d", a);
}
A) 5
B) 10
C) Undefined behavior
D) Compile-time error
✅ Answer: C
Explanation:
Modifying a const variable using a pointer leads to undefined behavior.
82.
Which of the following functions is executed automatically before main()?
A) _start()
B) init()
C) atexit()
D) main_pre()
✅ Answer: A
Explanation:
In C runtime, the OS loader calls _start(), which then calls __libc_start_main() and finally main().
83.
Which section of memory contains the function call stack?
A) Data segment
B) Stack segment
C) Heap segment
D) Code segment
✅ Answer: B
Explanation:
Stack segment holds function call frames, local variables, and return addresses.
84.
Which option shows the correct memory allocation order (lowest → highest addresses) in most systems?
A) Stack → Heap → Data → Code
B) Code → Data → Heap → Stack
C) Data → Code → Heap → Stack
D) Stack → Code → Data → Heap
✅ Answer: B
Explanation:
Memory layout (low→high): Code → Data → Heap → Stack (stack grows downward).
85.
Which file is produced after the assembler phase?
A) Source file
B) Object file
C) Executable file
D) Header file
✅ Answer: B
Explanation:
Assembler converts assembly code to an object file (.o/.obj).
86.
Which option best describes dynamic linking?
A) Libraries linked during compilation
B) Libraries linked during runtime
C) Libraries linked during preprocessing
D) Libraries linked during syntax checking
✅ Answer: B
Explanation:
Dynamic linking loads external library routines during program execution (e.g., using .so or .dll).
87.
What is the output of this code?
#include <stdio.h>
int main() {
printf("%d", printf("Gate"));
return 0;
}
A) Gate4
B) Gate5
C) Gate3
D) Gate0
✅ Answer: A
Explanation:
The inner printf("Gate") prints 4 chars and returns 4, which is printed by the outer printf().
88.
What happens when the following code runs?
#include <stdio.h>
int main() {
static int x = 5;
if (x--) {
main();
printf("%d", x);
}
}
A) Infinite recursion
B) 43210
C) 00000
D) Compilation error
✅ Answer: B
Explanation:x is static, shared across calls. Prints 43210 after recursion unwinds.
89.
Which memory region is used for dynamically allocated variables?
A) Stack
B) Heap
C) Code segment
D) Data segment
✅ Answer: B
Explanation:malloc() and calloc() allocate memory in the heap.
90.
What does the following produce?
#define PI 3.14
int main() {
printf("%f", PI);
}
A) 3.14
B) PI
C) Error
D) Garbage
✅ Answer: A
Explanation:
Macro replaces PI with 3.14 during preprocessing.
91.
Which command displays all symbols of an object file in Linux?
A) ld
B) objdump
C) nm
D) ar
✅ Answer: C
Explanation:nm file.o lists all symbols (variables/functions) in the object file.
92.
Which keyword helps avoid multiple inclusion of header files?
A) #define
B) #pragma once
C) #ifdef
D) Both B and C
✅ Answer: D
Explanation:#pragma once or include guards (#ifndef HEADER_H ... #endif) prevent multiple inclusions.
93.
What does the linker do if it finds an undefined symbol?
A) Ignores it
B) Replaces it with 0
C) Throws an error
D) Automatically defines it
✅ Answer: C
Explanation:
An undefined symbol error occurs if an external symbol is not found during linking.
94.
Which of these can cause undefined reference at link time?
A) Missing header
B) Missing object file
C) Syntax error
D) Preprocessor macro error
✅ Answer: B
Explanation:
If a function’s definition is in another source file not linked, linker raises undefined reference.
95.
What happens if two static functions have the same name in different source files?
A) Linker error
B) Compiler error
C) No error
D) Runtime crash
✅ Answer: C
Explanation:static limits visibility to the file, so no conflict across files.
96.
Which of the following files is produced before linking?
A) Source file
B) Object file
C) Executable file
D) Library file
✅ Answer: B
Explanation:
Each source file compiles to an object file first, then the linker combines them.
97.
Which of the following segments stores string literals?
A) Stack
B) Heap
C) Data segment (RO)
D) BSS
✅ Answer: C
Explanation:
String literals are stored in read-only data segment.
98.
What will this print?
#include <stdio.h>
int main() {
printf("%d", main);
}
A) Compiler error
B) Address of main
C) 0
D) Undefined behavior
✅ Answer: B
Explanation:main decays to a function pointer, printing its address (implementation-defined format).
99.
Which of the following correctly matches?
| Phase | Tool Used |
|---|---|
| Preprocessing | cpp |
| Compilation | cc1 |
| Assembling | as |
| Linking | ld |
A) All correct
B) Only first two correct
C) Only last two correct
D) None correct
✅ Answer: A
Explanation:
These are the exact GNU tools used for each compilation stage in GCC.
100.
In C, what is the purpose of the startup code (crt0)?
A) Defines macros
B) Initializes runtime environment before main()
C) Compiles standard libraries
D) Optimizes linking
✅ Answer: B
Explanation:crt0.o (C runtime startup) sets up stack, heap, and environment before calling main().