Deck 17: Templates
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/35
Play
Full screen (f)
Deck 17: Templates
1
Templates are an example of algorithm abstraction
True
2
If you define a function template, then the compiler will create a separate function definition for every data type that exists.
False
3
If you have a class template declared and you instantiate it in you program twice both times it is instantiated with an integer), how many versions of the class does the compiler create?
1
4
In a template, all members must be private
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
5
In a template function definition, all parameters must be of the template class T).
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
6
If you need to pass a class template named myClass) function an object of the class as a value parameter, then the type of the parameter is
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
7
Given a class template named listClass, declare a listClass object named myList that can hold doubles.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
8
If you want to make a function a template function, you must precede the function declaration and definition with
A) template
B) template
C) template
D) template
A) template
B) template
C) template
D) template
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
9
If your program defines a class template, then the compiler will generate a class for each different data type for which it is instantiated.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
10
You may not have overloaded friend operators in a class template
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
11
A class template may not use dynamic memory allocation.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
12
In a class template implementation, every use of the class name as the name of the class should be followed by .
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
13
Classes can be defined as templates.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
14
The C++ code
template
is called the ______________________
template
is called the ______________________
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
15
When you define a class as a template, then that class can contain _________ data type.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
16
If you have a class template declared and you instantiate it in you program twice once with an integer, once with a string), how many versions of the class does the compiler create?
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
17
Given the following template function definition, which of the following is not a valid invocation of the function?
Template
Void swapT& left, T& right)
{
//implementation goes here, not relevant to the question
}
Int int1, int2;
Float flt1, flt2;
Char ch1, ch2;
String s1, s2;
A) swaps1,s2);
B) swapint1, int2);
C) swapch1, ch2);
D) swapint1, ch2);
Template
Void swapT& left, T& right)
{
//implementation goes here, not relevant to the question
}
Int int1, int2;
Float flt1, flt2;
Char ch1, ch2;
String s1, s2;
A) swaps1,s2);
B) swapint1, int2);
C) swapch1, ch2);
D) swapint1, ch2);
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
18
Given a class template named listClass, declare a listClass object named myList that can hold strings.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
19
All classes should be converted to templates
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
20
Using template functions is an example of ___________ abstraction.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
21
Which of the following is a correct template prefix?
A) template
B) template
C) class T
D) all of the above
A) template
B) template
C) class T
D) all of the above
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
22
In the following function template, what must be true in order to use the function with a given data type?
Template
Int smallest T array[], int size)
{
Int small=0, i;
Fori=0;i{
Ifarray[i] < array[small])
Small=i;
}
Return small;
}
A) the data type must be a pre-defined data type
B) the data type must have a < operator defined for it
C) the data type must be numeric
D) the data type must be character based
Template
Int smallest T array[], int size)
{
Int small=0, i;
Fori=0;i
Ifarray[i] < array[small])
Small=i;
}
Return small;
}
A) the data type must be a pre-defined data type
B) the data type must have a < operator defined for it
C) the data type must be numeric
D) the data type must be character based
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
23
Given that you have two versions of a function that are the same except that one expects some integer parameters, and the other expects a float and an integer parameter, which parameters would you change to a T in order to make this a template function?
A) The parameter that always stays an integer
B) The parameter that is an integer in one function and a float in the other function.
C) Both parameters change
D) neither parameter changes
A) The parameter that always stays an integer
B) The parameter that is an integer in one function and a float in the other function.
C) Both parameters change
D) neither parameter changes
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
24
When would you want to make a function a function template?
A) when the implementation details of the function are independent of the data types) of the parameters.
B) all functions should be function templates
C) when two different functions have different implementation details
D) only when two functions have the same type of parameters
A) when the implementation details of the function are independent of the data types) of the parameters.
B) all functions should be function templates
C) when two different functions have different implementation details
D) only when two functions have the same type of parameters
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
25
Given the following function template, which of the following are NOT valid calls to larger?
Template
T largerconst T& left, const T& right)
{
Ifleft > right)
Return left;
Else
Return right;
}
A) int x=3, y=4; cout << largerx,y);
B) float x=3, y=4; cout << largerx,y);
C) char x='3', y='4'; cout << largerx,y);
D) char x[]="3", y[]="4"; cout << largerx,y);
Template
T largerconst T& left, const T& right)
{
Ifleft > right)
Return left;
Else
Return right;
}
A) int x=3, y=4; cout << largerx,y);
B) float x=3, y=4; cout << largerx,y);
C) char x='3', y='4'; cout << largerx,y);
D) char x[]="3", y[]="4"; cout << largerx,y);
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
26
Using templates in your program where warranted, is an example of
A) data abstraction
B) polymorphism
C) information hiding
D) algorithm abstraction
A) data abstraction
B) polymorphism
C) information hiding
D) algorithm abstraction
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
27
Given a search template function that will look for an occurrence of target in an array of items, what is necessary for the instantiating data type to implement?
A) the < operator
B) the > operator
C) the = operator
D) the == operator
A) the < operator
B) the > operator
C) the = operator
D) the == operator
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
28
Which would be the correct way to instantiate a containerClass object in your main program?
Template
Class containerClass
{
Public:
ContainerClass);
ContainerClassint newMaxSize);
ContainerClassconst containerClass& source);
~containerClass);
T getItem);
Int getCount);
Int getSize);
Void addItemT item);
Private:
T *container;
Int maxSize, count;
};
A) containerClass myContainer;
B) containerClass myContainer;
C) containerClass myContainer;
D) containerClass myContainer
Template
Class containerClass
{
Public:
ContainerClass);
ContainerClassint newMaxSize);
ContainerClassconst containerClass& source);
~containerClass);
T getItem);
Int getCount);
Int getSize);
Void addItemT item);
Private:
T *container;
Int maxSize, count;
};
A) containerClass myContainer;
B) containerClass
C) containerClass
D) containerClass myContainer
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
29
Why can you not use the swap template function to swap two complete arrays?
Template
Void swapT& left, T& right)
{
T tmp=left;
Left=right;
Right=tmp;
}
A) You can not pass an array to a function
B) The swap function does not return anything
C) the = operator does not work for an array
D) tmp should be an integer
Template
Void swapT& left, T& right)
{
T tmp=left;
Left=right;
Right=tmp;
}
A) You can not pass an array to a function
B) The swap function does not return anything
C) the = operator does not work for an array
D) tmp should be an integer
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
30
If you define some list class template in your program, and then declare a list of integers, 2 lists of doubles and 1 list of strings, how many different version of the template class will the compiler provide?
A) 1
B) 2
C) 3
D) 4
A) 1
B) 2
C) 3
D) 4
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
31
Given a class template, how many different times can you instantiate the class?
A) 0
B) 1
C) 1 for each different data type
D) as many as you need, but only one data type
E) as many as you need, of any data types
A) 0
B) 1
C) 1 for each different data type
D) as many as you need, but only one data type
E) as many as you need, of any data types
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
32
Which of the following describes a class that would be a good candidate for conversion to a template class?
A) A class which defines a new type of array
B) A class which defines rational numbers
C) A class which defines customers for a store
D) All of the above
A) A class which defines a new type of array
B) A class which defines rational numbers
C) A class which defines customers for a store
D) All of the above
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
33
Writing a template class
A) allows us to skip the implementation of that template class
B) allows us to write one class definition that can hold different data types
C) means we never have to write non-template classes again
D) is illegal
A) allows us to skip the implementation of that template class
B) allows us to write one class definition that can hold different data types
C) means we never have to write non-template classes again
D) is illegal
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
34
Give the following class template, what changes need to be made to the default constructor definition?
Template
Class containerClass
{
Public:
ContainerClass);
ContainerClassint newMaxSize);
ContainerClassconst containerClass& source);
~containerClass);
T getItem);
Int getCount);
Int getSize);
Void addItemT item);
Private:
T *bag;
Int maxSize, count;
};
ContainerClass::containerClass)
{
MaxSize = 10;
Bag = new int[maxSize];
Count=0;
}
A) add the template prefix
B) change the type of the dynamic array allocation to T
C) add the following the class name before the scope resolution operator
D) all of the above
E) none of the above
Template
Class containerClass
{
Public:
ContainerClass);
ContainerClassint newMaxSize);
ContainerClassconst containerClass& source);
~containerClass);
T getItem);
Int getCount);
Int getSize);
Void addItemT item);
Private:
T *bag;
Int maxSize, count;
};
ContainerClass::containerClass)
{
MaxSize = 10;
Bag = new int[maxSize];
Count=0;
}
A) add the template prefix
B) change the type of the dynamic array allocation to T
C) add the
D) all of the above
E) none of the above
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
35
Which of the following are valid template prefixes?
A) template
B) template
C) template
D) all of the above
E) none of the above
A) template
B) template
C) template
D) all of the above
E) none of the above
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck