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.


How can global variables be accessed across different files?

  1. By using the static keyword.

  2. By declaring them with extern in the other file.

  3. By redefining them in each file.

  4. By including the file where they are defined.

The correct answer is: By declaring them with extern in the other file.

Global variables are defined outside of a function and can be accessed by all functions within a file. To access them across different files, the keyword "extern" must be used in the other file. This tells the compiler that the variable is declared in another file and can be accessed. Option A is incorrect because the static keyword restricts the variable's scope to the file it is defined in. Option C is incorrect because redefining the global variable in each file can cause conflicts and may lead to unexpected results. Option D is also incorrect because including the file where the variables are defined will only allow access within that particular file.