C++ Templates MCQ: C++ Templates Multiple Choice Questions

MCQ on C++ Templates with answers and explanations for placement tests and job interviews. These solved C++ Templates MCQ are useful for the campus placement for all freshers including Engineering Students, MCA students, Computer and IT Engineers, etc.

Our C++ Templates MCQ ( C++ Templates multiple Choice Questions ) focuses on all areas of the C++ Templates and their concept. We will regularly update the quiz and most interesting thing is that questions come in a random sequence. So every time you will feel new questions.

Guideline of C++ Templates MCQ:

This C++ Templates MCQ is intended for checking your C++ Templates knowledge. It takes 40 minutes to pass the C++ Templates MCQ. If you don’t finish the MCQ on C++ Templates within the mentioned time, all the unanswered questions will count as wrong. You can miss the questions by clicking the “Next” button and return to the previous questions by the “Previous” button. Every unanswered question will count as wrong. C++ Templates MCQ has features of randomization which feel you a new question set at every attempt.

In this C++ Templates MCQ, we have also implemented a feature that not allowed the user to see the next question or finish the quiz without attempting the current C++ Templates MCQ.

0 votes, 0 avg

You have 40 minutes to take the C++ Templates MCQs

Your time has been Over.


C++ Templates MCQ

C++ MCQ

C++ Templates MCQ:  C++ Templates Multiple Choice Questions and Answers

1 / 30

Can I separate the definition of my template class from its declaration and put it inside a .cpp file?

2 / 30

What is the result of trying to run this program?

#include <iostream>
template <int T>
struct X
{
    enum val {v = T * X<T-1>::v };
};

template <>
struct X<0>
{
    enum val {v = 1 };
};

int main()
{
    std::cout<<X<5>::v;
}

 

3 / 30

Which of the following is true about templates?

1.  A template is a feature of C++ that allows us to write one code for different data types.

2. We can write one function that can be used for all data types including user-defined types. Like sort(), max(), min(), ..etc.

3. We can write one class or struct that can be used for all data types including user-defined types. Like Linked List, Stack, Queue ..etc.

4. Template is an example of compile-time polymorphism.

4 / 30

Which of the following describes a potentially surprising result of using templates?

5 / 30

Is there any semantic difference between class and typename in a type-parameter-key?

6 / 30

Which of the following provides the best description of an entity type?

7 / 30

In general, is it possible to completely hide the source code of a library written using templates?

8 / 30

What is the output of the below code?

#include <iostream>
using namespace std;

template <typename T>
T max(T x, T y)
{
    return (x > y)? x : y;
}

int main()
{
    cout << max(3, 7) << std::endl;
    cout << max(3.0, 7.0) << std::endl;
    cout << max(3, 7.0) << std::endl;

    return 0;
}

 

9 / 30

Output of the below code?

#include <iostream>
using namespace std;

template <int i>
void fun()
{
    i = 20;
    cout << i;
}

int main()
{
    fun<10>();

    return 0;
}

 

 

10 / 30

When must template functions have explicit template parameters?

11 / 30

The output of the below code?

#include <iostream>
using namespace std;

template<int n> struct funStruct
{
    static const int val = 2*funStruct<n-1>::val;
};

template<> struct funStruct<0>
{
    static const int val = 1 ;
};

int main()
{
    cout << funStruct<10>::val << endl;

    return 0;
}

 

12 / 30

What problems can a templated member function cause?

13 / 30

Output of the below code?

#include <iostream>
using namespace std;

template <class T>
T max (T &a, T &b)
{
    return (a > b)? a : b;
}

template <>
int max <int> (int &a, int &b)
{
    cout << "Called ";

    return (a > b)? a : b;
}

int main ()
{
    int a = 10, b = 20;

    cout << max <int> (a, b);

    return 0;
}

 

 

14 / 30

What is the output of the below code?

#include <iostream>
using namespace std;

template <class T>
class Test
{
private:
    T val;
public:
    static int count;
    Test()
    {
        count++;
    }
};

template<class T>
int Test<T>::count = 0;

int main()
{
    Test<int> a;
    Test<int> b;
    Test<double> c;

    cout << Test<int>::count   << endl;
    cout << Test<double>::count << endl;

    return 0;
}

 

15 / 30

The output of the following program? Assume that the size of char is 1 byte and size of int is 4 bytes, and there is no alignment done by the compiler.

#include<iostream>

using namespace std;

template<class T, class U>
class A
{
    T x;
    U y;
    static int count;
};

int main()
{
    A<char, char> a;
    A<int, int> b;
    cout << sizeof(a) << endl;
    cout << sizeof(b) << endl;

    return 0;
}

 

16 / 30

Select the correct statement.

#include <iostream>

template<class T> class X
{
    static T t;
};


class Y
{
private:
    struct S
    {
    };
    X<S> x;
};


int main()
{

    return 0;
}

 

17 / 30

Can we have overloading of the function templates?

18 / 30

From where does the template class derived?

19 / 30

Which of the following is an invalid template declaration:

20 / 30

Output of the program?

#include <iostream>
using namespace std;

template <class T, int max>
int arrMin(T arr[], int n)
{
    int m = max;
    int i;
    for (i = 0; i < n; i++)
    {
        if (arr[i] < m)
        {
            m = arr[i];
        }
    }
    return m;
}

int main()
{
    int arr1[]  = {10, 20, 15, 12};
    int n1 = sizeof(arr1)/sizeof(arr1[0]);

    char arr2[] = {1, 2, 3};
    int n2 = sizeof(arr2)/sizeof(arr2[0]);

    cout << arrMin<int, 10000>(arr1, n1) << endl;
    cout << arrMin<char, 256>(arr2, n2);

    return 0;
}

 

21 / 30

When are templates usually instantiated?

22 / 30

Output of following program? Assume that the size of int is 4 bytes and size of double is 8 bytes, and there is no alignment done by the compiler.

#include<iostream>

using namespace std;

template<class T, class U, class V=double>
class A
{
    T x;
    U y;
    V z;
    static int count;
};

int main()
{
    A<int, int> a;
    A<double, double> b;

    cout << sizeof(a) << endl;
    cout << sizeof(b) << endl;

    return 0;
}

 

 

23 / 30

What is the output?

#include <iostream>
using namespace std;

template <typename T, int count>
void Test(T x)
{
    T val[count];
    for(int i = 0; i < count; i++)
    {
        val[i] = x++;
        cout <<  val[i] << endl;
    }
};

int main()
{
    float data = 2.1;
    Test<float, 3>(data);
    
    return 0;
}

 

24 / 30

What will be the output of the below code?

#include <iostream>

using namespace std;

template <typename T>
void fun(const T&x)
{
    static int count = 0;
    cout << "x = " << x << " count = " << count << endl;
    ++count;
    return;
}

int main()
{
    fun<int> (1);
    cout << endl;

    fun<int>(1);
    cout << endl;

    fun<double>(1.1);
    cout << endl;

    return 0;
}

 

25 / 30

What is the validity of template parameters?

26 / 30

Which of the following is incorrect in C++ ?

  1. When we write overloaded function we must code the function for each usage.
  2. When we write function template we code the function only once.
  3. It is difficult to debug macros.
  4. Templates are more efficient than macros

27 / 30

Are templates conceptually related to polymorphism?

28 / 30

Which keyword can be used in template?

29 / 30

Which of the things does not require instantiation?

30 / 30

Given the below code, what happens when a method invokes callFunc on an object of type obj?

#include <iostream>

template <class X>
func(X val) 
{

}

template <> 
func<double>(double val) 
{

}

class obj
{
public:
    callFunc()
    {
        func(4.5);
    }
private:
    func(int val) {}
};

 

Your score is

The average score is 0%

0%

Recommended Articles for you: