Mastering C++: A Comprehensive Quiz Based on 'Thinking in C++'

Disable ads (and more) with a membership for a one time $2.99 payment

Test your C++ skills with our quiz based on Bruce Eckel's 'Thinking in C++'. Dive into object-oriented programming, advanced topics, and fundamentals. Perfect for learners and experts alike. Assess your knowledge and become a C++ master!

Practice this question and more.


Why might you use a global static object?

  1. To ensure the object is only accessible within one function

  2. To execute code before entering and after exiting main()

  3. To avoid initializing the object in each function call

  4. To prevent the object from being accessed by other translation units

The correct answer is: To execute code before entering and after exiting main()

A global static object can be used to execute code before entering and after exiting main(), ensuring that the code within the object is always run. Options A, C, and D do not fully address the purpose of using a global static object in this way. Option A mentions the object being accessible within one function, which does not align with the concept of a global object. Option C mentions avoiding initialization in each function call, which may be a reason for using a static object, but doesn't specifically address the global aspect. Option D mentions preventing access by other translation units, which can be achieved using the static keyword, but does not explain the purpose of using the object in this way. Therefore, option B is the most appropriate answer.