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

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

Prepare for the ultimate C++ challenge with our comprehensive quiz based on 'Thinking in C++'. Test your knowledge with engaging questions and receive instant feedback. Perfect for both beginners and experienced C++ programmers looking to sharpen their skills!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

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.