Here is a listing of C multiple choice questions on “Template Arguments to Specify Policy Usage” along with answers, explanations and/or solutions:
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
View Answer
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
View Answer
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
View Answer
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 <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
View Answer
Explanation: 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
5. 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
View Answer
Explanation: In this program, We are using the ternary operator on the template function.
Output:
$ g++ farg.cpp $ a.out 6 10
6. 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
View Answer
Explanation: In this program, We are passing the value and returning it from template.
Output:
$ g++ farg3.cpp $ a.out 2003.123
7. 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
View Answer
Explanation: In this program, We are using the non-type template parameter to increment the value in the function template.
Output:
$ g++ farg4.cpp $ a.out 2.1 3.1 4.1
8. Why we use :: template-template parameter?
a) binding
b) rebinding
c) both binding & rebinding
d) reusing
View Answer
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
View Answer
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
View Answer
Explanation: The compiler does not generate definitions for functions, non virtual member functions, class or member class because it does not require instantiation.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for C++ Internship
- Check C++ Books
- Check Programming Books