AlgoMaster Logo

Templates - Quiz

Last Updated: December 6, 2025

1 min read

Templates Quiz

28 quizzes

1
Multiple Choice

Which declaration correctly defines a generic max function template for any comparable type?

2
Multiple Choice

How does the compiler choose which function template to instantiate?

3
Multiple Choice

What is a key benefit of using a class template like std::vector<T>?

4
Multiple Choice

Which statement about full template specialization is correct?

5
Multiple Choice

What does typename... Args mean in a variadic template?

6
Multiple Choice

Which C++17 feature simplifies operating over all arguments of a variadic template?

7
Multiple Choice

In template metaprogramming, what does static_assert typically provide?

8
Multiple Choice

Which SFINAE-related behavior is correct?

9
Multiple Choice

Why might you specialize a template for pointer types?

10
Multiple Choice

Which is a practical use of a compile-time Factorial<N> template?

11
Code Completion

Declare a function template that swaps two values of the same type by reference.

cpp
1
template <typename T>
2
void swapValues( a, b) { std::swap(a, b); }

Click an option to fill blank 1 of 2:

12
Code Completion

Instantiate a Box class template storing std::string.

cpp
1
Box<> messageBox("Hello");

Click an option to fill the blank:

13
Code Completion

Define a variadic template function that forwards all arguments to std::make_shared.

cpp
1
template<typename T, typename... Args>
2
std::shared_ptr<T> makeSharedWrapper(&&... args) { return std::make_shared<T>(std::forward<>(args)...); }

Click an option to fill blank 1 of 2:

14
Output Prediction

What is the output of this code using a simple function template?

1#include <iostream>
2template<typename T>
3T add(T a, T b) { return a + b; }
4int main() {
5    std::cout << add(2, 3) << "\n";
6    std::cout << add(1.5, 2.5) << "\n";
7    return 0;
8}
15
Output Prediction

What does this code print with a class template instantiation?

1#include <iostream>
2
3template<typename T>
4class Box {
5public:
6    Box(T v) : value(v) {}
7    void print() const { std::cout << value << "\n"; }
8private:
9    T value;
10};
11
12int main() {
13    Box<int> bi(42);
14    Box<std::string> bs("templates");
15    bi.print();
16    bs.print();
17}
16
Output Prediction

What is the output of this partial specialization example?

1#include <iostream>
2
3template<typename T>
4struct TypePrinter {
5    static void print() { std::cout << "Generic" << "\n"; }
6};
7
8template<typename T>
9struct TypePrinter<T*> {
10    static void print() { std::cout << "Pointer" << "\n"; }
11};
12
13int main() {
14    TypePrinter<int>::print();
15    TypePrinter<int*>::print();
16}
17
17
Output Prediction

What is the output using a variadic template with fold expression?

1#include <iostream>
2
3template<typename... Args>
4int sum(Args... args) {
5    return (args + ... + 0);
6}
7
8int main() {
9    std::cout << sum(1, 2, 3) << "\n";
10    std::cout << sum(5) << "\n";
11}
12
18
Output Prediction

What is the output of this compile-time factorial template?

1#include <iostream>
2
3template<int N>
4struct Factorial {
5    static const int value = N * Factorial<N - 1>::value;
6};
7
8template<>
9struct Factorial<0> {
10    static const int value = 1;
11};
12
13int main() {
14    std::cout << Factorial<3>::value << "\n";
15    std::cout << Factorial<5>::value << "\n";
16}
17
19
Bug Spotting

Find the bug in this generic max function using templates.

Click on the line(s) that contain the bug.

cpp
1
#include <string>
2
 
3
template<typename T>
4
T myMax(T a, T b) {
5
    return (a > b) ? a : b;
6
}
7
 
8
int main() {
9
    const char* s1 = "apple";
10
    const char* s2 = "banana";
11
    auto m = myMax(s1, s2);
12
}
13
 
20
Bug Spotting

Find the bug in this class template specialization for managing dynamic memory.

Click on the line(s) that contain the bug.

cpp
1
#include <iostream>
2
 
3
template<typename T>
4
class Holder {
5
public:
6
    Holder(T value) : value(value) {}
7
    ~Holder() {}
8
private:
9
    T value;
10
};
11
 
12
template<>
13
class Holder<int*> {
14
public:
15
    Holder(int* p) : ptr(p) {}
16
    ~Holder() {}
17
private:
18
    int* ptr;
19
};
20
 
21
int main() {
22
    Holder<int*> h(new int(5));
23
}
24
 
21
Hotspot Selection

Click the line that relies on template argument deduction for function templates.

Click on the line to select.

cpp
1
#include <vector>
2
 
3
template<typename T>
4
void pushTwice(std::vector<T>& v, const T& value) {
5
    v.push_back(value);
6
    v.push_back(value);
7
}
8
 
9
int main() {
10
    std::vector<int> nums;
11
    pushTwice(nums, 10);
12
}
13
 
22
Hotspot Selection

Click the line where a class template is explicitly specialized.

Click on the line to select.

cpp
1
#include <iostream>
2
 
3
template<typename T>
4
struct Printer {
5
    static void print(const T& value) {
6
        std::cout << value << "\n";
7
    }
8
};
9
 
10
template<>
11
struct Printer<bool> {
12
    static void print(bool value) {
13
        std::cout << (value ? "true" : "false") << "\n";
14
    }
15
};
16
 
17
int main() {
18
    Printer<int>::print(42);
19
    Printer<bool>::print(true);
20
}
21
 
23
Fill in the Blanks

Complete the function template that works with iterators.

cpp
1
template < It>
2
void printRange(It first, It last) {
3
for (; first != last; ++first) {
4
std::cout << << " ";
5
}
6
std::cout << "\n";
7
}

Click an option to fill blank 1:

24
Fill in the Blanks

Complete the variadic template that logs all arguments.

cpp
1
#include <iostream>
2
3
template<typename... Args>
4
void logAll( args) {
5
(std::cout << ... << ) << "\n";
6
}

Click an option to fill blank 1:

25
Matching

Match each template-related concept with its description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Matching

Match each metaprogramming tool with its purpose.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

27
Sequencing

Order the steps to design and use a generic class template that wraps std::vector<T>.

Drag and drop to reorder, or use the arrows.

28
Sequencing

Order the steps to write a compile-time factorial using template metaprogramming.

Drag and drop to reorder, or use the arrows.

Premium Content

Subscribe to unlock full access to this content and more premium articles.