Templates and Generic Programming – MCQs July 22, 2025 by u930973931_answers 30 Score: 0 Attempted: 0/30 Subscribe 1. : What is the primary purpose of templates in C++? (A) To reduce runtime errors (B) To enable generic programming (C) To define macros (D) To restrict data types 2. : Which keyword is used to define a function template in C++? (A) generic (B) typename (C) function (D) define 3. : What is required to define a class template? (A) Only return type (B) Data type specifier (C) A template parameter list (D) Object constructor 4. : What is a benefit of using templates? (A) Code repetition (B) Hardcoded data types (C) Type independence (D) Slower execution 5. : How many types of templates are mainly supported in C++? (A) One (B) Two (C) Three (D) Four 6. : Which of the following is not a type of template? (A) Function template (B) Class template (C) Object template (D) Variable template 7. : What does template instantiation mean? (A) Compilation of main() function (B) Replacing function names (C) Creating a function or class for a specific type (D) Creating a new class 8. : Which template parameter allows a class to work with any type? (A) auto (B) this (C) typename T (D) static 9. : Templates allow functions to work with: (A) Only integers (B) Only strings (C) Different data types (D) Only pointers 10. : What does the term “generic programming” imply? (A) Using specific data types (B) Writing code for only one type (C) Writing code that works for any data type (D) Writing macros 11. : What happens if a template function is called with a type that lacks required operators? (A) It executes normally (B) It throws a runtime error (C) It causes a compile-time error (D) It is ignored 12. : Can a template function be overloaded in C++? (A) No (B) Yes, but with same signature (C) Yes, with different parameter types (D) Yes, but only inside a class 13. : Which of the following is not true about templates? (A) Templates improve code reuse (B) Templates increase code duplication (C) Templates allow type flexibility (D) Templates reduce development time 14. : Can templates be used with user-defined data types? (A) No (B) Only with inheritance (C) Yes (D) Only in Java 15. : What does a compiler do when it sees a template function? (A) Executes it (B) Discards it (C) Instantiates it for required types (D) Interprets it 16. : What is a constraint of using templates? (A) They slow down the system (B) All data types must behave similarly (C) Only integers are supported (D) Templates do not support recursion 17. : What is the default behavior of the compiler regarding templates? (A) Compiles all template instantiations (B) Instantiates templates only when needed (C) Ignores templates (D) Always throws warnings 18. : In a class template, what is the syntax to specify the data type? (A) class dataType (B) typename T (C) T (D) auto 19. : When is the actual code for a template function generated? (A) During linking (B) During runtime (C) When the function is called with a specific type (D) During preprocessing 20. : Can class templates have multiple parameters? (A) No (B) Only two (C) Yes (D) Only with functions 21. : Which of the following is a valid template declaration? 22. : What is “template specialization”? (A) Rewriting a function manually (B) Creating a unique version of a template for a specific type (C) Using a template inside a class (D) Using macro-based templates 23. : Which of the following is false about function templates? (A) They reduce code duplication (B) They work only with pointers (C) They are instantiated at compile time (D) They support type inference 24. : Why might a developer use a class template? (A) To improve memory usage (B) To hard-code types (C) To make the class work with multiple types (D) To prevent runtime errors 25. : Can templates be recursive? (A) Yes (B) No (C) Only with compiler flags (D) Only for arrays 26. : What is the major advantage of templates over function overloading? (A) Easier syntax (B) Better runtime performance (C) Type flexibility with less code (D) Templates support only integers 27. : What does type inference mean in the context of templates? (A) Programmer must specify type (B) Compiler determines the type (C) No type is used (D) Runtime type checking 28. : Are templates part of C++ Standard Library? (A) No (B) Only in C++20 (C) Yes (D) Only in GCC 29. : What happens if a template is used incorrectly? (A) The program runs with warnings (B) Compile-time error (C) Runtime error (D) Nothing happens 30. : Can templates be combined with inheritance? (A) No (B) Yes (C) Only with friend functions (D) Only in Java