C++ Programming Questions and Answers – Template Arguments to Specify Policy Usage

This section on C++ MCQs (multiple choice questions) focuses on “Template Arguments to Specify Policy Usage”. One shall practice these MCQs to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our multiple choice questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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

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
View Answer

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
View Answer

Answer: a
Explanation: Template parameters are valid inside a block only i.e. they have block scope.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.    #include <iostream>
  2.    using namespace std;
  3.    template <class T, int N>
  4.    class mysequence 
  5.    {
  6.        T memblock [N];
  7.        public:
  8.        void setmember (int x, T value);
  9.        T getmember (int x);
  10.    };
  11.    template <class T, int N>
  12.    void mysequence<T,N> :: setmember (int x, T value) 
  13.    {
  14.        memblock[x] = value;
  15.    }
  16.    template <class T, int N>
  17.    T mysequence<T,N> :: getmember (int x) 
  18.    {
  19.        return memblock[x];
  20.    }
  21.    int main () 
  22.    {  
  23.        mysequence <int, 5> myints;
  24.        mysequence <double, 5> myfloats;
  25.        myints.setmember (0, 100);
  26.        myfloats.setmember (3, 3.1416);
  27.        cout << myints.getmember(0) << '\n';
  28.        cout << myfloats.getmember(3) << '\n';
  29.        return 0;
  30.    }

a) 100
b) 3.1416
c)

100
3.1416
Note: Join free Sanfoundry classes at Telegram or Youtube

d) 4.14
View Answer

Answer: c
Explanation: In this program, We are printing the integer in the first function and float in the second function.
Output:

advertisement
$ g++ farg.cpp
$ a.out
100
3.1416

5. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class T>
  4.     T max (T& a, T& b) 
  5.     {
  6.         return (a>b?a:b);
  7.     }
  8.     int main () 
  9.     {
  10.         int i = 5, j = 6, k;
  11.         long l = 10, m = 5, n;
  12.         k = max(i, j);
  13.         n = max(l, m);
  14.         cout << k << endl;
  15.         cout << n << endl;
  16.         return 0;
  17.     }

a) 6
b)

6
10

c)

5
10

d) 5
View Answer

Answer: b
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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class type>
  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<int> Var1;
  25.         Test<double> Var2;
  26.         cout << Var1.Funct1(200);
  27.         cout << Var2.Funct2(3.123);
  28.         return 0;
  29.     }

a) 100
b) 200
c) 3.123
d) 2003.123
View Answer

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <typename T, int count>
  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 <<  val[ii] << endl;
  11.         }
  12.     };
  13.     int main()
  14.     {
  15.         float xx = 2.1;
  16.         loopIt<float, 3>(xx);
  17.     }

a) 2.1
b) 3.1
c)

   2.1
   3.1
   4.1

d) 4.1
View Answer

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

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
View Answer

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
View Answer

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.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.