github

Made bySaurav Hathi

MCQ Test C/C++: Templates

Q1:

Which of the following is not correct (in C++) ?

1. Class templates and function templates are instantiated in the same way

2. Class templates differ from function templates in the way they are initiated

3. Class template is initiated by defining an object using the template argument

4. Class templates are generally used for storage classes

Tags:
Section:
Core Programming
Options:
Q2:

What is the output of this C++ program?

#include <iostream>

using namespace std;

template <class T>

class A

{

public:

A(int a): x(a) {}

protected:

int x;

};

template <class T>

class B: public A<char>

{

public:

B(): A<char>::A(100)

{

cout << x * 2 << endl;

}

};

int main()

{

B<char> test;

return 0;

}

Tags:
Section:
Core Programming
Options:
Q3:

What is meant by template parameter in C++?

Tags:
Section:
Core Programming
Options:
Q4:

What will be the output of the given C++ 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;

}

Tags:
Section:
Core Programming
Options:
Q5:

Which of the following is true about templates in C++?
1) 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.

Tags:
Section:
Core Programming
Options:
Q6:

What will be the output of this C++ 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;

}

Tags:
Section:
Core Programming
Options:
Q7:

What is the output of this C++ program?

#include <iostream>

using namespace std;

template <class type>

class Test

{

public:

Test(){};

~Test(){};

type Funct1(type Var1)

{

return Var1;

}

type Funct2(type Var2)

{

return Var2;

}

};

int main()

{

Test<int> Var1;

Test<double> Var2;

cout << Var1.Funct1(200);

cout << Var2.Funct2(3.123);

return 0;

}

Tags:
Section:
Core Programming
Options:
Q8:

How to declare a template in C++?

Tags:
Section:
Core Programming
Options:
Q9:

What is the output of this C++ program?

#include <iostream>

#include <string>

using namespace std;

template < typename T >

void print_mydata(T output)

{

cout << output << endl;

}

int main()

{

double d = 5.5;

string s("Hello World");

print_mydata( d );

print_mydata( s );

return 0;

}

Tags:
Section:
Core Programming
Options:
Q10:

What is the validity of template parameters in C++?

Tags:
Section:
Core Programming
Options: