Study Guide 13: Templates

  1. When creating a template function, the template argument or variable is preceded by the keyword ____________ .
    1. template
    2. class
    3. typename
    4. template or class
    5. class or typename
  2. When creating a template class, the template argument or variable is preceded by the keyword ____________ .
    1. template
    2. class
    3. typename
    4. template or class
    5. class or typename
  3. A template class
    1. is designed to be stored in different containers.
    2. works with different data types.
    3. generates objects which must all be identical.
    4. generates classes with different numbers of member functions.
  4. There can be more than one template argument.
    1. True
    2. False
  5. When a class is implemented as a template, all member functions are placed in a header file. Choose the best explanation for why this is done.
    1. It is just a tradition.
    2. The functions cannot be compiled until the template variable(s) is/are expanded; therefore, they can't be placed in a regular library.
    3. This is done so that application programmers can see which parts of the class are based on templates.
    4. This is done to support the debugger, which is otherwise unable to process templates.
  6. FooBar is a container class implemented with one template variable. Which of the following statements correctly instantiates a FooBar container object to hold Bar objects?
    1. Bar myBar;
    2. Bar<FooBar> myBar;
    3. FooBar<Bar> myBar;
    4. FooBar<myBar> Bar;
  7. FooBar is a container class implemented with one template variable. Which of the following is the correct header or beginning for the FooBar class?
    1. template <class T>
      class FooBar
    2. template class T
      class FooBar
    3. class <template T>
      class FooBar
    4. class <template T> FooBar
  8. FooBar is a container class implemented with one template variable. Which of the following correctly begins a FooBar member function named Foo?
    1. template <class T>
      FooBar::Foo()
    2. template class T
      FooBar>T<::Foo()
    3. class <template T>
      class FooBar
    4. class <template T>
      FooBar<T>::Foo( )
    5. template <class T>
      FooBar<T>::Foo( )
  9. Given that CList is a container class implemented as a template that has a default constructor, which of the following is the correct way to define an instance of CList that will hold instances of class Person.
    1. CList<Person> my_list;
    2. CList my_list<Person>;
    3. CList(Person) my_list;
    4. CList my_list(Person);
    5. None of the above
  10. Explain what, if anything, is wrong with the following code fragment. Assume that the classes CList and Person are fully specified and in scope.
         while (true)
         {
             CList<Person> people;
             // add element to people
         }
  11. To use the STL map container class, a program must include the _____________ header file.
    1. <container>
    2. <stl>
    3. <map>
  12. Create am STL map object named foo_bar that maps a foo object to a bar.