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.


What is the benefit of declaring a variable as static within a file, outside of any function?

  1. It extends the variable's scope to the whole program.

  2. It limits the variable's visibility to the file it's declared in.

  3. It makes the variable a constant.

  4. It ensures the variable is initialized to zero.

The correct answer is: It limits the variable's visibility to the file it's declared in.

When declaring a variable as static within a file, it limits the variable's visibility to only the file it's declared in. This means that the variable cannot be accessed or modified by other files, making the program more organized and secure. Options A and D are incorrect because static variables do not extend the scope to the whole program nor ensure initialization to zero. Option C is also incorrect because a constant would not be able to be modified at all, making it less useful than a static variable.