Template Arguments To Specify Policy Usage(C++ Programming ) Questions and Answers

Question 1.

Which of the things does not require instantiation?


  1.    functions
  2.    non virtual member function
  3.    member class
  4.    all of the mentioned
Explanation:-
Answer: Option D. -> all of the mentioned

The compiler does not generate definitions for functions, non virtual member functions, class 

or member class because it does not require instantiation.



Question 2.

Which parameter is legal for non-type template?


  1.    pointer to member
  2.    object
  3.    class
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> pointer to member

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.



Question 3.

Why we use :: template-template parameter?


  1.    binding
  2.    rebinding
  3.    both a & b
  4.    none of these
Explanation:-
Answer: Option C. -> both a & b

It is used to adapt a policy into binary ones.


Question 4.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
template
4.
void loopIt(T x)
5.
{
6.
T val[count];
7.
for(int ii = 0; ii < count; ii++)
8.
{
9.
val[ii] = x++;
10.
cout
  1.    binding
  2.    rebinding
  3.    both a & b
  4.    none of these
Explanation:-
Answer: Option C. -> both a & b

Answer:d
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



Question 5.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
template
4.
class Test
5.
{
6.
public:
7.
Test()
8.
{
9.
};
10.
~Test()
11.
{
12.
};
13.
type Funct1(type Var1)
14.
{
15.
return Var1;
16.
}
17.
type Funct2(type Var2)
18.
{
19.
return Var2;
20.
}
21.
};
22.
int main()
23.
{
24.
Test Var1;
25.
Test Var2;
26.
cout
  1.    100
  2.    200
  3.    3.123
  4.    2003.123
Explanation:-
Answer: Option D. -> 2003.123

In this program, We are passing the value and returning it from template.
Output:
$ g++ farg3.cpp
$ a.out
2003.123