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.


Why should you prefer const over #defines for value substitution in C++?

  1. For type safety

  2. For simplicity

  3. For memory efficiency

  4. For compatibility with C

The correct answer is: For type safety

Using constants declared with the keyword "const" is an overall better practice compared to using #define macros for value substitution in C++. This is because const values have defined types, and are evaluated at the time of declaration, while #defines are evaluated during preprocessing. This means that #defines have no type and their values are not checked for correctness until runtime. Additionally, const values can have functions applied to them, while #defines cannot. Option B is incorrect because const values do not necessarily make the code simpler, but they do make it more predictable and maintainable. Option C is also incorrect because #defines do not use memory as they are directly replaced with their values during preprocessing. Option D is incorrect because C++ has its own way of defining constants using the keyword "const", so compatibility with C is not a valid reason to prefer #defines over const.