C++ Programming MCQ – Specialization

This section on 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), 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
View Answer

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
View Answer

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
View Answer

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.
advertisement
advertisement

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

  1.     #include <iostream>
  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
View Answer

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.
Output:

$ g++ spec.cpp
$ a.out
2: 4AA:

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

advertisement
  1.     #include <iostream>
  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
View Answer

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.
Output:

advertisement
$ 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 <iostream>
  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
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?

  1.     #include <iostream>
  2.     #include <string>
  3.     #include <cstring>
  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
View Answer

Answer: a
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?

  1.     #include <iostream>
  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
View Answer

Answer: c
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

Answer: b
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

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.