C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Template Arguments to Specify Policy”.
Q 1. What is meant by the template parameter?
A. It can be used to delete a type
B. It can be used to evaluate a type
C. It can of no return type
D. It can be used to pass a type as an argument
Show Answer
Answer:-D. It can be used to pass a type as an argumentExplanation
A template parameter is a special kind of parameter that can be used to pass a type as argument.Q 2. Which keyword can be used in template?
A. class
B. typename
C. function
D. both class & typename
Show Answer
Answer:-D. both class & typenameExplanation
Both keywords can be used as shown below: template
Q 3. What is the validity of template parameters?
A. inside the class
B. inside that block only
C. whole program
D. inside the main class
Show Answer
Answer:-B. inside that block onlyExplanation
Template parameters are valid inside a block only i.e. they have block scope.
Q 4. Why we use :: template-template parameter?
A. binding
B. rebinding
C. reusing
D. both binding & rebinding
Show Answer
Answer:-D. both binding & rebindingExplanation
It is used to adapt a policy into binary ones.
Q 5. Which of the things does not require instantiation?
A. functions
B. non virtual member function
C. member class
D. all of the mentioned
Show Answer
Answer:-D. all of the mentionedExplanation
The compiler does not generate definitions for functions, non virtual member functions, class or member class because it does not require instantiation.Q 6. Which parameter is legal for non-type template?
A. pointer to member
B. object
C. class
D. baseclass
Show Answer
Answer:-A. pointer to memberExplanation
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.
Q 7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
template <class type>
class Test
{
public:
Test()
{
};
~Test()
{
};
type Funct1(type Var1)
{
return Var1;
}
type Funct2(type Var2)
{
return Var2;
}
};
int main()
{
Test<int> Var1;
Test<double> Var2;
cout << Var1.Funct1(200);
cout << Var2.Funct2(3.123);
return 0;
}
A. 100
B. 200
C. 3.123
D. 2003.123
Show Answer
Answer:-D. 2003.123Explanation
In this program, We are passing the value and returning it from template. Output: $ g++ farg3.cpp $ a.out 2003.123Q 8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
template <class T, int N>
class mysequence
{
T memblock [N];
public:
void setmember (int x, T value);
T getmember (int x);
};
template <class T, int N>
void mysequence<T,N> :: setmember (int x, T value)
{
memblock[x] = value;
}
template <class T, int N>
T mysequence<T,N> :: getmember (int x)
{
return memblock[x];
}
int main ()
{
mysequence <int, 5> myints;
mysequence <double, 5> 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
Show Answer
Answer:-C. 100 3.1416Explanation
In this program, We are printing the integer in the first function and float in the second function. Output: $ g++ farg.cpp $ a.out 100 3.1416
Q 9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
template <class T>
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
Show Answer
Answer:-B. 6 10Explanation
In this program, We are using the ternary operator on the template function. Output: $ g++ farg.cpp $ a.out 6 10
Q 10. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
template <typename T, int count>
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<float, 3>(xx);
}
A. 2.1
B. 3.1
C. 2.1
3.1
4.1
D. 4.1
Leave a Reply