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!

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 is a static variable inside a function initialized?

  1. Every time the function is called

  2. Only the first time the function is invoked

  3. When the program ends

  4. It is not initialized automatically

The correct answer is: Only the first time the function is invoked

A static variable inside a function is only initialized the first time the function is invoked, meaning it will retain its value even after the function has completed. Option A is incorrect because a static variable declared inside a function will not be initialized every time the function is called. Option C is incorrect because a static variable will not be initialized when the program ends, it will retain its last assigned value. Option D is incorrect because while a static variable is not automatically initialized, it can be manually initialized in the function. Therefore, option B is the correct answer.