C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Class Templates”.
Q 1. What is the syntax of class template?
A. temp <paramaters> class declaration
B. Template <paramaters> class declaration
C. template <paramaters> class declaration
D. Temp <paramaters> class declaration
Show Answer
Answer:-C. templateExplanation
Syntax involves template keyword followed by list of parameters in angular brackets and then class declaration. As follows template
Q 2. How many template parameters are allowed in template classes?
A. 1
B. 2
C. 3
D. one or more
Show Answer
Answer:-D. one or moreExplanation
Just like normal parameters we can pass more than one or more template parameters to a template class.Q 3. 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
Show Answer
Answer:-D. All of the mentionedExplanation
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.
Q 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 a;
return 0;
}
A. Created
Destroyed
B. Destroyed
Created
C. Compile-time error
D. Run-time error
Show Answer
Answer:-C. Compile-time errorExplanation
As class A is a template class and a template class during object declaration requires template arguments, therefore A a; gives an error.
Q 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>a;
return 0;
}
A. Created
Destroyed
B. Destroyed
Created
C. Compile-time error
D. Run-time error
Show Answer
Answer:-A. Created DestroyedExplanation
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
Q 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:
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
Show Answer
Answer:-B. Created Created Created Destroyed Destroyed DestroyedExplanation
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.
Q 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 <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
Show Answer
Answer:-A. 1 1Explanation
As a1 object is defined with
Q 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:
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
Show Answer
Answer:-D. 1.5 1.5Explanation
As a1 object is defined withQ 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
{
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
Show Answer
Answer:-A. 1 4 8Explanation
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.
Q 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
Show Answer
Answer:-B. nothingExplanation
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.
Q 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
Leave a Reply