Templates


  1. Which of the things does not require instantiation?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    The compiler does not generate definitions for functions, non virtual member functions, class or member class because it does not require instantiation.


  1. What is a template?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    A template is a formula for creating a generic class



  1. What is the output of this program?
    #include <iostream>
    #include <string>
    using namespace std;
    template<typename T>
    void Print_Data(T output)
    {
    cout << output << endl;
    }
    int main()
    {
    double var = 6.25;
    string str("Hello Interview Mania");
    Print_Data( var );
    Print_Data( str );
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    In this program, We are passing the value to the template and printing it in the template.


  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    template <typename T, typename U>
    void squareAndPrint(T p, U q)
    {
    cout << p << p * p << endl;
    cout << q << " " << q * q << endl;
    };
    int main()
    {
    int kk = 3;
    float LL = 3.12;
    squareAndPrint<int, float>(kk, LL);
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    In this multiple templated types, We are passing two values of different types and producing the result.



  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    template <class T>
    inline T square(T p)
    {
    T result;
    result = p * p;
    return result;
    };
    template <>
    string square<string>(string str)
    {
    return (str+str);
    };
    int main()
    {
    int k = 4, kk;
    string bb("A");
    kk = square<int>(k);
    cout << k << kk;
    cout << square<string>(bb) << endl;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    In this program, We are using two template to calculate the square and to find the addition.