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!

Practice this question and more.


What happens if you attempt to modify a const variable?

  1. Compiler warning

  2. Compiler error

  3. Runtime error

  4. Undefined behavior

The correct answer is: Compiler error

A A compiler warning will not stop the program from executing, but it is not possible to modify a constant variable, so it would be futile. C: A runtime error would occur if the program attempted to access or modify a memory location that it does not have permission to, which would not be the case with modifying a const variable. D: Undefined behavior could potentially occur if attempting to modify a const variable, as different compilers may handle this differently. However, the most common and expected outcome would be a compiler error. Therefore, the best answer is B: Compiler error, as attempting to modify a const variable is not allowed and will result in a compilation error.