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

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

Study for the C++ exam based on 'Thinking in C++'. Engage with challenging quiz questions designed to boost your understanding and proficiency in C++. Get prepared to master C++ programming concepts through an interactive and informative quiz experience!

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.


When a local static object's constructor is executed?

  1. At program startup

  2. Before the function containing it is first called

  3. After the function containing it returns for the first time

  4. Immediately when the function containing it is defined

The correct answer is: Before the function containing it is first called

Local static objects are objects declared inside a function with the keyword "static". These objects are automatically initialized before any other code is run, but their constructor is only executed when the function containing them is first called. Therefore, options A, C, and D are incorrect because they all refer to a different timing for the constructor's execution. For option A, the program has already started running before the function is called, so the constructor has already been executed. For option C, the function has already returned for the first time, so the constructor cannot be executed. For option D, the function containing the object is not executed until it is called, so the constructor cannot be executed immediately when it is defined.