• ( 1 ) Output?
      #include
      using namespace std;
      template struct funStruct
      {
      static const int val = 2*funStruct::val;
      };
      template<> struct funStruct<0>
      {
      static const int val = 1 ;
      };
      int main()
      {
      cout << funStruct<10>::val << endl;
      return 0;
      }

    • 1) Compiler Error
    • 2) 1024
    • 3) 2
    • 4) 1
    • Discussion in forum
      Answer : 2) 1024
      Solution :








      discussion


      Answer : 2) 1024

    • ( 2 ) What is a function template?

    • 1) creating a function without having to specify the exact type.
    • 2) creating a function with having a exact type.
    • 3) Both 1 & 2
    • 4) None of these
    • Discussion in forum
      Answer : 1) creating a function without having to specify the exact type.
      Solution :








      discussion


      Answer : 1) creating a function without having to specify the exact type.

    • ( 4 ) Which parameter is legal for non-type template?

    • 1) pointer to member
    • 2) object
    • 3) class
    • 4) None of these
    • Discussion in forum
      Answer : 1) pointer to member
      Solution : 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.








      discussion


      Answer : 1) pointer to member

    • ( 5 ) From where does the template class derived?

    • 1) regular non-templated C++ class
    • 2) templated class
    • 3) Both 1 & 2
    • 4) None of these
    • Discussion in forum
      Answer : 3) Both 1 & 2
      Solution :








      discussion


      Answer : 3) Both 1 & 2

    • ( 6 ) Which one is suitable syntax for function template?

    • 1) template< class T> return_type Function_Name(parameters)
    • 2) template< typename T> return_type Function_Name(parameters)
    • 3) Both 1 & 2
    • 4) None of these
    • Discussion in forum
      Answer : 3) Both 1 & 2
      Solution : Both class and typename keywords can be used alternatively for specifying a generic type in a template.








      discussion


      Answer : 3) Both 1 & 2

    • ( 7 ) What is the output of this program?
      #include
      #include
      #include
      using namespace std;
      template
      type MyMax(const type Var1, const type Var2)
      {
      cout << "no specialization";
      return Var1 < Var2 ? Var2 : Var1;
      }
      template <>
      const char *MyMax(const char *Var1, const char *Var2)
      {
      return (strcmp(Var1, Var2)<0) ? Var2 : Var1;
      }
      int main()
      {
      string Str1 = "class", Str2 = "template";
      const char *Var3 = "class";
      const char *Var4 = "template";
      const char *q = MyMax(Var3, Var4);
      cout << q << endl;
      return 0;
      }

    • 1) template
    • 2) class
    • 3) no specialization
    • 4) None of these
    • Discussion in forum
      Answer : 1) template
      Solution :








      discussion


      Answer : 1) template

    • ( 8 ) Function templates can accept

    • 1) any type of parameters
    • 2) only one parameter
    • 3) only parameters of the basic type
    • 4) only parameters of the derived type
    • Discussion in forum
      Answer : 3) only parameters of the basic type
      Solution :








      discussion


      Answer : 3) only parameters of the basic type

    • ( 9 ) Pick out the correct statement.

    • 1) you only need to write one function, and it will work with many different types.
    • 2) it will take a long time to execute
    • 3) duplicate code is increased
    • 4) None of these
    • Discussion in forum
      Answer : 1) you only need to write one function, and it will work with many different types.
      Solution : Because of template type parameters, It will work with many types and saves a lot of time








      discussion


      Answer : 1) you only need to write one function, and it will work with many different types.





Top