Templates M9 Test

  1. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>
    using namespace std;
    template<class Ty> class Pocket
    {
      Ty value;
    public:
      Pocket(){}
      Pocket(Ty value);
      Ty getValue()
      {
        return value;
      }
      void add (Ty _Right)
      {
        value += _Right;
      }
      template < class Tx > Tx get (Tx _Right)
      {
        return (Tx) (value) + _Right; //LINE I
      }
      friend ostream & operator<<(ostream & _os, const Pocket<Ty> &value)
      {
        _os << value.value;
        return _os;
      }
    };
    template<class T> Pocket < T >::Pocket (T value):value (value){}
    
    int
    main()
    {
      Pocket < string > a ("Hi");
      string n ("Tech");
      a.add(n); //LINE II
      cout << a << ", ";
      cout << a.get < double >(1);
      return 0;
    }
    •  the program outputs HiTech,
    • compilation error in LINE I
    • the program outputs Hi, Tech
    • runtime error at LINE I
    • compilation error in LINE II
    • the program outputs HiTech, HiTech
  2. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;
    template<class T> class Pocket
    {
      T value; //LINE I
    public:
      Pocket (T value):value (value)
      {
      }
    };
    int
    main()
    {
       Pocket < double > a (7);
       cout << a.value << end1; //LINE II
       return 0;
    }
    •  the program outputs 127.45, 0x7f.2d, 0x7f.2d
    • compilation error in LINE II
    • compilation error in LINE I
    • runtime error at LINE I
    •  the program outputs 127.45, 0x7f.2d, 7f.2d
    •  the program outputs 127.45, 0x7f, 7f
  3. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;
    template<class A> void
    f (A & a) //LINE I
    {
      cout << 1 + a << endl;
    }
    void
    f (double &a) //LINE II
    {
      cout << 2 + a << endl;
    }
    int
    main()
    {
       float a = 1.5;
       f < float &>(a); //LINE II
       return 0;
    }
    •  the program outputs 2.5
    • the program outputs 2
    • the program outputs 3.5
    • compilation error in LINE II
    • compilation error in LINE I
    • runtime error at LINE II
  4. What will happen when you attempt to compile and run the following code assuming that you will enter following sequence: 9 8 7<enter>?

    #include <iostream>
    #include <string>
    using namespace std;
    template<typename T> class Pocket
    {
      T value;
     public:
      Pocket()
      {
      }
      Pocket (T value);
      T getValue()
      {
        return value;
      }
      void add (T _Right)
      {
        value += _Right;
      }
    };
    template < class T>Pocket <T>::Pocket (T value):value(value){}
    int
    main()
    {
      Pocket <string> a("Hi");
      string n ("Maker");
      a.add(n); //LINE I
      cout<< a.getValue() <<", ";
      a.add (3); //LINE II
      cout <<a.getValue();
      return 0;
    }
    •  the program outputs HiMaker
    • compilation error in LINE II
    • the program outputs HiMaker, HiMaker
    • runtime error at LINE II
    • compilation error in LINE I
    • the program outputs Hi, Maker
  5. What happens if you try to compile and run this program?

    #include <iostream>
    #include <string>
    using namespace std;
    template <class T> class Pocket
    {
    public:
      T value; //LINE I
    Pocket(T value):value(value){}
    };
    int
    main()
    {
      Pocket<double>a(7);
      cout << a.value<<endl; //LINE II
      return 0;
    }
    • the program outputs 0
    • the program outputs 7.0000
    • compilation error in LINE II
    • runtime error at LINE II
    • the program outputs 7
    • compilation error in LINE I
  6. What happens if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    class SomethingSpacial
    {
    public:
      double value;
       SomethingSpacial():value(0){}
       SomethingSpacial(double value):value(value){}
       SomethingSpacial operator += (SomethingSpacial & _Right)
      {
        SomethingSpacial result;
        result.value = value + _Right.value;
        return result;
      }
    };
    template<typename T>class Pocket
    {
      T value;
    public:
      Pocket(){}
      Pocket (T value);
      T getValue()
      {
        return value;
      }
      void add(T _Right)
     {
       value += _Right;
     }
    };
    template<class T> Pocket<T>::Pocket(T value):value(value){}
    int
    main()
    {
      Pocket<double>a(7); //LINE I
      Pocket<SomethingSpacial> n;
      n.add(SomethingSpacial()); //LINE II
      cout << a.getValue()<<", ";
      a.add(3);
      cout << a.getValue();
      return 0;
    }
    • the program outputs 10, 13
    • the program outputs 7, 10
    • the program outputs 7, 7
    • compilation error in LINE II
    • compilation error in LINE II
    • runtime error at LINE II
  7.  What will happen when you attempt to compile and run the following code assuming that you will enter following sequence:9 8 a <enter>?

    #include <iostream>
    #include <string>
    using namespace std;
    template<typename T>class Pocket
    {
      T value;
      public:
      Pocket(){}
      Pocket(T value);
      T getValue()
      {
        return value;
      }
      void add(T _Right)
      {
        value += _Right;
      }
    };
    template<class T> Pocket<T>::Pocket(T value):value(value){}
    int
    main()
    {
      Pocket<string>a("Hi");
      string n("Maker");
      a.add(n); //LINE I
      cout << a.getValue() <<", "; //LINE II
      cout <<a.getValue();
      return 0;
    }
    • the program outputs HiMaker, HiMaker
    • runtime error at LINE II
    • compilation error in LINE I
    • the program outputs Hi, Maker
    • the program outputs HiMaker, 
    • compilation error in LINE II
  8. What happens if you try to compile and run this program?

    #include <iostream>
    #include <string>
    using namespace std;
    template<typedef T> //LINE I
      class Pocket
    {
    public:
      T value;
      Pocket (T value);
    };
    template<class T>Pocket<T>::Pocket(T value):value(value){} //LINE II
    int
    main()
    {
      Pocket<double> a(7);
      cout << a.value<<endl;
      return 0;
    }
    • runtime error at LINE II
    • the program outputs 7
    • compilation error in LINE II
    • the program outputs 0
    • compilation error in LINE I
    • the program outputs 7.0000
  9. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;
    template<class T>void
    f(T & a) //LINE I
    {
      cout << 1 + a << endl;
    }
    int
    main()
    {
      int a = 1;
      f(a); //LINE II
      return 0;
    }
    • the program outputs 2
    • runtime error at LINE II
    • compilation error in LINE II
    • the program outputs 3
    • compilation error in LINE I
    • the program outputs 1
  10.  What happen if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    template<class T>void
    f(T & a) //LINE I
    {
       cout << 1 + a << endl;
    }
    void
    f(double &a) //LINE II
    {
       cout << 2 + a << endl;
    }
    int
    main()
    {
       double a = 1.5;
       f(a); //LINE II
       return 0;
    }
    • the program outputs 2.5
    • runtime error at LINE II
    • the program outputs 3.5
    • the program outputs 2
    • compilation error in LINE I
    • compilation error in LINE II
  11.  What happen if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    template<class T> class Pocket
    {
      T _v;
    public:
      Pocket(){}
    Pocket(T v):_v (v){}
      T getV()
      {
        return _v;
      }
      void add(T & a)
      {
         _v += a;
      }
    };
    int
    main()
    {
      Pocket<string> a("Hello");
      string s (" world!");
      a.add(s);
      cout << a.getV() <<endl;
      return 0;
    }
    • the program outputs Hello
    • the program outputs Hello world!
    • program will run without any output
    • program will not compile
  12.  What happen if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    class NothingSpecial{};
    template<typename T> class Pocket
    {
      T value; //LINE I
    public:
      Pocket(){}
      Pocket (T value);
      T getValue()
      {
        return value;
      }
      void add (T _Right)
      {
         value += _Right;
      }
    };
    template <class T> Pocket<T>::Pocket(T value):value(value){}
    int
    main()
    {
      Pocket<double> a(7);
      Pocket<NothingSpecial> n; //LINE II
      a.add(3);
      cout << a.getValue() <<", ";
      a.add(3);
      cout << a.getValue();
      return 0;
    }
    • the program outputs 10, 10
    • compilation error in LINE II
    • the program outputs 10, 13
    • compilation error in LINE I
    • runtime error at LINE II
    • the program outputs 7, 7
  13.  What happen if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    template<class T> class Pocket
    {
    public:
    T value;
    Pocket (T value);
    };
    template <class T> Pocket::Pocket(T value):value(value)
    {
    } //LINE II
    
    int
    main()
    {
    Pocket<double>
    a(7); //LINE II
    cout << a.value <<endl;
    return 0;
    }
    • the program outputs 7.0000
    • the program outputs 7
    • the program outputs 0
    • runtime error at LINE II
    • compilation error in LINE I
    • compilation error in LINE II
  14.  What happen if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    class SomethingSpecial{};
    template<typename T> class Pocket
    {
      T value;
    public:
      Pocket(){}
    
      Pocket(T value);
      T getValue()
      {
        return value;
      }
      void add (T _Right)
      {
        value += _Right;
      }
    };
    template <class T> Pocket<T>::Pocket(T value):value(value){}
    int
    main()
    {
      Pocket<double>a (7); //LINE I
      Pocket<SomethingSpecial> n;
      n.add(SomethingSpecial()); //LINE II
      cout << a.getValue() <<", ";
      a.add(3);
      cout << a.getValue();
      return 0;
    }
    • compilation error in LINE II
    • compilation error in LINE I
    • the program outputs 7, 7
    • the program outputs 10, 13
    • runtime error at LINE II
    • the program outputs 7, 10
  15.  What happen if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    template<class T> class Pocket
    {
    public:
      T value;
      Pocket (T value);
    };
    template <class T> Pocket<T>::Pocket(T value):value(value){}
    int
    main()
    {
      Pocket<double>a(7); //LINE II
      cout << a.value<<endl;
      return 0;
    }
    • the program outputs 7
    • compilation error in LINE I
    • compilation error in LINE II
    • the program outputs 0
    • the program outputs 7.0000
    • runtime error at LINE II
  16. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>
    using namespace std;
    template<typename T> //LINE I
    class Pocket
    {
       public:
       T value;
       Pocket(T value);
    };
    template<class T>Pocket<T>::Pocket(T value):value(value){} //LINE II
    int
    main()
    {
      Pocket<double> a(7);
      cout << a.value<<endl;
      return 0;
    }
    • the program putouts 0
    • the program putouts 7.0000
    • runtime error at LINE II
    • compilation error in LINE II
    • the program outputs 7
    • compilation error in LINE II
  17. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>
    using namespace std;
    template<typename T> class Pocket
    {
      T value;
    public:
      Pocket (){}
      Pocket(T value);
      T getValue()
      {
         return value;
      }
      void add(T _Right)
      {
         value += _Right;
      }
      void add (string & _Right)
      {
          value.insert(0, _Right);
      }
    };
    template <class T> Pocket<T>::Pocket(T value):value(value){}
    int
    main()
    {
      Pocket<string>a("Hi");
      string n ("Tech");
      a.add(n); //LINE I
      cout << a.getValue() <<", "; //LINE II
      cout <<a.getValue();
      return 0;
    }
    • runtime error at LINE II
    • compilation error in LINE I
    • the program outputs HiTech, HiTech
    • the program outputs HiTech,
    • compilation error in LINE II
    • the program outputs Hi, Tech
  18. What happen if you try to compile and run the this  program?

    #include <iostream>
    using namespace std;
    template<class T>void //LINE I
      f(T & a)
      {
        cout << 1 + a << endl;
      }
      void
      f(double &a) //LINE II
      {
        cout << 2 + a << endl;
      }
    int
    main()
    {
      int a =1.5;
      f(a); //LINE II
      return 0;
    }
    • the program outputs 3.5
    • the program outputs 2.5
    • compilation error in LINE II
    • compilation error in LINE I
    • runtime error at LINE II
    • the program outputs 2
  19. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>
    using namespace std;
    template<class Ty> class Pocket
    {
      Ty value;
    public:
      Pocket(){}
       Pocket (Ty value);
      Ty getValeu()
     {
        return value;
     }
     void add(Ty _Right)
     {
       value += _Right;
     }
     template<class Tx> Tx get(Tx _Right)
      {
         return (Tx)(value) + _Right; //LINE I
      }
     friend ostream & operator << (ostream & _os, const Pocket<Ty> & value)
      {
        _os <<value.value;
        return _os;
      }
    };
    template<class T> Pocket<T>::Pocket(T value):value(value){}
    int
    main()
    {
        Pocket<int> a(7);
        cout <<a<<", ";
        cout <<a.get<double>(2);
        return 0;
    }
    • the program outputs 7
    • the program outputs 7, 9
    • compilation error in LINE I
    • the program outputs 7, 2
    • runtime error at LINE I
    • compilation error in LINE II
  20. What happen if you try to compile and run the this  program?

    #include <iostream>
    using namespace std;
    template<class T> void
    f(T & a) //LINE I
    {
      cout << 1 +a << endl;
    }
    voidf(double & a)
    {
      cout <<2 + a<< endl;
    }
    int
    main()
    {
      double a = 1.5;
      f<double &> (a); //LINE II
      return 0;
    }
    • the program outputs 127, 7f, 0x7f
    • compilation error in LINE II
    • compilation error in LINE I
    • runtime error at LINE II
    • the program outputs 2
    • the program outputs 2.5
    • the program outputs 3.5