Here is a listing of C++ interview questions on “Specialization ” along with answers, explanations and/or solutions:
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
View Answer
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
View Answer
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
View Answer
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 <iostream>
using namespace std;
template <class T>
inline T square(T x)
{
T result;
result = x * x;
return result;
};
template <>
string square<string>(string ss)
{
return (ss+ss);
};
int main()
{
int i = 2, ii;
string ww("A");
ii = square<int>(i);
cout << i << ": " << ii;
cout << square<string>(ww) << ":" << endl;
}
a) 2: 4AA:
b) 2:4
c) AA
d) 2:4A
View Answer
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.
Output:
$ g++ spec.cpp $ a.out 2: 4AA:
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;
}
a) 2.1
b) 378.228
c) 2.1: 378.228
d) 376
View Answer
Explanation: In this program, We specify the type in the template function. We need to compile this program by adding -std=c++0x.
Output:
$ g++ -std=c++0x spec1.cpp $ a.out 2.1: 378.228
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.2View Answer
Explanation: In this program, We are passing the value of specified type and printing it by specialization.
Output:
$ g++ spec2.cpp $ a.out 1 1 1.2
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
View Answer
Explanation: In this program, We are computing the result in the specialized block of the program.
Output:
$ g++ spec3.cpp $ a.out template
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
template<class T = float, int i = 5> class A
{
public:
A();
int value;
};
template<> class A<>
{
public: A();
};
template<> class A<double, 10>
{
public: A();
};
template<class T, int i> A<T, i>::A() : value(i)
{
cout << value;
}
A<>::A()
{
cout << "default";
}
A<double, 10>::A()
{
cout << "10" << endl;
}
int main()
{
A<int, 6> x;
A<> y;
A<double, 10> z;
}
a) 6
b) 10
c) 6default10
d) 6default
View Answer
Explanation: In this program, We are defining three templates and specializing it and passing the values to it and printing it.
Output:
$ g++ spec5.cpp $ a.out 6default10
9. How many types of specialization are there in c++?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: There are two types of specialization. They are full specialization and partial specialization.
10. What is another name of full specialization?
a) explicit specialization
b) implicit specialization
c) function overloading template
d) overloading template
View Answer
Explanation: explicit specialization is another name of full specialization.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check C++ Books
- Apply for C++ Internship
- Check Programming Books
- Check Computer Science Books
- Practice Computer Science MCQs