C++ Multiple Choice and Coding Questions on Function
and Class Templates
Function Template Questions:
1. What is the correct syntax to declare a function template in C++?
a) template <typename T> void func(T a) { }
b) template function <T> void func(T a) { }
c) function template <T> void func(T a) { }
d) void template <T> func(T a) { }
2. What is the main advantage of using function templates in C++?
a) Faster execution speed
b) Reduce code duplication
c) Increases code size
d) None of the above
3. Which of the following is true about function templates?
a) A function template can only accept one type parameter.
b) Function templates must have at least one non-template parameter.
c) A function template can have multiple type parameters.
d) Function templates cannot be overloaded.
4. What will be the output of the following C++ program?
```cpp
#include <iostream>
using namespace std;
template <typename T>
void display(T a) {
cout << a << endl;
}
int main() {
display(10);
display(3.14);
display("Hello"); return 0;}
```
a) Compilation Error
b) 10
3.14
Hello
c) 10.0
3.14
Hello
d) None of the above
5. Can function templates be specialized for a particular data type?
a) No
b) Yes, using explicit specialization
c) Yes, using static keyword
d) Yes, using virtual functions
Class Template Questions:
6. Which of the following is the correct syntax for declaring a class
template?
a) class template <T> class Sample { }
b) template <typename T> class Sample { }
c) template class <T> class Sample { }
d) template class Sample<T> { }
7. What happens when a class template is instantiated with a specific
data type?
a) A separate copy of the class is created for that data type
b) A new function is created
c) The compiler generates a runtime error
d) The template is ignored
8. What will be the output of the following C++ code?
```cpp
#include <iostream>
using namespace std;
template <class T>
class Box {
private:
T data;
public:
Box(T d) { data = d; }
void show() { cout << data << endl; }
};
int main() {
Box<int> obj1(10);
Box<double> obj2(5.5);
obj1.show();
obj2.show();
return 0;
}
```
a) Compilation Error
b) 10
5.5
c) 10.0
5.5
d) None of the above
9. How can a class template be explicitly instantiated?
a) template class Sample<int>;
b) Sample<int> obj;
c) template<int> class Sample;
d) int Sample;
10. Can a class template have multiple type parameters?
a) No
b) Yes, but only two
c) Yes, using template <typename T1, typename T2>
d) Yes, but only in derived classes
1. write a program for Generic Stack using function and class
templates.
2. write a program for Insertion Sort using function and class
templates.
Answers:
Function Template Questions:
1. a) template void func(T a) { }
2. b) Reduce code duplication
3. c) A function template can have multiple type parameters.
4. b) 10
3.14
Hello
5. b) Yes, using explicit specialization
Class Template Questions:
6. b) template class Sample { }
7. a) A separate copy of the class is created for that data type
8. b) 10
5.5
9. a) template class Sample;
10. c) Yes, using template <typename T1, typename T2>