Templates

Last Updated :
Discuss
Comments

Question 1

What is the main purpose of function templates in C++?

  • A

    To make functions execute faster

  • B

    To store multiple function definitions in one place

  • C

    To create generic functions that work with multiple data types

  • D

    To restrict a function to only one data type

Question 2

Which keyword is used to define a template in C++?

  • A

    template

  • B

    typename only

  • C

    generic

  • D

    template_function

Question 3

Consider the following code:

C++
#include 
using namespace std;

template <typename T>
T add(T a, T b) {
    return a + b;
}

int main() {
    cout << add(5, 10);
}

What will be the output?

  • A

    Compilation Error

  • B

    5 10

  • C

    15

  • D

    Undefined Behaviour

Question 4

Which is the correct syntax for template specialization for int type?

  • A
    C++
    template <> int func<int>(int a) { ... }
    


  • B
    C++
    template <> int func<int>(int a) { ... }
    


  • C
    C++
    template <> int func(int a) { ... }
    


  • D
    C++
    specialize template int func(int a) { ... }
    


Question 5

Which of the following is TRUE about variadic function templates?

  • A

    They can accept only integers


  • B

    They are available since C++98

  • C

    They can accept a variable number of template arguments

  • D

    They require all arguments to be the same type

Question 6

What is the base case in a variadic function template recursion?

  • A

    When the compiler finds no template arguments left

  • B

    When there are two arguments left

  • C

    When all arguments are of the same type

  • D

     When the last argument is nullptr

Tags:

There are 6 questions to complete.

Take a part in the ongoing discussion