My Report

C++ Template Test – 1


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. What will be the output of the following C++ code?

  
    #include <iostream>
    using namespace std;
    template<typename type>
    type Max(type Var1, type Var2)
    {
        return Var1 > Var2 ? Var1:Var2;
    }
    int main()
    {
        int p;
        p = Max(100, 200);
        cout << p << endl;
        return 0;
    } 

2. What will be the output of the following C++ code?

  
    #include <iostream>
    using namespace std;
    template<typename type>
    class TestVirt
    {
        public:
        virtual type TestFunct(type Var1)
        {
            return Var1 * 2;
        }
    };
    int main()
    {
        TestVirt<int> Var1;
        cout << Var1.TestFunct(100) << endl;
        return 0;
    }

3. Which is used to describe the function using placeholder types?

4. What can be passed by non-type template parameters during compile time?

5. What is a function template?

6. What will be the output of the following C++ code?

  
    #include <iostream>
    using namespace std;
    template<typename T> 
    inline T square(T x)
    {
        T result;
        result = x * x;
        return result;
    };
    int main()
    {
        int i, ii;
        float x, xx;
        double y, yy;
        i = 2;
        x = 2.2;
        y = 2.2;
        ii = square(i);
        cout << i << " "  << ii << endl;
        yy = square(y);
        cout << y << " " << yy << endl;
    }

7. From where does the template class derived?

8. Pick out the correct statement.

9. What will be the output of the following C++ code?

  
    #include <iostream>
    using namespace std;
    template<typename T> 
    void loopIt(T x)
    {
        int count = 3;
        T val[count];
        for (int ii=0; ii < count; ii++)
        {
            val[ii] = x++;
            cout <<  val[ii] << endl;
        }
    };
    int main()
    {
        float xx = 2.1;
        loopIt(xx);
    }

10. What will be the output of the following C++ code?

  
    #include <iostream>
    using namespace std;
    template<typename type> 
    class Test
    {
        public:
        Test()
        {
        };
        ~Test()
        {
        };
        type Funct1(type Var1)
        {
            return Var1;
        }
        type Funct2(type Var2)
        {
            return Var2;
        }
    };
    int main()
    {
        Test<int> Var1;
        Test<float> Var2;
        cout << Var1.Funct1(200) << endl;
        cout << Var2.Funct2(3.123) << endl;
        return 0;
    }

 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.