250+ TOP MCQs on Specialization and Answers

This section on C++ interview questions and answers 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), placements, 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.

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
Answer: a
Clarification: 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
Clarification: 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
Clarification: 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.

  250+ TOP MCQs on Exception Handling – 3 and Answers

4. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     template <class T>
  4.     inline T square(T x)
  5.     {
  6.         T result;
  7.         result = x * x;
  8.         return result;
  9.     };
  10.     template <>
  11.     string square<string>(string ss)
  12.     {
  13.         return (ss+ss);
  14.     };
  15.     int main()
  16.     {
  17.         int i = 2, ii;
  18.         string ww("A");
  19.         ii = square<int>(i);
  20.         cout << i << ": " << ii;
  21.         cout << square<string>(ww) << ":" << endl;
  22.     }

a) 2: 4AA:
b) 2:4
c) AA
d) 2:4A
Answer: a
Clarification: 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?

  1.     #include 
  2.     using namespace std;
  3.     template <typename T = float, int count = 3>
  4.     T multIt(T x)
  5.     {
  6.         for(int ii = 0; ii < count; ii++)
  7.         {
  8.             x = x * x;
  9.         }
  10.         return x;
  11.     };
  12.     int main()
  13.     {
  14.         float xx = 2.1;
  15.         cout << xx << ": " << multIt<>(xx) << endl;
  16.     }

a) 2.1
b) 378.228
c) 2.1: 378.228
d) 376
Answer: c
Clarification: 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?

  1.     #include 
  2.     using namespace std;
  3.     template <class T>
  4.     class XYZ
  5.     {
  6.         public:
  7.         void putPri();
  8.         static T ipub;
  9.         private:
  10.         static T ipri; 
  11.     };
  12.     template <class T>
  13.     void XYZ<T>::putPri()
  14.     {
  15.         cout << ipri++ << endl;
  16.     }
  17.     template <class T> T XYZ<T>::ipub = 1;
  18.     template <class T> T XYZ<T>::ipri = 1.2;
  19.     int main()
  20.     {
  21.         XYZ<int> a;
  22.         XYZ<float> b;
  23.         a.putPri();
  24.         cout << a.ipub << endl;
  25.         b.putPri();
  26.     }

a) 1
b) 1.2
c)

   1
   1.2

d)

   1
   1
   1.2

View Answer

Answer: d
Clarification: 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?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     template <class type>
  6.     type MyMax(const type Var1, const type Var2)
  7.     {
  8.         cout << "no specialization";
  9.         return Var1 < Var2 ? Var2 : Var1;
  10.     }
  11.     template <>
  12.     const char *MyMax(const char *Var1, const char *Var2)
  13.     {
  14.         return (strcmp(Var1, Var2)<0) ? Var2 : Var1;
  15.     }
  16.     int main()
  17.     {
  18.         string Str1 = "class", Str2 = "template";
  19.         const char *Var3 = "class";
  20.         const char *Var4 = "template";
  21.         const char *q = MyMax(Var3, Var4);
  22.         cout << q << endl;
  23.         return 0;
  24.     }

a) template
b) class
c) no specialization
d) templateclass
Answer: a
Clarification: 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?

  1.     #include 
  2.     using namespace std;
  3.     template<class T = float, int i = 5> class A
  4.     {
  5.         public:
  6.         A();
  7.         int value;
  8.     };
  9.     template<> class A<> 
  10.     { 
  11.         public: A(); 
  12.     };
  13.     template<> class A<double, 10>
  14.     { 
  15.         public: A(); 
  16.     };
  17.     template<class T, int i> A<T, i>::A() : value(i)
  18.     {
  19.         cout << value;
  20.     }
  21.     A<>::A() 
  22.     {
  23.         cout << "default";
  24.     }
  25.     A<double, 10>::A() 
  26.     {
  27.         cout << "10" << endl;
  28.     }
  29.     int main() 
  30.     {
  31.         A<int, 6> x;
  32.         A<> y;
  33.         A<double, 10> z;
  34.     }

a) 6
b) 10
c) 6default10
d) 6default
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: explicit specialization is another name of full specialization.

Leave a Comment

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

Scroll to Top