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
Explanation:
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
Explanation:
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
Explanation:
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
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
Explanation:
In this program, We are returning the maximum value by using function template.
5. What will be the output of the following C++ code?
#include
using namespace std;
template
class Test
{
public:
Test()
{
};
~Test()
{
};
type Funct1(type Var1)
{
return Var1;
}
type Funct2(type Var2)
{
return Var2;
}
};
int main()
{
Test Var1;
Test 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
Explanation:
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.
6. What will be the output of the following C++ code?
#include
using namespace std;
template
class TestVirt
{
public:
virtual type TestFunct(type Var1)
{
return Var1 * 2;
}
};
int main()
{
TestVirt Var1;
cout << Var1.TestFunct(100) << endl;
return 0;
}
A. 100
B. 200
C. 50
D. 150
Answer: B
Explanation:
In this program, We are using class to pass the value and then we are manipulating it.
7. What will be the output of the following C++ code?
#include
using namespace std;
template
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
Explanation:
In this program, We are passing the values and calculating the square of the value by using the function template.
8. What will be the output of the following C++ code?
#include
using namespace std;
template
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);
}
} }; 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
Explanation:
In this program, We are using the for loop to increment the value by 1 in the function template.
9. What can be passed by non-type template parameters during compile time?
A. int
B. float
C. constant expression
D. string
Answer: 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.
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
Explanation:
Class derived template is derived from regular non-templated C++ class or templated class.
- 10+ TOP C++ MCQs on OOPs Concept – 4
- 10+ TOP MCQs on C++ vs C
- 10+ TOP C++ MCQs on Sizes
- 10+ TOP C++ MCQs on Void
- 10+ TOP C++ MCQs on Pointer to Void
- 10+ TOP C++ MCQs on Pointer to Structures
- 10+ TOP C++ MCQs on Operator Overloading – 1
- 10+ TOP C++ MCQs on Operator Overloading – 2