C++ programming interview questions on “Function Templates” along with answers, explanations and/or solutions:
1. What is a function template?
a) creating a function without having to specify the exact type
b) creating a function with having an exact type
c) creating a function without having blank spaces
d) creating a function without class
Answer: a
Clarification: Function template is used to create a function without having to specify the exact type.
2. Which is used to describe the function using placeholder types?
a) template parameters
b) template type parameters
c) template type
d) type parameters
Answer: b
Clarification: During runtime, We can choose the appropriate type for the function and it is called as template type parameters.
3. Pick out the correct statement.
a) you only need to write one function, and it will work with many different types
b) it will take a long time to execute
c) duplicate code is increased
d) it will take a long time to execute & duplicate code is increased
Answer: a
Clarification: Because of template type parameters, It will work with many types and saves a lot of time.
4. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
template<typename type>
-
type Max(type Var1, type Var2)
-
{
-
return Var1 > Var2 ? Var1:Var2;
-
}
-
int main()
-
{
-
int p;
-
p = Max(100, 200);
-
cout << p << endl;
-
return 0;
-
}
a) 100
b) 200
c) 300
d) 100200
Answer: b
Clarification: In this program, We are returning the maximum value by using function template.
Output:
$ g++ ftemp.cpp $ a.out 200
5. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
template<typename type>
-
class Test
-
{
-
public:
-
Test()
-
{
-
};
-
~Test()
-
{
-
};
-
type Funct1(type Var1)
-
{
-
return Var1;
-
}
-
type Funct2(type Var2)
-
{
-
return Var2;
-
}
-
};
-
int main()
-
{
-
Test<int> Var1;
-
Test<float> Var2;
-
cout << Var1.Funct1(200) << endl;
-
cout << Var2.Funct2(3.123) << endl;
-
return 0;
-
}
a)
200 3.123
b)
3.123 200
c) 200
d) 3.123
Answer: a
Clarification: In this program, We are passing the values and getting it back from template. And we are using the constructor and destructor for the function template.
Output:
$ g++ ftemp1.cpp $ a.out 200 3.123
6. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
template<typename type>
-
class TestVirt
-
{
-
public:
-
virtual type TestFunct(type Var1)
-
{
-
return Var1 * 2;
-
}
-
};
-
int main()
-
{
-
TestVirt<int> Var1;
-
cout << Var1.TestFunct(100) << endl;
-
return 0;
-
}
a) 100
b) 200
c) 50
d) 150
Answer: b
Clarification: In this program, We are using class to pass the value and then we are manipulating it.
Output:
$ g++ ftemp3.cpp $ a.out 200
7. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
template<typename T>
-
inline T square(T x)
-
{
-
T result;
-
result = x * x;
-
return result;
-
};
-
int main()
-
{
-
int i, ii;
-
float x, xx;
-
double y, yy;
-
i = 2;
-
x = 2.2;
-
y = 2.2;
-
ii = square(i);
-
cout << i << " " << ii << endl;
-
yy = square(y);
-
cout << y << " " << yy << endl;
-
}
a)
2 4 2.2 4.84
b) 2 4
c) error
d) 3 6
Answer: a
Clarification: In this program, We are passing the values and calculating the square of the value by using the function template.
Output:
$ g++ ftemp4.cpp $ a.out 2 4 2.2 4.84
8. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
template<typename T>
-
void loopIt(T x)
-
{
-
int count = 3;
-
T val[count];
-
for (int ii=0; ii < count; ii++)
-
{
-
val[ii] = x++;
-
cout << val[ii] << endl;
-
}
-
};
-
int main()
-
{
-
float xx = 2.1;
-
loopIt(xx);
-
}
a) 2.1
b) 3.1
c)
2.1 3.1 4.1
d) 3.2
Answer: d
Clarification: In this program, We are using the for loop to increment the value by 1 in the function template.
Output:
$ g++ ftemp5.cpp $ a.out 2.1 3.1 4.1
9. What can be passed by non-type template parameters during compile time?
a) int
b) float
c) constant expression
d) string
Answer: c
Clarification: 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.
10. From where does the template class derived?
a) regular non-templated C++ class
b) templated class
c) regular non-templated C++ class or templated class
d) main function
Answer: c
Clarification: Class derived template is derived from regular non-templated C++ class or templated class.