Sample Data Structures Questions
Chapter 6
Software Reuse with Templates

Data Structures and Other Objects Using C++
by Michael Main and Walter Savitch
Second Edition ISBN 0-201-70297-5, Softcover, 816 pages, 2000


The Purpose of These Questions

These are typical exam questions from Chapter 6 of the textbook. These exact questions might not be on your exam, but if you research and find the right answers to these questions, that should be good preparation for a real exam. (It's also possible that some of this material was not covered in your class.) There are 7 short answer questions and 13 multiple choice questions in this file.

Short Answers

    Short Answers
    Sections 6.1
    Template Functions
  1. Write one or two short sentences to clearly explain the advantage of template functions.

  2. Why must the template parameter appear in the parameter list of the template function?

  3. Consider the code below which is supposed to shift the values from data[0] through data[n-1] rightward one position (so these values now reside in data[1] through data[n]).
    
        size_t i;
        for (i = 0; i < n; ++i)
            data[i+1] = data[i];
    
    There is a bug in the above code. Write one sentence to describe the bug and write new code that does the job correctly.

  4. Why is it a bad idea to have a size_t parameter for a template function?

    Short Answers
    Section 6.2
    Template Classes

  5. Suppose that you convert a bag class to a new template class. Name one place where the word bag remains just bag instead of changing to bag<Item>.

  6. Write one or two short sentences to clearly explain the advantage of template classes.

    Short Answers
    Section 6.4
    The Node Template Class

  7. Complete the body of this template function. Check the precondition as much as possible, and don't cause a heap leak.
    
      template <class Item>
      void list_head_remove(node<Item>*& head_ptr)
      //  Precondition: head_ptr is the head pointer of a linked list,
      //  with at least one node.
      //  Postcondition: The head node has been removed and returned to the heap;
      //  head_ptr is now the head pointer of the new, shorter linked list.
    

Multiple Choice

    Multiple Choice
    Sections 6.1
    Template Functions
  1. What is the primary purpose of template functions?
    • A. To allow a single function to be used with varying types of arguments
    • B. To hide the name of the function from the linker (preventing duplicate symbols)
    • C. To implement container classes
    • D. To permit the use of the debugger without the -gstabs flag

  2. Consider this prototype for a template function:
    
        template <class Item>
        void foo(Item x);
    
    Which is the right way to call the foo function with an integer argument i?
    • A. foo( i );
    • B. foo<int>( i );
    • C. foo<Item>( i );
    • D. foo(<int> i );
    • E. foo(<Item> i );

  3. Consider the following definition:
    
        template <class Item>
        Item maximal (Item a, Item b)
        {
            if (a > b)
                return a;
            else
                return b;
        }
    
    What restrictions are placed on the Item data type for a program that uses the maximal function?
    • A. The Item data type must be either int, double, or float.
    • B. The Item data type must be one of the built-in C++ data types.
    • C. The Item data type must have a copy constructor and a > operator defined.
    • D. None of the above restrictions apply.

  4. When should a function be implemented as a template function?
    • A. When the data types of the parameters all have copy constructors.
    • B. When the function depends on an underlying data type.
    • C. When the function is relatively short (usually just one line).
    • D. When the function only takes one argument.

  5. What is a major difference between the header file for a toolkit of template functions and the header file for a toolkit of ordinary functions?
    • A. The ordinary function toolkit header file must have a #include for the implementation file, but the template version does not have this #include.
    • B. The template function toolkit header file must have a #include for the implementation file, but the ordinary version does not have this #include.
    • C. The ordinary function toolkit header file must have a macro guard, but the template version does not have this macro guard.
    • D. The template function toolkit header file must have a macro guard, but the ordinary version does not have this macro guard.

  6. Why is it a bad idea to have a size_t parameter for a template function?
    • A. Because it would require stdlib.h to be separately compiled.
    • B. Because most compilers would not permit the actual argument to be 42 (an int).
    • C. Because size_t values cannot be negative.
    • D. Because the compiler can tell the size of something without having a size_t parameter.

    Multiple Choice
    Section 6.2
    Template Classes

  7. Our original bag classes in Chapters 3-5 used a typedef to define the Item data type. What problem is solved by using a template bag class instead of these original typedef versions?
    • A. None of the typedef versions permit a program to use a bag of Strings.
    • B. With all of the typedef versions, it is difficult for a program to use several bags with different Item types.
    • C. With all of the typedef versions, the CAPACITY of the bag is fixed during compilation. A program cannot dynamically allocate a bag.
    • D. With all of the typedef versions, the Item data type must be one of the built-in C++ data types (char, int, etc.)

  8. Suppose bag is a template class, what is the syntax for declaring a bag b of integers?
    • A. bag b;
    • B. bag<int> b;
    • C. bag of int b;
    • D. int bag b;

  9. Why is it recommended to first implement a class using a typedef for the Item type, before implementing the template version?
    • A. It is easier to debug the typedef version.
    • B. The typedef version requires less disk space during the design stage.
    • C. The typedef version requires less memory during execution.
    • D. The template version will not compile unless you implement the typedef version first.

  10. When you write a template class, where does the template prefix occur?
    • A. Before the template class definition
    • B. Before each member function implementation.
    • C. Before any other template functions that manipulate the template class.
    • D. TWO of the above answers are correct.
    • E. All of the (A), (B), and (C) are correct.

    Multiple Choice
    Sections 6.3 and 6.4
    STL Classes, Iterators,
    and the Node Template Class

  11. Suppose that a program wants to use both a bag of doubles and a bag of strings. Which version of the bag could you use?
    • A. You can use the array version of the non-template bag
    • B. You can use the linked list version of the non-template bag
    • C. You can use the array version of the template bag
    • D. Two of the above answers are right
    • E. Answers A, B, and C are all right

  12. What technique is used to provide the capability to step through items of a container class?
    • A. A copy constructor.
    • B. A default constructor.
    • C. A destructor.
    • D. An iterator.
    • E. An overloaded assignment operator.

  13. Why does the new node template class require two versions of the data member function, but the node in Chapter 5 needed only one?
    • A. The Chapter 5 node was singly linked, but the node template class is doubly linked.
    • B. The Chapter 5 data function returned a copy of the data, but the data function for the node template class returns a reference to the data.
    • C. The Chapter 5 node had no iterator.
    • D. All of the above.


Data Structures and Other Objects Using C++

Michael Main (main@colorado.edu)
and
Walter Savitch (wsavitch@ucsd.edu)

Thank you for visiting http://www.cs.colorado.edu/~main/questions/chap06q.html
Copyright © 2000 Addison-Wesley Computer and Engineering Publishing Group