1. What is meant by the template parameter?
A. It can be used to pass a type as an argument
B. It can be used to evaluate a type
C. It can of no return type
D. It can be used to delete a type
Answer: A
Explanation:
A template parameter is a special kind of parameter that can be used to pass a type as argument.
2. Which keyword can be used in template?
A. class
B. typename
C. both class & typename
D. function
Answer: C
Explanation:
Both keywords can be used as shown below:
template <class T> function declaration;
template <typename T> function declaration;
3. What is the validity of template parameters?
A. inside that block only
B. inside the class
C. whole program
D. inside the main class
Answer: A
Explanation:
Template parameters are valid inside a block only i.e. they have block scope.
4. What will be the output of the following C++ code?
#include
using namespace std;
template
class mysequence
{
T memblock [N];
public:
void setmember (int x, T value);
T getmember (int x);
};
template
void mysequence :: setmember (int x, T value)
{
memblock[x] = value;
}
template
T mysequence :: getmember (int x)
{
return memblock[x];
}
int main ()
{
mysequence myints;
mysequence myfloats;
myints.setmember (0, 100);
myfloats.setmember (3, 3.1416);
cout << myints.getmember(0) << '\n';
cout << myfloats.getmember(3) << '\n';
return 0;
}
A. 100
B. 3.1416
C.
100
3.1416
D. 4.14
Answer: C
Explanation:
In this program, We are printing the integer in the first function and float in the second function.
5. What will be the output of the following C++ code?
#include
using namespace std;
template
T max (T& a, T& B.
{
return (a>b?a:B.;
}
int main ()
{
int i = 5, j = 6, k;
long l = 10, m = 5, n;
k = max(i, j);
n = max(l, m);
cout << k << endl;
cout << n << endl;
return 0;
}
A. 6
B.6
10
C.5
10
D. 5
Answer: B
Explanation:
In this program, We are using the ternary operator on the template function.
6. 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);
cout << Var2.Funct2(3.123);
return 0;
}
A. 100
B. 200
C. 3.123
D. 2003.123
Answer: D
Explanation:
In this program, We are passing the value and returning it from template.
7. What will be the output of the following C++ code?
#include
using namespace std;
template
void loopIt(T x)
{
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. 4.1
Answer: C
Explanation:
In this program, We are using the non-type template parameter to increment the value in the function template.
8. Why we use :: template-template parameter?
A. binding
B. rebinding
C. both binding & rebinding
D. reusing
Answer: C
Explanation:
It is used to adapt a policy into binary ones.
9. Which parameter is legal for non-type template?
A. pointer to member
B. object
C. class
D. baseclass
Answer: A
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.
10. Which of the things does not require instantiation?
A. functions
B. non virtual member function
C. member class
D. all of the mentioned
Answer: D
Explanation:
The compiler does not generate definitions for functions, non virtual member functions, class or member class because it does not require instantiation.
- 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