C++ Programming MCQ – Specialization

C++ interview questions and answers focuses on “Specialization “. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews) entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ interview questions on “Specialization ” along with answers, explanations and/or solutions:


Q 1. What is meant by template specialization?
A. Certain data types are invalid
B. It will make certain data types to be dynamic
C. It will have certain data types to be fixed
D. It will make all data types to be dynamic

Show Answer Answer:-C. It will have certain data types to be fixed
Explanation In the template specialization, it will make the template to be specific for some data types.


Q 2. Which is similar to template specialization?
A. function template overloading
B. function overloading
C. template
D. overloading

Show Answer Answer:-A. function template overloading
Explanation function template overloading is similar to template specialization.


Q 3. Which is called on allocating the memory for the array of objects?
A. constructor
B. destructor
C. method
D. class

Show Answer Answer:-A. constructor
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.

Q 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

Show Answer Answer:-A. 2: 4AA:
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:


Q 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. 376
D. 2.1: 378.228

Show Answer Answer:-D. 2.1: 378.228
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


Q 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

Show Answer Answer:-D. 1 1 1.2
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



Q 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. class
B. template
C. no specialization
D. templateclass

Show Answer Answer:-B. template
Explanation In this program, We are computing the result in the specialized block of the program. Output: $ g++ spec3.cpp $ a.out template


Q 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

Show Answer Answer:-C. 6default10
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


Q 9. How many types of specialization are there in c++?
A. 1
B. 2
C. 3
D. 4

Show Answer Answer:-D. 4
Explanation There are two types of specialization. They are full specialization and partial specialization.


Q 10. What is another name of full specialization?
A. explicit specialization
B. implicit specialization
C. function overloading template
D. overloading template

Show Answer Answer:-A. explicit specialization
Explanation explicit specialization is another name of full specialization.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page