LearnFrenzy
Sign in Take a free test

Templates · Exercise 3 of 4

Templates Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each C++ program contains a template mistake that the compiler rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

template <typename T>
T smaller(T a, T b) { return a < b ? a : b; }

int main() {
    std::cout << smaller(10, 7.5) << '\n';
    return 0;
}
A < cannot compare numbers
B T is deduced as int from 10 and double from 7.5; the conflict makes deduction fail – use smaller<double>(10, 7.5) or two type parameters
C smaller must be inline
D Templates cannot return T
Question page & discussion
Question 2 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <string>

struct Student { std::string name; int marks; };

template <typename T>
T best(T a, T b) { return a > b ? a : b; }

int main() {
    Student x{"Asha", 91}, y{"Ravi", 78};
    std::cout << best(x, y).name << '\n';
    return 0;
}
A Student needs a constructor
B best uses a > b, but Student has no operator>; define one (or <=>) or pass a comparator
C Templates cannot take structs
D name must be public
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <array>
#include <iostream>

int main() {
    int n = 5;
    std::array<int, n> a{};
    std::cout << a.size() << '\n';
    return 0;
}
A std::array needs a type only
B n is a run-time variable; non-type template arguments must be compile-time constants – declare it constexpr int n = 5; or use std::vector
C a{} is not allowed
D size() is not a member
Question page & discussion
Question 4 Hard

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <vector>

template <typename C>
void print_first(const C& c) {
    C::const_iterator it = c.begin();
    std::cout << *it << '\n';
}

int main() {
    print_first(std::vector<int>{4, 5});
    return 0;
}
A const_iterator does not exist
B C::const_iterator depends on C, so the compiler needs typename to know it is a type: typename C::const_iterator it = …; (or just auto)
C c must not be const
D Vectors cannot be passed by reference
Question page & discussion
Question 5 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <concepts>
#include <iostream>

template <std::floating_point T>
T average(T a, T b) { return (a + b) / 2; }

int main() {
    std::cout << average(2.5, 3.5) << '\n';
    std::cout << average(2, 3) << '\n';
    return 0;
}
A average must return double
B int does not satisfy std::floating_point, so average(2, 3) is rejected; call average(2.0, 3.0) or loosen the constraint
C Concepts need C++23
D The division is wrong
Question page & discussion
Question 6 Hard

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

template <typename T, typename U>
void show(T, U) { std::cout << "general\n"; }

template <typename T>
void show<T, int>(T, int) { std::cout << "second is int\n"; }

int main() {
    show(1.5, 2);
    return 0;
}
A show cannot take two parameters
B Function templates cannot be partially specialised; write an overload template <typename T> void show(T, int) instead
C int is not a valid template argument
D main must be a template
Question page & discussion
Question 7 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

template <typename T>
struct Holder {
    T value{};
};

int main() {
    Holder h;
    std::cout << h.value << '\n';
    return 0;
}
A value{} is invalid
B Holder needs a template argument: with no constructor argument there is nothing to deduce T from, so write Holder<int> h;
C Holder must have a constructor
D structs cannot be templates
Question page & discussion
Question 8 Hard

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

template <typename... Ts>
auto product(Ts... xs) { return (xs * ...); }

int main() {
    std::cout << product(2, 3, 4) << '\n';
    std::cout << product() << '\n';
    return 0;
}
A * cannot be folded
B A unary fold over an empty pack is ill-formed for * (only &&, || and , have empty values); use a binary fold (xs * ... * 1)
C product must take at least one int
D auto return type is not allowed
Question page & discussion
Question 9 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

template <typename T>
struct Repository {
    template <typename Key>
    virtual T find(const Key& k) const { return T{}; }
    virtual ~Repository() = default;
};

int main() {
    Repository<int> r;
    std::cout << r.find(5) << '\n';
    return 0;
}
A T{} is invalid
B Member function templates cannot be virtual; make find non-virtual, or give it a fixed key type so it can be an ordinary virtual function
C Repository must be abstract
D Key must be a class
Question page & discussion
Question 10 Hard

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

template <typename T>
struct Label { static const char* get() { return "value"; } };

void show() { std::cout << Label<int>::get() << '\n'; }   // Label<int> instantiated here

template <>
struct Label<int> { static const char* get() { return "integer"; } };

int main() {
    show();
    return 0;
}
A get must not be static
B Label<int> was already implicitly instantiated in show(), so an explicit specialisation declared afterwards is an error; declare the specialisation before any use
C Specialisations must be in main
D int cannot be specialised
Question page & discussion
Up next · Exercise 4Template Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links