Which of the following is incorrect in C++?

2015

Which of the following is incorrect in C++?

  1. A.

    when we write overloaded function we must code the function for each usage

  2. B.

    when we write function template we code the function only once

  3. C.

    it is difficult to debug macros

  4. D.

    templates are more efficient than macros

Attempted by 130 students.

Show answer & explanation

Correct answer: D

Answer: templates are more efficient than macros is incorrect.

Why this is incorrect:

  • Templates are not automatically or universally more efficient than macros. Templates provide compile-time type checking and integrate with compiler optimizations such as inlining, but actual runtime performance depends on the generated code and optimizations.

  • Macros are simple text substitutions done by the preprocessor and lack type safety and debugging support. That makes them less safe and harder to maintain, but does not make templates strictly faster in every case.

  • Because performance depends on the code and compiler, saying templates are categorically more efficient is an overgeneralization and therefore incorrect.

Brief notes on the other statements (these are correct):

  • When you write overloaded functions, you must provide an implementation for each overload you intend to use.

  • A function template is written once and the compiler generates specific instantiations for each type used, reducing code duplication.

  • Macros are difficult to debug because they are expanded before compilation and do not preserve type or scope information.

Explore the full course: Mppsc Assistant Professor