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

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

Challenge your C++ knowledge with a comprehensive quiz based on 'Thinking in C++'. Explore a variety of multiple choice questions with hints and explanations to solidify your understanding. Get prepared!

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.


What best describes the scope of a static variable in a C++ function?

  1. Global scope

  2. Class scope

  3. Namespace scope

  4. Block scope, but with static storage duration

The correct answer is: Block scope, but with static storage duration

A static variable in a C++ function has block scope, meaning that it is only accessible within the function in which it is defined. However, it also has a static storage duration, which means it retains its value even after the function has finished executing. So, while it may seem similar to a global variable in terms of accessibility, it is still local to the function and cannot be accessed from other parts of the code. The other options - global scope, class scope, and namespace scope - are incorrect because they do not fully encompass the unique combination of properties that a static variable in a C++ function has.