Multifile Programs in C ++ Programming MCQ Questions and Answers
1. In a multifile C++ program, the #include directive is used primarily to:
A) Include only function definitions from another file
B) Include declarations or prototypes from a header file
C) Link multiple object files
D) Execute code from another file
Answer: B
2. In a multifile project, which file typically contains function implementations?
A) Header file (.h)
B) Source file (.cpp)
C) Object file (.o)
D) Makefile
Answer: B
3. The purpose of a header file in C++ is to:
A) Execute all functions directly
B) Store only global variables
C) Provide declarations for use in multiple source files
D) Manage memory allocation
Answer: C
4. When compiling multifile C++ programs, which stage ensures all external symbols are matched?
A) Preprocessing
B) Compilation
C) Linking
D) Execution
Answer: C
5. The keyword extern in C++ is mainly used to:
A) Declare a variable as local
B) Access a global variable defined in another file
C) Allocate dynamic memory
D) Hide a symbol from other files
Answer: B
6. What happens if two different source files define the same global variable without extern?
A) Program runs normally
B) Linker error: multiple definitions
C) Compiler warning only
D) Preprocessor ignores duplicates
Answer: B
7. A header guard in C++ prevents:
A) Infinite loops
B) Memory leaks
C) Multiple inclusions of the same header file
D) Linking errors
Answer: C
8. The correct syntax for a header guard is:
A) #protect FILE_H … #endprotect
B) #ifdef FILE_H … #endif
C) #ifndef FILE_H\n#define FILE_H\n…\n#endif
D) #if FILE_H … #endif
Answer: C
9. If a function is defined in one .cpp file and used in another, it must be:
A) Declared as static
B) Declared as extern in a header file
C) Redeclared in both files
D) Defined again in the using file
Answer: B
10. During compilation of a multifile program, the compiler generates:
A) Executable file directly
B) Object files for each source file
C) Header files
D) Linker scripts
Answer: B
11. Which of the following commands correctly compiles two source files in g++?
A) g++ file1.cpp
B) g++ file1.h file2.h
C) g++ file1.cpp file2.h
D) g++ file1.cpp file2.cpp -o program
Answer: D
12. The .o or .obj files in C++ compilation represent:
A) Final executables
B) Source code backups
C) Machine code output before linking
D) Header files
Answer: C
13. The linker combines:
A) Source and header files
B) Object files and library files
C) Only source code
D) Memory blocks
Answer: B
14. A static global variable in one source file:
A) Can be accessed from other files
B) Is limited to that file only
C) Must be declared extern
D) Causes linker error
Answer: B
15. Which of these is a valid way to include a user-defined header file?
A) #include <myheader>
B) #include “myheader”
C) #include <myheader.h>
D) #include “myheader.h”
Answer: D
16. Which file should contain main() in a multifile C++ project?
A) Exactly one .cpp file
B) Every source file
C) Header file
D) None
Answer: A
17. What will happen if a function is declared but not defined in any source file?
A) Linker error
B) Compiler warning only
C) Program executes partially
D) Preprocessor ignores it
Answer: A
18. Which tool is used to manage and automate compilation of multifile programs?
A) Editor
B) Debugger
C) Makefile / Build system
D) Header guard
Answer: C
19. The #include directive is handled during which compilation phase?
A) Preprocessing
B) Compilation
C) Linking
D) Execution
Answer: A
20. The linker cannot resolve:
A) Syntax errors
B) Undefined external symbols
C) Type mismatches
D) Variable shadowing
Answer: B
21. The .h file typically contains:
A) Only global variables
B) Function definitions
C) Function declarations and class prototypes
D) Object code
Answer: C
22. In a multifile project, each .cpp file is:
A) Ignored unless it contains main()
B) Compiled separately into an object file
C) Linked independently
D) Included as a header
Answer: B
23. To share a constant across multiple files, you should:
A) Declare it in every file
B) Use #define in a .cpp file
C) Define it in one .cpp file and declare it as extern in a header
D) Declare it static
Answer: C
24. If a header file is included twice without guards, what error can occur?
A) Syntax error
B) Redefinition error
C) Runtime crash
D) No issue
Answer: B
25. Which of the following correctly declares an external variable?
A) global int x;
B) static int x;
C) register int x;
D) extern int x;
Answer: D
26. In C++, which type of linkage allows identifiers to be shared between files?
A) Internal linkage
B) Automatic linkage
C) External linkage
D) Static linkage
Answer: C
27. If a function is declared as static in a source file, its visibility is:
A) Global to all files
B) Restricted to that translation unit
C) Accessible from linked libraries
D) Managed by extern
Answer: B
28. The term translation unit in C++ refers to:
A) A linked executable
B) A library file
C) A single source file after preprocessing
D) A class definition only
Answer: C
29. To prevent cyclic inclusions in multifile programs, one should use:
A) Multiple #include directives
B) #define macros
C) Include guards
D) Global variables
Answer: C
30. Which of the following can lead to “undefined reference” errors?
A) Missing function definition in any source file
B) Duplicate header files
C) Typo in include guard name
D) Extra include statements
Answer: A
31. What is the correct order of stages in C++ program compilation?
A) Linking → Compilation → Preprocessing
B) Execution → Linking → Compilation
C) Preprocessing → Compilation → Linking → Execution
D) Compilation → Linking → Preprocessing
Answer: C
32. Suppose add() is defined in math.cpp and declared in math.h. To use it in main.cpp, you should:
A) Copy-paste the code
B) Include math.h in main.cpp
C) Include math.cpp in main.cpp
D) Define add() again in main.cpp
Answer: B
33. Which file extension is NOT used in multifile C++ programming?
A) .cpp
B) .h
C) .hpp
D) .exe (during coding phase)
Answer: D
34. The function prototype must appear in:
A) A header file or before first use in a source file
B) Only the main file
C) Object file
D) After main()
Answer: A
35. The command g++ -c main.cpp will:
A) Link all files
B) Produce executable
C) Generate main.o without linking
D) Delete temporary files
Answer: C
36. Which keyword defines an identifier with internal linkage?
A) extern
B) const
C) static
D) public
Answer: C
37. The linker resolves which kind of names?
A) Local variables
B) Template parameters
C) Global symbols and external references
D) Inline functions
Answer: C
38. If two different source files define the same function name with static, what happens?
A) Conflict occurs
B) No conflict; each has its own copy
C) Linker merges them
D) Compiler rejects both
Answer: B
39. The inclusion of a library like <iostream> is an example of:
A) File linking
B) Header inclusion from system directory
C) Namespace declaration
D) External definition
Answer: B
40. The correct way to share a class definition among files is:
A) Define it in every .cpp
B) Declare it in a header file and include where needed
C) Copy the .cpp content
D) Use extern class
Answer: B
41. The linker cannot detect:
A) Syntax errors inside source files
B) Missing external symbols
C) Undefined functions
D) Multiple symbol definitions
Answer: A
42. Which of the following errors occurs when a symbol is declared but not defined anywhere?
A) Syntax error
B) Runtime exception
C) Linker “undefined reference” error
D) Preprocessor failure
Answer: C
43. A namespace declared in one file can be used in another file if:
A) Defined inside a class
B) Declared in a header included in that file
C) Copied manually
D) Defined as static
Answer: B
44. In multifile programs, inline functions are typically defined in:
A) Header files
B) Separate .cpp files
C) Library binaries
D) Object files only
Answer: A
45. The reason inline functions go in headers is because:
A) They are static by default
B) The compiler must see their definition during compilation
C) They can’t be linked
D) They depend on extern variables
Answer: B
46. Which of the following statements about const variables across multiple files is TRUE?
A) They are automatically extern
B) They have internal linkage by default
C) They cause multiple-definition errors
D) They require dynamic memory
Answer: B
47. To make a const variable accessible from another file, you should:
A) Redefine it in each file
B) Declare it with extern
C) Mark it static
D) Use macros
Answer: B
48. The compilation of multiple C++ files into one executable requires:
A) Each file having main()
B) All object files linked together
C) Each file containing header guards
D) Re-including every .cpp
Answer: B
49. A function defined in multiple .cpp files without static leads to:
A) Compilation success
B) Linker multiple-definition error
C) Memory overflow
D) Preprocessor expansion
Answer: B
50. The primary benefit of multifile program organization is:
A) Faster CPU execution
B) Modular development and easier maintenance
C) Automatic debugging
D) Avoiding compiler optimization
Answer: B
51. In a multifile project, the .obj or .o file contains:
A) Machine code for one translation unit
B) Complete linked executable
C) Intermediate source code
D) Only header information
Answer: A
52. To avoid circular inclusion between two header files, we can use:
A) Multiple inheritance
B) Forward declarations
C) Global definitions
D) Static variables
Answer: B
53. If two header files include each other, the compiler will:
A) Link them automatically
B) Ignore one include
C) Produce recursive inclusion unless guarded
D) Execute sequentially
Answer: C
54. The purpose of a forward declaration in C++ is to:
A) Inform the compiler about a type or function before its definition
B) Link symbols
C) Resolve runtime errors
D) Replace header guards
Answer: A
55. If you declare a class in file1.h and define its member functions in file1.cpp, then:
A) The class can’t be used outside file1.cpp
B) Including file1.h allows use in other .cpp files
C) The linker fails
D) You must redeclare class members
Answer: B
56. Suppose two .cpp files both include the same header with global variable definition. What occurs?
A) Program runs normally
B) Linker error: multiple definitions of variable
C) Compiler silently overrides one
D) Header ignored
Answer: B
57. How do you correctly define a global variable shared among multiple files?
A) Define it in every file
B) Define once in one .cpp file and declare as extern in a header
C) Declare as static everywhere
D) Use #define macros
Answer: B
58. The linker’s job in multifile programs is to:
A) Optimize syntax
B) Combine object files and resolve external references
C) Translate source code to assembly
D) Run the executable
Answer: B
59. The extern keyword affects:
A) Memory allocation
B) Variable linkage and visibility
C) Runtime execution order
D) Header inclusion
Answer: B
60. Which statement about static variables in global scope is TRUE?
A) They cannot be accessed from other source files
B) They are global by default
C) They must be declared extern
D) They are reinitialized automatically
Answer: A
61. What happens if two different functions with identical signatures are defined in different files without static?
A) Linker error due to multiple definitions
B) Both functions coexist
C) One overwrites the other
D) Compiler merges them
Answer: A
62. The make utility in C++ multifile programming helps to:
A) Format code
B) Recompile only modified files automatically
C) Link libraries manually
D) Remove object files
Answer: B
63. A library file in C++ (.lib or .a) contains:
A) Executable code
B) Precompiled object modules for reuse
C) Source code templates
D) Preprocessor directives
Answer: B
64. Dynamic linking in C++ occurs:
A) During preprocessing
B) At runtime
C) At compile time
D) After execution
Answer: B
65. Static linking in C++ occurs:
A) At runtime
B) During execution
C) At link time before creating executable
D) After preprocessing
Answer: C
66. The advantage of dynamic linking is:
A) Faster compile time
B) Smaller executable and runtime flexibility
C) No runtime dependency
D) Inline expansion
Answer: B
67. The namespace keyword in C++ helps multifile projects by:
A) Preventing name collisions across files
B) Reducing file size
C) Improving linking speed
D) Hiding headers
Answer: A
68. A function declared inside a namespace in one file can be used in another by:
A) Redeclaring it
B) Copying its body
C) Using the namespace scope or using directive
D) Declaring it static
Answer: C
69. The using namespace directive should generally appear:
A) Inside headers
B) In source files, not headers
C) Before include guards
D) Inside class definitions
Answer: B
70. The linker produces the final:
A) Header file
B) Object file
C) Executable file
D) Assembly file
Answer: C
71. A C++ project with multiple .cpp files must:
A) Contain one header per file
B) Contain exactly one main() function overall
C) Contain main() in every file
D) Use inline linkage
Answer: B
72. The keyword inline affects:
A) Linking
B) Preprocessing
C) Function call substitution during compilation
D) Memory allocation
Answer: C
73. What happens if a header file defines a non-inline function?
A) The compiler optimizes it
B) Each including source file may get a duplicate definition
C) Linker ignores it
D) The function becomes static
Answer: B
74. To avoid duplicate function definitions, header files should contain:
A) Only declarations, not definitions
B) Complete implementation code
C) Static variable initialization
D) Inline implementations
Answer: A
75. The main reason for splitting C++ programs into multiple files is:
A) Improved modularity, maintainability, and compilation efficiency
B) Reducing CPU usage
C) Automatic memory cleanup
D) Faster runtime execution
Answer: A
76. The “One Definition Rule” (ODR) in C++ states that:
A) A function can have multiple definitions in one file
B) Each object or function must have only one definition across the entire program
C) Multiple declarations are not allowed
D) Macros can appear only once
Answer: B
77. Multiple declarations of the same function in different files are:
A) Disallowed
B) Allowed if only one definition exists
C) Linked automatically
D) Treated as inline
Answer: B
78. The One Definition Rule applies to:
A) Only variables
B) Only templates
C) Functions, variables, classes, and templates
D) Only main() function
Answer: C
79. The linker error “multiple definition of x” violates:
A) Header inclusion
B) The One Definition Rule (ODR)
C) Template specialization
D) Runtime environment
Answer: B
80. Templates in multifile C++ programs should generally be:
A) Defined in .cpp files
B) Defined entirely in header files
C) Declared as extern
D) Declared static
Answer: B
81. Why should template definitions be in header files?
A) Linker cannot resolve them
B) The compiler must see the full definition to instantiate templates
C) They cannot be extern
D) For memory optimization
Answer: B
82. A Makefile rule generally consists of:
A) Declarations only
B) Function prototypes
C) Target, dependencies, and commands
D) Preprocessor macros
Answer: C
83. The command used to clean compiled files in a Makefile is typically:
A) compile
B) clean
C) delete
D) remove
Answer: B
84. Which Makefile symbol represents the current target name?
A) $@
B) $@
C) $<
D) $^
Answer: B
85. In a Makefile, $< stands for:
A) All prerequisites
B) The first dependency
C) Target directory
D) Executable file
Answer: B
86. C++ linkage specification extern “C” is used to:
A) Make functions inline
B) Disable name mangling for C compatibility
C) Enable dynamic linking
D) Hide symbols from linker
Answer: B
87. Name mangling occurs because:
A) Compiler optimization
B) C++ supports function overloading
C) Linker limitations
D) Preprocessor behavior
Answer: B
88. To call a C function from C++ source, you must:
A) Include C headers
B) Use extern “C” declaration
C) Redefine function
D) Use inline directive
Answer: B
89. The file extension .a on Linux typically indicates:
A) Object file
B) Static library archive
C) Dynamic library
D) Executable
Answer: B
90. The file extension .so on Linux typically indicates:
A) Header file
B) Static library
C) Shared (dynamic) library
D) Object file
Answer: C
91. When linking with a shared library, the linker:
A) Records only references to the library symbols
B) Copies all code into the executable
C) Compiles the library again
D) Generates inline definitions
Answer: A
92. Static libraries are linked:
A) During runtime
B) At compile/link time
C) During execution
D) After preprocessing
Answer: B
93. The -I option in g++ command line is used to:
A) Specify libraries
B) Specify include directory paths
C) Define macros
D) Link object files
Answer: B
94. The -L option in g++ command line is used to:
A) Include header files
B) Specify library search directories
C) Compile code
D) Remove files
Answer: B
95. The -l option (lowercase L) in g++ is used to:
A) Link with a specific library by name
B) Include header files
C) Set language mode
D) Run linter
Answer: A
96. Which of the following is NOT recommended in header files?
A) Function prototypes
B) Global variable definitions
C) Macros
D) Class declarations
Answer: B
97. The linker resolves overloaded functions by:
A) Name only
B) Mangled symbol names including parameter types
C) Function bodies
D) File order
Answer: B
98. To avoid multiple inclusion of a header, one can alternatively use:
A) #ifndef and #endif guards
B) #pragma once
C) #protect directives
D) Inline macros
Answer: B
99. The benefit of using #pragma once is:
A) Faster runtime
B) Simplifies include guard management
C) Forces dynamic linking
D) Enables template specialization
Answer: B
100. In a multifile C++ project, successful compilation and linking result in:
A) Object file
B) Executable file ready to run
C) Temporary assembly
D) Library archive
Answer: B
