C++ Programming Multiple Choice Questions - Templates
This section focuses on the "Templates" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which value is placed in the base class?
A. Derived values
B. Default type values
C. Both default type & derived values
D. None of the above
View Answer
Ans : B
Explanation: We can place the default type values in a base class and overriding some of them through derivation.
2. What is a template?
A. A template is a formula for creating a generic class
B. A template is used to manipulate the class
C. A template is used for creating the attributes
D. None of the above
View Answer
Ans : A
Explanation: A template is a formula for creating a generic class
3. Which of the following best defines the syntax for template function ?
A. template return_type Function_Name(Parameters)
B. template return_type Function_Name(Parameters)
C. Both A and B
D. None of the above
View Answer
Ans : C
Explanation: Both A or B is the syntax for template function.
4. Templates are abstract recipe for producing a concrete code, and it is used for
A. Producing functions
B. Producing classes
C. Nothing
D. Both A and B
View Answer
Ans : D
Explanation: Templates are abstract recipe for producing a concrete code, and it is used for Both A and B options.
5. How many parameters are legal for non-type template?
A. 1
B. 2
C. 3
D. 4
View Answer
Ans : D
Explanation: The following are legal for non-type template parameters: integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.
6. How many kinds of entities are directly parameterized in c++?
A. 1
B. 2
C. 3
D. 4
View Answer
Ans : C
Explanation: C++ allows us to parameterize directly three kinds of entities through templates: types, constants, and templates
7. From where does the template class derived?
A. Regular non-templated C++ class
B. Templated class
C. Both A or B
D. None of the above
View Answer
Ans : C
Explanation: The template class derived Both A or B
8. Can we have overloading of the function templates?
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: Yes, we have overloading of the function templates.
9. A container class is a class whose instances are
A. Containers
B. Functions
C. Strings
D. None of the above
View Answer
Ans : A
Explanation: A container class is a class whose instances are Containers.
10. Which of the things does not require instantiation?
A. Functions
B. Non virtual member function
C. Member class
D. All of the above
View Answer
Ans : D
Explanation: All of the above things does not require instantiation.
11. What is the output of this program?
#include <iostream>
using namespace std;
template <typename T>
T max (T& p, T& q)
{
return (p>q?p:q);
}
int main ()
{
int x = 55, y = 60, m;
long a = 105, b = 53, n;
m = max(x, y);
n = max(a, b);
cout << m << endl;
cout << n << endl;
return 0;
}
A. 60
B. 60 105
C. 50 105
D. 60 53
View Answer
Ans : B
Explanation: In this program, We are using the ternary operator on the template function.
12. What is the output of this program?
#include <iostream>
using namespace std;
template <typename T>
void loopIt(T x)
{ int num=3;
T lfc[num];
for(int p = 0; p < num; p++)
{
lfc[p] = x++;
cout << lfc[p] << endl;
}
};
int main()
{
float q = 2.1;
loopIt(q);
}
A. 2.1
B. 3.1
C. 4.1
D. 2.1 3.1 4.1
View Answer
Ans : D
Explanation: In this program, We are using the non-type template parameter to increment the value in the function template.
13. What is the output of this program?
#include <iostream>
using namespace std;
template <typename T>
T maximum(T x, T y)
{
return (x > y)? x : y;
}
int main()
{
cout << maximum(3, 7) << std::endl;
cout << maximum(3.0, 7.0) << std::endl;
cout << maximum(3, 7.0) << std::endl;
return 0;
}
A. Compiler Error in last cout statement as call to maximum is ambiguous
B. Compiler Error in all cout statements as data type is not specified
C. 7 7.0 7.0
D. None of the above
View Answer
Ans : A
Explanation: This fails in compilation because there is no template definition matching the third cout statement.
for third statememnt to work, you will have to define another function template that takes another typename (for accepting two different types at a time).
14. Which of the following statement is correct about the program given below?
#include <iostream>
using namespace std;
template <class T, class U>
class A {
T x;
U y;
static int count;
};
int main() {
A<char, char> p;
A<int, int> q;
cout << sizeof(p) << endl;
cout << sizeof(q) << endl;
return 0;
}
A. Compiler Error: There can not be more than one template arguments.
B. 6 12
C. 8 8
D. 2 8
View Answer
Ans : D
Explanation: Since count is static, it is not counted in sizeof.
15. Which of the following is true about the following program
#include <iostream>
using namespace std;
template <class P, class Q, class R>
class A {
P x;
Q y;
R z;
static int count;
};
int main()
{
A<int, int, int> m;
A<char, char, char> n;
cout << sizeof(m) << endl;
cout << sizeof(n) << endl;
return 0;
}
A. Compiler Error: template parameters cannot have default values
B. 12 6
C. 12 3
D. 6 3
View Answer
Ans : B
Explanation: templates can also have default parameters. The rule is same all default values must be on the rightmost side. Since count is static, it is not counted in sizeof.
16. What will be the output of this program?
#include <iostream>
using namespace std;
template <int i>
void fun()
{
i = 20;
cout << i;
}
int main()
{
fun<10>();
return 0;
}
A. Compile Error
B. 10
C. 20
D. 15
View Answer
Ans : A
Explanation: Compiler error in line "i = 20;" Non-type parameters must be const, so they cannot be modified.
17. What will be the output of this program?
#include <iostream>
using namespace std;
template<int n> struct funStruct
{
static const int val = 2*funStruct<n-1>::val;
};
template<> struct funStruct<0>
{
static const int val = 1 ;
};
int main()
{
cout << funStruct<10>::val << endl;
return 0;
}
A. Compiler Error
B. 1
C. 2
D. 1024
View Answer
Ans : D
Explanation: This is an example of template metaprogramming. The program mainly calculates 2^10.
18. What will be the output of the following program?
Note:Includes all required header files
using namespace std;
template < typename T >
void print_mydata(T find)
{
cout << find << endl;
}
int main()
{
double p = 17.5;
string s("Hi to all");
print_mydata( p );
print_mydata( s );
return 0;
}
A. 17.5 Hi to all
B. 17.5
C. Hi to all
D. None of the above
View Answer
Ans : A
Explanation: In this program, We are passing the value to the template and printing it in the template.
19. How many bits of memory needed for internal representation of class?
A. 1
B. 2
C. 4
D. No memory needed
View Answer
Ans : C
Explanation: classes that contain only type members, nonvirtual function members, and static data members do not require memory at run time.
20. What can be passed by non-type template parameters during compile time?
A. Int
B. Float
C. Constant expression
D. None of the above
View Answer
Ans : C
Explanation: Non-type template parameters provide the ability to pass a constant expression at compile time. The constant expression may also be an address of a function, object or static class member.
21. How many bits of memory needed for internal representation of class?
A. 1
B. 2
C. 4
D. No memory needed
View Answer
Ans : D
Explanation: classes that contain only type members, nonvirtual function members, and static data members do not require memory at run time.
22. Which value is placed in the base class?
A. Derived values
B. Default type values
C. Both A & B
D. None of the above
View Answer
Ans : B
Explanation: We can place the default type values in a base class and overriding some of them through derivation.
23. What is other name of full specialization?
A. Explicit specialization
B. Implicit specialization
C. Function overloading template
D. None of the above
View Answer
Ans : A
Explanation: None
24. A common activity performed on a container is called
A. Functioning
B. Iterator
C. Traversal
D. All of them
View Answer
Ans : C
Explanation: None
25. How many types of templates are there in c++?
A. 1
B. 2
C. 3
D. 4
View Answer
Ans : B
Explanation: There are two types of templates. They are function template and class template.
26. Which is dependant on template parameter?
A. Base class
B. Abstract class
C. Method
D. None of the above
View Answer
Ans : A
Explanation: None
27. Pick out the correct statement about string template?
A. It is used to replace a string.
B. It is used to replace a string with another string at runtime.
C. It is used to delete a string.
D. None of the above
View Answer
Ans : B
Explanation: Every string template is used to replace the string with another string at runtime
28. Containers have the same types, that's why they are called
A. Heterogeneous
B. Homogeneous
C. Vectors
D. None of them
View Answer
Ans : B
Explanation: None
Also check :
Discussion