C++ Template Aptitude Questions and Answers

C++ Template Aptitude: This section contains C++ Template Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 20, 2021

1) There are the following statements that are given below, which of them are correct about templates in C++?

  1. The template is a new feature in C++; templates do not exist in C language.
  2. The template is used for generic programming.
  3. The template is a special class which used to store data elements.
  4. All the above

Options:

  1. A and B
  2. A and C
  3. B and C
  4. D

2) We can implement templates as a?

  1. Class templates
  2. Function templates
  3. Enumeration templates
  4. Pointer templates

Options:

  1. A and B
  2. A and C
  3. B, C, and D
  4. A, B, C, and D

3) There are following options are given below, which of them is correct syntax of function templates in C++?

  1. templates <class type> return type function_name(parameter_list) { }
  2. Template <class type> return type function_name(parameter_list) { }
  3. Templates <class type> return type function_name(parameter_list) { }
  4. template <class type> return type function_name(parameter_list) { }

4) What is the correct output of given code snippets in C++?

#include <iostream>
using namespace std;

template <class T>
T sum(T num1, T num2)
{
    T C = num1 + num2;

    return C;
}

int main()
{
    int A = 5;
    int B = 10;

    double X = 3.5;
    double Y = 4.6;

    cout << sum(A, B) << " " << sum(X, Y) << endl;

    return 0;
}

Options:

  1. 15 8.1
  2. 15 8
  3. Garbage values
  4. Syntax error

5) What is the correct output of given code snippets in C++?

#include <iostream>
using namespace std;

template <class T>
int sum(T num1, T num2)
{
    T C = num1 + num2;

    return C;
}

int main()
{
    int A = 5;
    int B = 10;

    double X = 3.5;
    double Y = 4.6;

    cout << sum(A, B) << " " << sum(X, Y) << endl;

    return 0;
}

Options:

  1. 15 8.1
  2. 15 8
  3. Garbage values
  4. Syntax error

6) What is the correct output of given code snippets in C++?

#include <iostream>
using namespace std;

template <class T1>
int sum(T1 num1, T1 num2)
{
    T1 C = num1 + num2;

    return C;
}

int main()
{
    int A = 5;
    int B = 10;

    double X = 3.5;
    double Y = 4.6;

    cout << sum(A, B) << " " << sum(X, Y) << endl;

    return 0;
}

Options:

  1. 15 8.1
  2. 15 8
  3. Garbage values
  4. Syntax error

7) What is the correct output of given code snippets in C++?

#include <iostream>
using namespace std;

<class T> template int sum(T num1, T num2)
{
    T C = num1 + num2;

    return C;
}

int main()
{
    int A = 5;
    int B = 10;

    double X = 3.5;
    double Y = 4.6;

    cout << sum(A, B) << " " << sum(X, Y) << endl;

    return 0;
}

Options:

  1. 15 8.1
  2. 15 8
  3. Garbage values
  4. Syntax error

8) What is the correct output of given code snippets in C++?

#include <iostream>
using namespace std;

template <class X, class Y>
Y sum(X num1, Y num2)
{
    Y C = num1 + num2;

    return C;
}

int main()
{
    int A = 5;
    int B = 10;

    double X = 3.5;
    double Y = 4.6;

    cout << sum(A, Y) << endl;

    return 0;
}

Options:

  1. 9
  2. 9.6
  3. Garbage values
  4. Syntax error

9) Can we overload template functions in C++?

  1. Yes
  2. No

10) What is the correct output of given code snippets in C++?

#include <iostream>
using namespace std;

class template <class T>
Sample
{
    T a;
    T b;

public:
    Sample(T a, T b)
    {
        this->a = a;
        this->b = b;
    }
    void print()
    {
        cout << a << " " << b << endl;
    }
};

int main()
{
    Sample<int> S(10, 20);

    S.print();

    return 0;
}

Options:

  1. 10 20
  2. Garbage values
  3. Syntax error
  4. Runtime exception

11) What is the correct output of given code snippets in C++?

#include <iostream>
using namespace std;

template <class T>
class Sample {
    T a;
    T b;

public:
    Sample(T a, T b)
    {
        this->a = a;
        this->b = b;
    }
    void print()
    {
        cout << a << " " << b << endl;
    }
};

int main()
{
    Sample<int> S(10, 20);

    S.print();

    return 0;
}

Options:

  1. 10 20
  2. Garbage values
  3. Syntax error
  4. Runtime exception

12) What is the correct output of given code snippets in C++?

#include <iostream>
using namespace std;

template <class T, int b>
class Sample {
    T a;

public:
    Sample(T a)
    {
        this->a = a;
    }
    void print()
    {
        cout << a << " " << b << endl;
    }
};

int main()
{
    Sample<int, 20> S(10);

    S.print();

    return 0;
}

Options:

  1. 10 20
  2. Garbage values
  3. Syntax error
  4. Runtime exception





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.