This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Class Templates”.
1. What is the syntax of class template?
a) template <paramaters> class declaration
b) Template <paramaters> class declaration
c) temp <paramaters> class declaration
d) Temp <paramaters> class declaration
View Answer
Explanation: Syntax involves template keyword followed by list of parameters in angular brackets and then class declaration. As follows template <paramaters> class declaration;
2. What will be the output of the following C++ code?
#include <iostream> #include <string> #include <cstdlib> using namespace std; template<class T> class A { public: A(){ cout<<"Created\n"; } ~A(){ cout<<"Destroyed\n"; } }; int main(int argc, char const *argv[]) { A a; return 0; }
a)
Created Destroyed
b)
Destroyed Created
c) Compile-time error
d) Run-time error
View Answer
Explanation: As class A is a template class and a template class during object declaration requires template arguments, therefore A a; gives an error.
3. What will be the output of the following C++ code?
#include <iostream> #include <string> #include <cstdlib> using namespace std; template<class T> class A { public: A(){ cout<<"Created\n"; } ~A(){ cout<<"Destroyed\n"; } }; int main(int argc, char const *argv[]) { A <int>a; return 0; }
a)
Created Destroyed
b)
Destroyed Created
c) Compile-time error
d) Run-time error
View Answer
Explanation: In this program object of template class has an argument, therefore, the program does not give any error. Hence the program runs perfectly and the output is produced.
Output:
$ ./a.out Created Destroyed
4. What will be the output of the following C++ code?
#include <iostream> #include <string> #include <cstdlib> using namespace std; template<class T> class A { public: A(){ cout<<"Created\n"; } ~A(){ cout<<"Destroyed\n"; } }; int main(int argc, char const *argv[]) { A <int>a1; A <char>a2; A <float>a3; return 0; }
a)
Created Destroyed Created Destroyed Created Destroyed
b)
Created Created Created Destroyed Destroyed Destroyed
c)
Destroyed Created Destroyed Created Destroyed Created
d)
Destroyed Destroyed Destroyed Created Created CreatedView Answer
Explanation: All the objects are first created and then all are destroyed when the scope of all the objects are destroyed i.e. at the end of the main function. That’s why first all the created are printed and then after that, all the destroyed are printed.
5. What will be the output of the following C++ code?
#include <iostream> #include <string> #include <cstdlib> using namespace std; template<class T> class A { public: T func(T a, T b){ return a/b; } }; int main(int argc, char const *argv[]) { A <int>a1; cout<<a1.func(3,2)<<endl; cout<<a1.func(3.0,2.0)<<endl; return 0; }
a)
1 1
b)
1 1.5
c)
1.5 1
d)
1.5 1.5View Answer
Explanation: As a1 object is defined with <int> as the template parameter therefore all the numbers passed to the func() are converted to integers and as integer division of 3 by 2 is 1 therefore the output is 1.
6. What will be the output of the following C++ code?
#include <iostream> #include <string> #include <cstdlib> using namespace std; template<class T> class A { public: T func(T a, T b){ return a/b; } }; int main(int argc, char const *argv[]) { A <float>a1; cout<<a1.func(3,2)<<endl; cout<<a1.func(3.0,2.0)<<endl; return 0; }
a)
1 1
b)
1 1.5
c)
1.5 1
d)
1.5 1.5View Answer
Explanation: As a1 object is defined with <float> as the template parameter therefore all the numbers passed to the func() are converted to decimal floating point numbers and as floating division of 3 by 2 is 1.5 therefore the output is 1.5 for both 1st and 2nd call of the function.
7. What will be the output of the following C++ code?
#include <iostream> #include <string> #include <cstdlib> using namespace std; template<class T> class A { public: T func(T a, T b){ return a/b; } }; int main(int argc, char const *argv[]) { A <char>a1; cout<<a1.func(65,1)<<endl; cout<<a1.func(65.28,1.1)<<endl; return 0; }
a)
A A
b)
65 65
c)
A 65
d)
65 AView Answer
Explanation: As a1 object is defined with
8. What will be the output of the following C++ code?
#include <iostream> #include <string> #include <cstdlib> using namespace std; template<class T> class A { T a; public: A(){} ~A(){} }; int main(int argc, char const *argv[]) { A <char>a1; A <int>a2; A <double>a3; cout<<sizeof(a1)<<endl; cout<<sizeof(a2)<<endl; cout<<sizeof(a3)<<endl; return 0; }
a)
1 4 8
b)
4 1 8
c)
1 1 1
d)
4 4 4View Answer
Explanation: class A has a variable of type template type therefore the type of the class depends on the template type assigned. Therefore for char template the class size is 1. For int template type size is 4 and for double type size is 8.
9. How the template class is different from the normal class?
a) Template class generate objects of classes based on the template type
b) Template class saves system memory
c) Template class helps in making genetic classes
d) All of the mentioned
View Answer
Explanation: Size of the object of template class varies depending on the type of template parameter passed to the class. Due to which each object occupies different memories on system hence saving extra memories. Template class also helps in making generic classes.
10. What will be the output of the following C++ code?
#include <iostream> #include <string> #include <cstdlib> using namespace std; template<class T, class U = char> class A { T a; U b; public: A(T a_val, char b_val = '$'){ this->a = a_val; this->b = b_val; } void print(){ cout<<a<<' '<<b<<endl; } }; int main(int argc, char const *argv[]) { A <int, int> a1(5,10); A <int> a2(5); A <float> a3(10.0); return 0; }
a)
5 10 5 $ 10 $
b) nothing
c) Error
d) Segmentation fault
View Answer
Explanation: The program is correct therefore no error or segmentation fault but as no print() function is called using any object therefore nothing is printed on the output console.
11. What will be the output of the following C++ code?
#include <iostream> #include <string> #include <cstdlib> using namespace std; template<class T, class U = char> class A { T a; U b; public: A(T a_val, char b_val = '$'){ this->a = a_val; this->b = b_val; } void print(){ cout<<a<<' '<<b<<endl; } }; int main(int argc, char const *argv[]) { A <int, int> a1(5,10); A <int> a2(5); A <float> a3(10.0); a1.print(); a2.print(); a3.print(); return 0; }
a)
5 10 5 $ 10 $
b) Nothing
c) Error
d) Segmentation fault
View Answer
Explanation: As we have defined the default templates so when a1 is initilized with 5 and 10 as we have specified <int,int> as the parameters. For a2 5 and $ is initialized as we have only passed <int> as the template parameter. Similar goes for a3. Hence the output is printed as mentioned.
$ ./a.out 5 10 5 $ 10 $
12. How many template parameters are allowed in template classes?
a) 1
b) 2
c) 3
d) one or more
View Answer
Explanation: Just like normal parameters we can pass more than one or more template parameters to a template class.
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.
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Practice Programming MCQs
- Check Computer Science Books
- Apply for C++ Internship