Templates Questions with Answers

27 practice questions on Templates (C++) with answers and step-by-step solutions. Try 10 samples below, then practise all 27 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output when compiled as C++17?

#include <iostream>
using namespace std;

#include <utility>

int main() {
    pair p{3, 'x'};
    cout << p.first << p.second;
    return 0;
}
  1. x3
  2. Compilation error
  3. 3120
  4. 3x

Answer: D. 3x

Explanation

C++17 class template argument deduction (CTAD) deduces pair<int, char> from the initialisers, so no template arguments need to be written.

Step-by-step solution

  1. C++17 class template argument deduction works out pair<int, char>.
  2. first = 3, second = 'x'.
  3. Prints 3 then x: 3x.

Why the other options are wrong

  • A. The order is first then second.
  • B. CTAD makes this compile in C++17.
  • C. A char prints as a character, not its code.

Tip: C++17: pair p{3,'x'} just works.

Question 2Easy

In a template parameter list, what is the difference between template <class T> and template <typename T>?

  1. class allows only class types, typename allows any type
  2. typename allows only built-in types
  3. There is no difference; both declare a type parameter
  4. class makes T default to an empty class

Answer: C. There is no difference; both declare a type parameter

Explanation

Both keywords are interchangeable for type parameters; template <class T> also accepts int, double and so on.

Step-by-step solution

  1. For a type parameter, class and typename mean exactly the same thing.
  2. Both can declare T, and both accept int, double and class types.
  3. So there is no difference: option C.

Why the other options are wrong

  • A. Class does not limit T to class types; it accepts any type.
  • B. Typename does not limit T to built-in types; it accepts any type.
  • D. Class does not make T default to an empty class.

Tip: Class and typename are interchangeable for type parameters; typename is needed for dependent names.

Question 3Easy

What is the output?

#include <iostream>
using namespace std;

template <typename... Args>
void count(Args... args) { cout << sizeof...(args); }

int main() {
    count(1, 'a', 2.0, "hi");
    count();
    return 0;
}
  1. 40
  2. Compilation error
  3. 50
  4. 4

Answer: A. 40

Explanation

sizeof... gives the number of elements in a parameter pack: 4 for the first call and 0 for the second.

Step-by-step solution

  1. sizeof...(args) counts the elements of the parameter pack.
  2. count(1, a, 2.0, hi) has four arguments, so it prints 4.
  3. count() has no arguments, so it prints 0.
  4. The output is 40: option A.

Why the other options are wrong

  • B. The variadic template compiles, so the output is 40.
  • C. The first call has four arguments, so the output is 4, not 5.
  • D. The second call prints 0, so the output is 40, not 4.

Tip: sizeof... gives the number of elements in a parameter pack.

Question 4Easy

What is the output?

#include <iostream>
using namespace std;

template <int N>
int times(int x) { return N * x; }

int main() {
    cout << times<3>(4) + times<10>(2);
    return 0;
}
  1. 20
  2. Compilation error
  3. 12
  4. 32

Answer: D. 32

Explanation

N is a non-type template parameter fixed at compile time: 3*4 + 10*2 = 32.

Step-by-step solution

  1. Times with N = 3 returns 3 times 4 = 12.
  2. Times with N = 10 returns 10 times 2 = 20.
  3. The sum is 12 + 20 = 32.
  4. The output is 32: option D.

Why the other options are wrong

  • A. 20 is only the second term; the first term 12 is also added.
  • B. The template argument is a constant, so the code compiles.
  • C. 12 is only the first term; the second term 20 is also added.

Tip: A non-type template argument is a compile-time constant, so each use gives a fixed value.

Question 5Medium

What is the output of this program?

   #include <iostream>
   using namespace std;
   template <class T, int N>
   class mysequence
   {
       T memblock [N];
       public:
       void setmember (int x, T value);
       T getmember (int x);
   };
   template <class T, int N>
   void mysequence<T,N> :: setmember (int x, T value)
   {
       memblock[x] = value;
   }
   template <class T, int N>
   T mysequence<T,N> :: getmember (int x)
   {
       return memblock[x];
   }
   int main ()
   {
       mysequence <int, 5> myints;
       mysequence <double, 5> myfloats;
       myints.setmember (0, 100);
       myfloats.setmember (3, 3.1416);
       cout << myints.getmember(0) << '\n';
       cout << myfloats.getmember(3) << '\n';
       return 0;
   }
  1. 100
  2. 3.1416
  3. 100 3.1416
  4. None of the mentioned

Answer: C. 100 3.1416

Explanation

In this program, We are printing the integer in the first function and float in the second function.

Output:
100
3.1416

Step-by-step solution

  1. Myints stores 100 at index 0, so getmember(0) returns 100.
  2. Myfloats stores 3.1416 at index 3, so getmember(3) returns 3.1416.
  3. Each value is printed on its own line.
  4. The output is 100 3.1416 (on two lines).

Why the other options are wrong

  • A. The double is printed too, so 100 alone is incomplete.
  • B. The int is printed too, so 3.1416 alone is incomplete.
  • D. The program prints two values, so None of the mentioned is wrong.

Tip: Template class instances are separate; each one keeps its own data.

Question 6Medium

What is the output?

#include <iostream>
using namespace std;

template <typename... Ts>
int sum(Ts... xs) { return (xs + ... + 0); }

int main() {
    cout << sum(1, 2, 3, 4) << " " << sum();
    return 0;
}
  1. Compilation error
  2. 10
  3. 10 0
  4. 10 1

Answer: C. 10 0

Explanation

The C++17 binary fold (xs + ... + 0) expands to 1 + (2 + (3 + (4 + 0))). With an empty pack only the initial value 0 remains.

Step-by-step solution

  1. sum(1, 2, 3, 4) expands the fold to 1 + (2 + (3 + (4 + 0))), giving 10.
  2. sum() has an empty pack, so the fold gives only the initial value 0.
  3. Cout prints 10, a space, then 0.
  4. The output is 10 0: option C.

Why the other options are wrong

  • A. The fold with an initial value works for an empty pack, so the code compiles.
  • B. The second value is 0, not blank, so the output has two numbers.
  • D. The empty pack gives 0, not 1.

Tip: A binary fold with an initial value returns that value for an empty pack.

Question 7Medium

Which value is placed in the base class?

  1. derived values
  2. default type values
  3. both a & b
  4. None of the mentioned

Answer: B. default type values

Explanation

We can place the default type values in a base class and overriding some of them through derivation.

Step-by-step solution

  1. The question asks which value is placed in the base class.
  2. The explanation says default type values go in the base class, and derived classes override some of them.
  3. Derived values belong to the derived classes, so option C is also wrong.
  4. So the correct answer is option B, default type values.

Why the other options are wrong

  • A. Derived values are set in the derived classes that override the defaults.
  • C. Option C needs option A to be right, and it is not.
  • D. Option B is the value placed in the base class, so none is wrong.

Tip: Base classes hold defaults; derived classes override only what they need.

Question 8Medium

What is the validity of template parameters?

  1. inside that block only
  2. inside the class
  3. whole program
  4. any of the mentioned

Answer: A. inside that block only

Explanation

The scope of a template parameter starts from the point of its declaration to the end of its template definition.
This implies that you may use the name of a template parameter in other template parameter declarations and their default arguments.

Step-by-step solution

  1. A template parameter's scope starts where it is declared and ends at the end of its template definition.
  2. So the name is valid only inside that template block.
  3. The answer is inside that block only, which is option A.

Why the other options are wrong

  • B. It is not valid across the whole class, only inside its template definition.
  • C. It is not valid across the whole program.
  • D. Option A is correct, so 'any of the mentioned' is wrong.

Tip: A template parameter name lasts only as long as its template declaration.

Question 9Hard

What is the output?

#include <iostream>
using namespace std;

template <typename T>
void f(T) { cout << "T"; }

void f(int) { cout << "N"; }

int main() {
    f(1);
    f(1.0);
    f<>(1);
    f('c');
    return 0;
}
  1. NTTN
  2. TTTT
  3. NTTT
  4. NTNT

Answer: C. NTTT

Explanation

For an exact match the non-template function wins. f(1.0) and f('c') deduce a template that matches exactly (better than a conversion to int), and f<>(1) forces the template.

Step-by-step solution

  1. f(1): the non-template f(int) is an exact match and wins, so N is printed.
  2. f(1.0): the template deduces double and matches exactly, which beats converting to int, so T is printed.
  3. F<>(1) forces the template with T as int, so T is printed; f('c') deduces char exactly, so T is printed.
  4. The output is NTTT: option C.

Why the other options are wrong

  • A. f(1.0) and f('c') pick the template, so the second and fourth outputs are T, not N or T.
  • B. f(1) picks the non-template f(int), so the first output is N.
  • D. f('c') picks the template, so the fourth output is T, not N.

Tip: Overload order: exact non-template, then exact template, then promotions and conversions; f<> forces a template.

Question 10Hard

What is the output?

#include <iostream>
using namespace std;

template <typename T>
struct Holder {
    T t{};
    void ok() { cout << "ok"; }
    void bad() { t.nonexistent(); }
};

int main() {
    Holder<int> h;
    h.ok();
    return 0;
}
  1. okbad
  2. ok
  3. Compilation error
  4. Runtime error

Answer: B. ok

Explanation

Member functions of a class template are instantiated only if they are used. bad() is never called for Holder<int>, so the invalid call int.nonexistent() is never compiled.

Step-by-step solution

  1. Member functions of a class template are instantiated only when used.
  2. Holder<int> uses ok() only.
  3. bad() calls t.nonexistent() but is never instantiated, so no error.
  4. Prints ok.

Why the other options are wrong

  • A. bad() is never called, so nothing from it prints.
  • C. The invalid code is never instantiated, so there is no compile error.
  • D. The program compiles and runs normally.

Tip: Unused template members are never checked against T.

Practise all 27 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C++ topics · All topics