Deck 17: Templates

Full screen (f)
exit full mode
Question
In a template,all members must be private
Use Space or
up arrow
down arrow
to flip the card.
Question
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
Question
Using template functions is an example of ___________ abstraction.
Question
Given a class template named listClass,declare a listClass object named myList that can hold doubles.
Question
All classes should be converted to templates
Question
In a template function definition,all parameters must be of the template class T).
Question
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
Question
Templates are an example of algorithm abstraction
Question
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);
Question
Which of the following are valid template prefixes?

A)template
B)template
C)template
D)all of the above
E)none of the above
Question
In a class template implementation,every use of the class name as the name of the class should be followed by .
Question
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
Question
When you define a class as a template,then that class can contain _________ data type.
Question
You may not have overloaded friend operators in a class template
Question
Given a class template named listClass,declare a listClass object named myList that can hold strings.
Question
If you define a function template,then the compiler will create a separate function definition for every data type that exists.
Question
Classes can be defined as templates.
Question
If your program defines a class template,then the compiler will generate a class for each different data type for which it is instantiated.
Question
The C++ code
template
is called the ______________________
Question
A class template may not use dynamic memory allocation.
Question
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
Question
Which of the following is a correct template prefix?

A)template
B)template
C)
Question
Using templates in your program where warranted,is an example of

A)data abstraction
B)polymorphism
C)information hiding
D)algorithm abstraction
Question
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
Question
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
Question
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
Question
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
Question
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
Question
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
Question
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
Question
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);
Question
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
Question
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
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/33
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 17: Templates
1
In a template,all members must be private
False
2
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
B
3
Using template functions is an example of ___________ abstraction.
Algorithm
4
Given a class template named listClass,declare a listClass object named myList that can hold doubles.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
5
All classes should be converted to templates
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
6
In a template function definition,all parameters must be of the template class T).
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
7
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
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
8
Templates are an example of algorithm abstraction
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
9
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);
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
10
Which of the following are valid template prefixes?

A)template
B)template
C)template
D)all of the above
E)none of the above
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
11
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 33 flashcards in this deck.
Unlock Deck
k this deck
12
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 33 flashcards in this deck.
Unlock Deck
k this deck
13
When you define a class as a template,then that class can contain _________ data type.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
14
You may not have overloaded friend operators in a class template
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
15
Given a class template named listClass,declare a listClass object named myList that can hold strings.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
16
If you define a function template,then the compiler will create a separate function definition for every data type that exists.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
17
Classes can be defined as templates.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
18
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 33 flashcards in this deck.
Unlock Deck
k this deck
19
The C++ code
template
is called the ______________________
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
20
A class template may not use dynamic memory allocation.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
21
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
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
22
Which of the following is a correct template prefix?

A)template
B)template
C)
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
23
Using templates in your program where warranted,is an example of

A)data abstraction
B)polymorphism
C)information hiding
D)algorithm abstraction
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
24
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
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
25
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
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
26
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
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
27
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
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
28
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
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
29
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
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
30
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
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
31
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);
Unlock Deck
Unlock for access to all 33 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
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
33
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
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 33 flashcards in this deck.