Difficulty: Medium
Correct Answer: Define a global object whose constructor prints Hello, so that the constructor runs before main starts
Explanation:
Introduction / Context:
In C plus plus and, to a limited extent, in C with certain extensions, some code can run before the main function begins. This happens during the initialization of global and static objects. Many interview questions test whether candidates understand that constructors of global objects execute before main and can therefore be used for early initialization or logging. This question asks which technique allows printing the message Hello before main executes.
Given Data / Assumptions:
Concept / Approach:
In C plus plus, when you define a global object of a class that has a constructor, the constructor runs during program start up, before main is called. If the constructor includes code that prints Hello to standard output, that message will appear before any code in main executes. This approach relies on the language rule that static and global objects are initialized before entering main. Other options such as macros or an ordinary printf inside main do not satisfy the requirement of executing earlier than main.
Step-by-Step Solution:
Step 1: Identify features that execute before main. Global object constructors are one such feature in C plus plus.
Step 2: Option a describes defining a global object whose constructor prints Hello. This directly uses the pre main initialization phase.
Step 3: Option b simply places printf inside main, which will only execute when main runs, not before.
Step 4: Option c mentions only macros, which are expanded by the preprocessor and do not guarantee execution before main unless used in a constructor or similar.
Step 5: Option d suggests calling main manually before program start up, which does not make sense because the runtime system decides when main is first entered.
Step 6: Option e incorrectly claims that no code can run before main, ignoring global initialization behavior.
Verification / Alternative check:
You can test this in C plus plus with code that defines a class with a constructor containing a call to printf or cout, then declare a global instance of that class. When you compile and run the program, you will see the constructor output before any output from main. Many textbooks use this pattern to illustrate static initialization order. Although more advanced techniques exist, such as compiler specific attributes, the global constructor example is a standard and portable method.
Why Other Options Are Wrong:
Option b does not satisfy the condition of running before main. Option c overestimates what macros can do, since they simply transform text at compile time. Option d misunderstands how main is invoked by the runtime. Option e fails to recognize the well defined initialization phase that occurs before main.
Common Pitfalls:
A pitfall is to overuse global objects for complex initialization, which can create hard to debug order dependencies between different translation units. Another issue is assuming that the order of initialization across different compilation units is always predictable. For this interview question, however, the focus is simply on recognizing that global constructors are executed before main.
Final Answer:
The required behavior can be achieved by defining a global object whose constructor prints Hello, so that the constructor runs before main starts, as described in option a.
Discussion & Comments