My Report

C++ Template Test – 2


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. What is the syntax of class template?

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:
	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;
}

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
{
	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;
}

4. How many template parameters are allowed in template classes?

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:
	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;
}

6. 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;
}

7. How the template class is different from the normal class?

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
{
    public:
	A(){
		cout<<"Created\n";
	}
	~A(){
		cout<<"Destroyed\n";
	}
};

int main(int argc, char const *argv[])
{
	A <int>a;
	return 0;
}

9. 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;
}

10. 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;
}

11. 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;
}

12. 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;
}

 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

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.