15 questions: 1 With a single template class, we can implement a container class
ID: 3868442 • Letter: 1
Question
15 questions:
1
With a single template class, we can implement a container class that can be instantiated with a different data type for each object.
True
False
2
When you write a template class, where does the template prefix occur? (Select all that apply)
Before the template class definition
Before each member function implementation.
Before the main function
Before each object declaration.
3
Consider this prototype for a template function:
template <class Item> //line 1
void foo(Item<x>); //line 2
Something is wrong with this prototype. Which line has the error?
line 2
line 1
None of the above. There are no errors in these lines.
4
What is printed by these statements?
int *p1 = new int;
int *p2 = p1;
*p1 = 3;
*p2 = *p1;
cout << p1 << endl;
3
4
Unpredictable
The address of p1
5.
What does the following code block do?
vector<int> a_list;
vector<int>::iterator iter;
iter = min_element(a_list.begin(), a_list.end());
cout << *iter << " ";
This code block declares a vector of integers and an iterator for a vector of integers. The iterator called iter is set to point to the smallest element in the vector and that value is displayed.
This code block declares a vector of iterators. The iterator with the smallest memory location is located and that memory location is displayed.
This code block declares a vector of integers and an iterator for a vector of integers. The iterator called iter is set to point to the smallest element in the vector and that element is removed.
This code block declares a vector of integers and an iterator for a vector of integers. The iterator called iter is set to point to the first element in the vector and that value is displayed.
6
Consider the following main function:
int main()
{
Time t(2, 50);
t.setHour(5);
t.setMinute(20);
cout << "The time is " << t << endl;
}
What is necessary in the Time class to make this main function compile?
None of the above.
A friend function called t is needed.
The << operator must be overloaded for the Time class
The Time class must have the data member t defined in the public area of the class.
7
In which of the following ways can you write an operator overload? (Select all that apply)
in a class member function
in a friend function
in a nonmember function
in the main function
8
Suppose cursor is a pointer that points to a penguin node in a linked list using a penguin class that includes a member function get_link(). You wish to move cursor to point to the next node (to the next penguin) in the list. Which statement do you use?
cursor++;
cursor = cursor->get_link( );
cursor.get_link();
cursor = get_link();
9
Suppose cursor points to a penguin node in a linked list using a penguin class, which includes functions called get_data() and get_link(). What Boolean expression should be true when that penguin node is the ONLY node in the list?
cursor->get_data( ) == 0.0
cursor->get_link( ) == NULL
cursor == NULL
cursor->get_data( ) == NULL
10.
The following steps have been performed for deleting a node from a linked list. The nodes have the member accessor function get_link(). The head_ptr points to the head node of a list.
Step 1: Set up a local variable called remove_ptr and set it equal to head_ptr
Step 2: Set head_ptr equal to the head_ptr->get_link()
Step 3: Delete remove_ptr
Which of the following situations should use these steps?
To delete ALL nodes from a list
To delete a node from the middle (neither front nor end) of a list.
To delete a node from the front of a list
To delete a node from the end of a list
11.
Which operation is better for storing items in an array than storing items on a linked list?
deleting an item at the beginning (position 0).
inserting an item at the beginning (position 0).
Growing the size of the container.
retrieve a copy of the item at location i.
12.
You are working with a node class named node. Suppose that f is a non-member function with a prototype like this with a missing data type in the parameter list:
void f(________ head_ptr);
// Precondition: head_ptr is a head pointer for a linked list.
// Postcondition: The function f has done some computation with
// the linked list, but the list itself is unchanged.
What is the best data type for head_ptr in the parameter list of this function?
node
node* &
node*
node&
13.
A virtual function provides you with the ability to have several different functions with the same name in a parent and children classes and have C++ figure out which one to call at run-time.
True
False
14.
Suppose cursor points to a node in a linked list (using the node definition with member functions called get_data and get_link). What Boolean expression will be true when cursor points to the tail node (last node) of the list?
cursor == NULL
None of the above
cursor->get_link( ) == NULL
cursor->get_data( ) == NULL
15.
You wish to convert a container class to a template container class. Which of the following should you consider?
Make sure operations on container data are not type specific. For example, don't use string functions on container data.
Make the class visible to the client. One way to do this is to include the main function in the header file.
Make implementation visible. One way to do this is to include both interface and implementation in the header file.
Make sure all return statements that return container data only return container data. If a return statement returns container data under certain conditions, but another return statement returns something that is not container data under other conditions, there could be a data type conflict when a different data type is used for this template.
Explanation / Answer
Please try to post your questions into parts, I can only answer the first four, however I will answer the first five. Here goes:
1. True
It is very much the case with vectors, with template class we have defined it as a container and can be used with different data type like vector <int> or vector <string>.
2. Before the template class definition & Before each member function implementation
The keyword is used immediately before the prototype as well as before any implementation of a template funtion.
3. line 2
The correct format would be:
template <class Item>
void foo(Item x);
4. The address of p1
As it is clearly seen that p1 holds an address and cout prints it.
5. This code block declares a vector of integers and an iterator for a vector of integers. The iterator called iter is set to point to the smallest element in the vector and that value is displayed.
min_element just selects the minimum i.e. smallest element from the starting index of the vector to the ending one.
Do comment for further doubts and queries !
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.