C++ Programming Questions and Answers – Class Templates

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

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

advertisement
advertisement
Destroyed
Created

c) Compile-time error
d) Run-time error
View Answer

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

Note: Join free Sanfoundry classes at Telegram or Youtube
Created
Destroyed

b)

advertisement
Destroyed
Created

c) Compile-time error
d) Run-time error
View Answer

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

advertisement
#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
Created
View Answer
Answer: b
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.5
View Answer
Answer: a
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.5
View Answer
Answer: d
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
A
View Answer
Answer: a
Explanation: As a1 object is defined with as the template parameter therefore all the numbers passed to the func() are converted to characters integers. So when 1st func() call is done with 65 and 1 then after dividing 65 by 1 gives 65 for which char equivalent is ‘A’ hence ‘A’ is printed similarly in 2nd case 65.28/1.1 = 65 integer which represents ‘A’ in char type therefore ‘A’ is printed.
 
 

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
4
View Answer
Answer: a
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

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

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

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

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

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.