C++ programming interview questions and answers focuses on “Function Templates”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ programming interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.
Here is a listing of C++ programming interview questions on “Function Templates” along with answers, explanations and/or solutions:
Q 1. What is a function template?
A. creating a function without class
B. creating a function with having an exact type
C. creating a function without having blank spaces
D. creating a function without having to specify the exact type
Show Answer
Answer:-D. creating a function without having to specify the exact typeExplanation
Function template is used to create a function without having to specify the exact type.Q 2. Which is used to describe the function using placeholder types?
A. template parameters
B. template type parameters
C. template type
D. type parameters
Show Answer
Answer:-B. template type parametersExplanation
During runtime, We can choose the appropriate type for the function and it is called as template type parameters.Q 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
Show Answer
Answer:-A. you only need to write one function, and it will work with many different typesExplanation
Because of template type parameters, It will work with many types and saves a lot of time.Q 4. What will be the output of the following C++ code?
#include <iostream>
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
Show Answer
Answer:-D. 100200Explanation
In this program, We are returning the maximum value by using function template. Output: $ g++ ftemp.cpp $ a.out 200
Q 5. What will be the output of the following C++ code?
#include <iostream>
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
Show Answer
Answer:-A. 200 3.123Explanation
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
Q 6. What will be the output of the following C++ code?
#include <iostream>
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. 50
B. 100
C. 150
D. 200
Show Answer
Answer:-D. 200Explanation
In this program, We are using class to pass the value and then we are manipulating it. Output: $ g++ ftemp3.cpp $ a.out 200
Q 7. What will be the output of the following C++ code?
#include <iostream>
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
Show Answer
Answer:-A. 2 4 2.2 4.84Explanation
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
Q 8. What will be the output of the following C++ code?
#include <iostream>
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
Show Answer
Answer:-C. 2.1 3.1 4.1Explanation
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
Q 9. What can be passed by non-type template parameters during compile time?
A. int
B. float
C. constant expression
D. string
Show Answer
Answer:-C. constant expressionExplanation
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.
Q 10. From where does the template class derived?
A. regular non-templated C++ class
B. templated class
C. main function
D. regular non-templated C++ class or templated class
Leave a Reply