1. What is meant by template specialization?
A. It will have certain data types to be fixed
B. It will make certain data types to be dynamic
C. Certain data types are invalid
D. It will make all data types to be dynamic
Answer: A
Explanation:
In the template specialization, it will make the template to be specific for some data types.
2. Which is similar to template specialization?
A. template
B. function overloading
C. function template overloading
D. overloading
Answer: C
Explanation:
function template overloading is similar to template specialization.
3. Which is called on allocating the memory for the array of objects?
A. destructor
B. constructor
C. method
D. class
Answer: B
Explanation:
When you allocate memory for an array of objects, the default constructor must be called to construct each object. If no default constructor exists, you’re stuck needing a list of pointers to objects.
4. What will be the output of the following C++ code?
#include
using namespace std;
template
inline T square(T x)
{
T result;
result = x * x;
return result;
};
template <>
string square(string ss)
{
return (ss+ss);
};
int main()
{
int i = 2, ii;
string ww("A");
ii = square(i);
cout << i << ": " << ii;
cout << square(ww) << ":" << endl;
}
A. 2: 4AA:
B. 2:4
C. AA
D. 2:4A
Answer: A
Explanation:
Template specialization is used when a different and specific implementation is to be used for a specific data type. In this program, We are using integer and character.
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
template <typename T = float, int count = 3>
T multIt(T x)
{
for(int ii = 0; ii < count; ii++)
{
x = x * x;
}
return x;
};
int main()
{
float xx = 2.1;
cout << xx << ": " << multIt<>(xx) << endl;
}
Answer: C
Explanation: AWT stands for Abstract Window Toolkit, it is used by applets to interact with the user.
A. 2.1
B. 378.228
C. 2.1: 378.228
D. 376
Answer: C
Explanation:
In this program, We specify the type in the template function. We need to compile this program by adding -std=c++0x.
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
template <class T>
class XYZ
{
public:
void putPri();
static T ipub;
private:
static T ipri;
};
template <class T>
void XYZ<T>::putPri()
{
cout << ipri++ << endl;
}
template <class T> T XYZ<T>::ipub = 1;
template <class T> T XYZ<T>::ipri = 1.2;
int main()
{
XYZ<int> a;
XYZ<float> b;
a.putPri();
cout << a.ipub << endl;
b.putPri();
}
A. 1
B. 1.2
C.1
1.2
D.1
1
1.2
Answer: D
Explanation:
In this program, We are passing the value of specified type and printing it by specialization.
7. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
template <class type>
type MyMax(const type Var1, const type Var2)
{
cout << "no specialization";
return Var1 < Var2 ? Var2 : Var1;
}
template <>
const char *MyMax(const char *Var1, const char *Var2)
{
return (strcmp(Var1, Var2)<0) ? Var2 : Var1;
}
int main()
{
string Str1 = "class", Str2 = "template";
const char *Var3 = "class";
const char *Var4 = "template";
const char *q = MyMax(Var3, Var4);
cout << q << endl;
return 0;
}
A. template
B. class
C. no specialization
D. templateclass
Answer: C
Explanation:
In this program, We are using the non-type template parameter to increment the value in the function template.
8. How many types of specialization are there in c++?
A. 1
B. 2
C. 3
D. 4
Answer: B
Explanation:
There are two types of specialization. They are full specialization and partial specialization.
9. What is another name of full specialization?
A. explicit specialization
B. implicit specialization
C. function overloading template
D. overloading template
Answer: A
Explanation:
explicit specialization is another name of full specialization.
- 10+ TOP C++ MCQs on OOPs Concept – 2
- 10+ TOP C++ MCQs on OOPs Concept – 3
- 10+ TOP C++ MCQs on Integer Types
- 10+ TOP C++ MCQs on Floating Point Types
- 10+ TOP C++ MCQs on References – 2
- 10+ TOP C++ MCQs on References – 3
- 10+ TOP C++ MCQs on Objects
- 10+ TOP C++ MCQs on Operator Functions