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 );
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.
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.
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.
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
|
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.)
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;
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.
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
|
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
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.
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.