Templates in C ++ Programming MCQ Questions and Answers
Q1. What is the correct syntax to declare a function template in C++?
A) template function
B) template
C) template
D) template (typename T)
Answer: C
Q2. In a function template, what does the keyword typename signify?
A) Declares a function argument
B) Declares a new class
C) Specifies a placeholder for a data type
D) Specifies a return type
Answer: C
Q3. Which of the following correctly defines a class template?
A) template class Example { };
B) class Example { };
C) class Example(template ) { };
D) template Example { };
Answer: A
Q4. What is the purpose of templates in C++?
A) To increase program speed
B) To enable generic programming
C) To create only specialized functions
D) To replace classes
Answer: B
Q5. Which keyword can be used interchangeably with typename in template declarations?
A) data
B) variable
C) class
D) generic
Answer: C
Q6. A template argument list is enclosed in which symbols?
A) ( )
B) [ ]
C) { }
D) < >
Answer: D
Q7. What is the output type of a template function determined by?
A) Return statement only
B) The type of argument passed
C) Template parameter’s name
D) Function signature
Answer: B
Q8. What happens when a template is instantiated?
A) Compiler generates a version of the function/class for a specific type
B) The template is ignored
C) Runtime polymorphism is applied
D) It executes automatically
Answer: A
Q9. What is template specialization in C++?
A) Declaring multiple templates
B) Providing a specific implementation for a particular type
C) Using templates in multiple files
D) Compiling templates faster
Answer: B
Q10. Which of the following is a correct syntax for template specialization?
A) template
B) template <> class Example { };
C) template <> class Example { };
D) class template <> Example { };
Answer: C
Q11. What is the purpose of non-type template parameters?
A) To pass class names as parameters
B) To allow constants or values as template arguments
C) To limit the number of template types
D) To store runtime values
Answer: B
Q12. Which statement is true about function overloading and templates?
A) Templates cannot be overloaded
B) Function templates cannot coexist with normal functions
C) Templates can be overloaded by using different parameter lists
D) Overloading with templates causes ambiguity always
Answer: C
Q13. When does a function template get compiled?
A) During template declaration
B) When it is instantiated
C) At runtime
D) During linking
Answer: B
Q14. What is the correct way to define a function template with two type parameters?
A) template <typename A; typename B>
B) template
C) template <typename A, typename B>
D) template (typename A, typename B)
Answer: C
Q15. Which one is not a valid template type parameter name?
A) T
B) Type
C) U
D) int
Answer: D
Q16. Which of the following can use templates in C++?
A) Only functions
B) Only classes
C) Only structures
D) Functions, classes, and structures
Answer: D
Q17. Templates help implement what kind of programming paradigm?
A) Functional
B) Generic
C) Object-oriented
D) Procedural
Answer: B
Q18. What is an advantage of using templates?
A) Code duplication increases
B) Compilation becomes faster
C) Reusability for multiple data types
D) Execution speed decreases
Answer: C
Q19. In template declaration, what does template <typename T = int> mean?
A) Specifies a default template argument of type int
B) Declares a new integer variable
C) Restricts the template to integers only
D) Enables runtime type casting
Answer: A
Q20. What will happen if a template function is called with a data type it does not support?
A) It will call a default version
B) It will skip compilation
C) Compilation error occurs
D) It runs with type conversion
Answer: C
Q21. Which header file is required for using templates in C++?
A) iostream
B) templates.h
C) stdtemplate
D) No special header is needed
Answer: D
Q22. How can template classes be defined across multiple files?
A) Using extern keyword
B) Using #include of implementation file
C) By defining implementation in the header file
D) By using inline functions
Answer: C
Q23. What happens if two template functions have the same name and are both viable?
A) One is randomly chosen
B) Compiler uses the most specific match
C) Compilation fails always
D) The template with more parameters wins
Answer: B
Q24. In template metaprogramming, templates are evaluated at:
A) Runtime
B) Compile-time
C) Linking phase
D) Execution phase
Answer: B
Q25. What does STL stand for in context of templates?
A) Simple Template Library
B) Standard Template Library
C) Static Type Library
D) Standard Type Logic
Answer: B
Q26. Which of the following correctly declares a non-type template parameter?
A) template
B) template
C) template
D) template
Answer: B
Q27. What is the correct way to instantiate a class template Box with double type?
A) Box b;
B) Box b;
C) Box(b, double);
D) Box b(double);
Answer: A
Q28. What happens when a template function is called with a user-defined type?
A) It throws an exception
B) It is converted to base type
C) A new version of the function is instantiated for that type
D) It fails compilation
Answer: C
Q29. Which of the following statements about templates is false?
A) Templates support code reuse
B) Templates can have multiple type parameters
C) Templates are resolved during compilation
D) Templates increase runtime overhead
Answer: D
Q30. Which of the following correctly defines a function template for swapping two variables?
A) swap(T &a, T &b)
B) function swap(T &a, T &b)
C) template void swap(T &a, T &b) { T temp = a; a = b; b = temp; }
D) template swap (T &a, T &b)
Answer: C
Q31. Template arguments must be known at which time?
A) Compile-time
B) Runtime
C) Linking time
D) Execution time
Answer: A
Q32. What is the term for using one template inside another?
A) Double instantiation
B) Nested function
C) Template nesting
D) Recursive instantiation
Answer: C
Q33. Which of the following types can be used as non-type template parameters?
A) Floating point constants
B) Strings
C) Integral constants and pointers
D) Objects
Answer: C
Q34. What is the purpose of the template<> prefix in specialization?
A) Declares a default argument
B) Indicates explicit specialization
C) Creates a function overload
D) Declares a macro
Answer: B
Q35. When a function template is called, how does the compiler determine the type?
A) Runtime type checking
B) User input
C) From the argument types provided in the call
D) From return statement
Answer: C
Q36. Which one is a valid syntax for a template with a non-type and type parameter?
A) template <int size, typename T>
B) typename T, int size
C) template <size, typename T>
D) template <typename T, int size>
Answer: D
Q37. What is the correct syntax for defining a template member function outside the class?
A) template void Class::func() { }
B) template void Class::func() { }
C) template void Class::func() { }
D) template Class::func() { }
Answer: B
Q38. Which type of polymorphism do templates implement?
A) Runtime polymorphism
B) Dynamic polymorphism
C) Operator overloading
D) Compile-time polymorphism
Answer: D
Q39. Template instantiation can occur how many times for the same type?
A) Infinite
B) Twice
C) Once per translation unit
D) Never
Answer: C
Q40. What happens if no matching specialization or instantiation exists for a template call?
A) It runs with defaults
B) Compiler chooses random specialization
C) Compilation error occurs
D) Linker substitutes default template
Answer: C
Q41. How can you restrict template instantiation to specific data types?
A) By using typename only
B) By providing template specialization for specific types
C) By overloading constructors
D) By using typedef
Answer: B
Q42. Which keyword is used to explicitly specify template arguments in a call?
A) auto
B) args
C) <>
D) ()
Answer: C
Q43. What is a template parameter pack?
A) A group of macros
B) A feature that allows variable number of template arguments
C) A set of runtime parameters
D) Template file container
Answer: B
Q44. What syntax introduces a template parameter pack?
A) typename pack…
B) template <typename… Args>
C) template
D) template (…Args)
Answer: B
Q45. What is a variadic template?
A) A template that supports only one type
B) Template without parameters
C) Template that accepts a variable number of arguments
D) Nested template
Answer: C
Q46. Which operator is used to unpack a parameter pack?
A) %
B) @
C) *
D) …
Answer: D
Q47. How can you ensure a specific template is preferred over another?
A) By declaring it earlier
B) By making it more specialized
C) By renaming it
D) By compiling separately
Answer: B
Q48. What is partial specialization in class templates?
A) Defining a class template for a subset of template parameters
B) Declaring all templates as default
C) Creating an alias
D) Declaring variable templates
Answer: A
Q49. Which of the following is not allowed in function templates?
A) Default template arguments
B) Partial specialization
C) Explicit instantiation
D) Overloading
Answer: B
Q50. What is the difference between typename and class in templates?
A) They have different meanings
B) One defines only classes
C) They are interchangeable in template parameter lists
D) typename cannot be used
Answer: C
Q51. What is the correct syntax for explicit instantiation of a template function?
A) template void func(int);
B) void func();
C) template func();
D) explicit func();
Answer: A
Q52. What is explicit specialization in templates?
A) Overloading a function template
B) Providing a custom implementation for a specific data type
C) Using templates inside macros
D) Converting a template into a normal function
Answer: B
Q53. Which of the following statements about template instantiation is true?
A) All templates are instantiated automatically
B) Templates are executed before compilation
C) A template is instantiated only when it is used
D) Templates must be manually compiled
Answer: C
Q54. In C++, which container in STL is implemented as a template class?
A) string
B) cout
C) vector
D) math
Answer: C
Q55. What is the type of a std::vector<int> object?
A) A macro
B) A class instantiated from a class template
C) A function template
D) A generic function
Answer: B
Q56. Which STL component is not a template?
A) vector
B) iostream
C) map
D) list
Answer: B
Q57. Which keyword is used to instantiate a template manually?
A) instance
B) new
C) template
D) class
Answer: C
Q58. In a template class, how do you define a member function outside the class?
A) typename Class::func()
B) template void Class::func() { }
C) template void Class::func();
D) template (T) func();
Answer: B
Q59. What is the output of using a template class without specifying type parameters?
A) Default type is used
B) Template automatically infers type
C) Compilation error
D) Class is created as void type
Answer: C
Q60. Which of the following is an example of a function template overload?
A) template void print(T);
template void print(T, T);
B) template void print(T, int);
C) Both A and B
D) Neither A nor B
Answer: C
Q61. What is true about template<class T> and template<typename T>?
A) Only typename is valid
B) Only class is valid
C) class and typename mean different things
D) They are interchangeable
Answer: D
Q62. What is template metaprogramming (TMP)?
A) Runtime programming technique
B) Object-oriented concept
C) Compile-time computation using templates
D) A debugging method
Answer: C
Q63. What is a common use of template metaprogramming?
A) File handling
B) GUI programming
C) Compile-time optimization and type checking
D) Runtime polymorphism
Answer: C
Q64. Which of the following can be computed at compile-time using templates?
A) File operations
B) Factorials or Fibonacci numbers
C) User input
D) Network I/O
Answer: B
Q65. Which feature of templates helps implement compile-time recursion?
A) Overloading
B) Template specialization
C) Virtual functions
D) Inline macros
Answer: B
Q66. What happens if two templates match equally well during instantiation?
A) Random selection
B) Compilation ambiguity error
C) Runtime choice
D) Template overloading
Answer: B
Q67. Which STL class template allows mapping of key-value pairs?
A) vector
B) deque
C) map
D) array
Answer: C
Q68. What is the base concept of STL containers like vector, map, and set?
A) Class inheritance
B) Template-based generic programming
C) Operator overloading
D) Stream manipulation
Answer: B
Q69. What is the type parameter used in std::vector<T> called?
A) Class variable
B) Template argument
C) Instance parameter
D) Runtime variable
Answer: B
Q70. Which of the following STL algorithms is a function template?
A) printf()
B) sort()
C) get()
D) push_back()
Answer: B
Q71. What is the advantage of template-based STL algorithms?
A) They work only with integers
B) They are faster than macros
C) They work with any compatible container or data type
D) They require manual type casting
Answer: C
Q72. What type of polymorphism do STL templates primarily implement?
A) Dynamic polymorphism
B) Virtual polymorphism
C) Static (compile-time) polymorphism
D) None
Answer: C
Q73. What is the template parameter type in std::pair<int, double>?
A) Two type parameters: int and double
B) One integer template
C) A non-type template
D) A specialization
Answer: A
Q74. What is the output of using std::pair<int, int> with two integer values?
A) It throws an exception
B) It stores two integers as a pair
C) It swaps the values
D) It merges them into one
Answer: B
Q75. What will std::tuple<int, char, float> create?
A) A list of integers
B) A template class storing multiple heterogeneous values
C) A string
D) A pair only
Answer: B
Q76. What does SFINAE stand for in the context of templates?
A) Simple Function Invocation Notation for All Expressions
B) Substitution Failure Is Not An Error
C) Static Function Inference Not Allowed Expression
D) Standard Function Inheritance and Execution
Answer: B
Q77. What is the purpose of SFINAE in template programming?
A) To handle runtime errors
B) To enable function overload resolution without hard compilation errors
C) To detect template recursion
D) To perform runtime substitution
Answer: B
Q78. In C++20, which feature formally replaces many uses of SFINAE?
A) Template macros
B) Concepts and requires clause
C) Dynamic templates
D) Virtual templates
Answer: B
Q79. Which of the following correctly defines a simple C++20 concept?
A) concept Numeric = T;
B) concept Numeric;
C) template concept Numeric = std::is_arithmetic_v;
D) concept T : int;
Answer: C
Q80. What is the main goal of “concepts” in templates?
A) Speed up runtime execution
B) Constrain template parameters with type requirements
C) Replace all specializations
D) Reduce memory usage
Answer: B
Q81. Which keyword introduces a constraint in a C++20 template?
A) when
B) requires
C) restrict
D) ensure
Answer: B
Q82. What is the output when a constrained template fails to meet its condition?
A) Program crashes
B) Compile-time error
C) Runtime exception
D) Template ignored silently
Answer: B
Q83. What is the benefit of using “requires clause” in templates?
A) Adds runtime flexibility
B) Improves template readability and compile-time checking
C) Reduces compilation time
D) Enables template recursion
Answer: B
Q84. What happens if multiple templates satisfy the same constraint in C++20?
A) Random selection
B) Most specialized template is chosen
C) Compile-time ambiguity error
D) First declared template is used
Answer: B
Q85. Which of the following is true for dependent names inside templates?
A) They are resolved during parsing
B) They are ignored
C) They are resolved only during template instantiation
D) They cause ambiguity
Answer: C
Q86. What keyword is used to indicate that a dependent name is a type?
A) class
B) typename
C) typename
D) using
Answer: C
Q87. Which of the following is a valid example of template inheritance?
A) class Derived : template Base {};
B) template class Derived : public Base {};
C) template <Base> class Derived {};
D) template class Derived(Base);
Answer: B
Q88. What is the correct syntax for a friend function in a class template?
A) friend void func();
B) template friend void show(const MyClass&);
C) friend func();
D) friend void show();
Answer: B
Q89. Which type of specialization is allowed for class templates only?
A) Explicit
B) Partial
C) Total
D) Inline
Answer: B
Q90. What does template argument deduction mean?
A) Programmer must specify type
B) Compiler ignores type
C) Compiler automatically determines type from function arguments
D) Template is expanded manually
Answer: C
Q91. What is the main restriction of template argument deduction?
A) Works only for integers
B) Cannot deduce array-to-pointer decay or initializer lists easily
C) It always requires typename
D) Works only in classes
Answer: B
Q92. What will be the output of sizeof(std::vector<int>) == sizeof(std::vector<double>)?
A) True (size of container is independent of contained type)
B) False
C) Depends on compiler
D) Causes runtime error
Answer: A
Q93. Which of the following statements about template<class T> is false?
A) T can represent any type
B) Template can have multiple type parameters
C) Template can be specialized
D) T must always be a primitive type
Answer: D
Q94. Which of the following correctly defines a recursive template to compute factorial?
A) template struct Fact { N * Fact::value; };
B) template struct Fact { static int value = Fact::value; };
C) template struct Fact { static const int value = N * Fact::value; }; template <> struct Fact<0> { static const int value = 1; };
D) template struct Fact {};
Answer: C
Q95. What is true about recursive template instantiation?
A) Happens at runtime
B) Occurs during compilation and stops at base specialization
C) Continues infinitely
D) Requires user input
Answer: B
Q96. What kind of programming does template metaprogramming enable?
A) Runtime logic programming
B) Compile-time logic and computation
C) Macro-based execution
D) Event-driven programming
Answer: B
Q97. Which of the following can be used as a non-type template argument in C++17 and above?
A) Floating-point literal
B) Pointer or reference to a variable with external linkage
C) Class name only
D) Local object reference
Answer: B
Q98. Which of the following statements about templates and inline functions is true?
A) Templates cannot be inline
B) Template functions can be inline
C) Inline functions cannot use templates
D) Templates replace inline functions
Answer: B
Q99. What is the main difference between macros and templates?
A) Macros are type-safe
B) Templates are type-safe and checked during compilation
C) Templates are replaced before compilation
D) Macros can handle any type automatically
Answer: B
Q100. Why are templates called “compile-time polymorphism”?
A) They use virtual functions
B) They achieve polymorphic behavior through type substitution during compilation
C) They generate dynamic bindings
D) They use RTTI internally
Answer: B
