• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/40

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

40 Cards in this Set

  • Front
  • Back

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? (Ch6)

With all of the typedef versions, it is difficult for a program to use several bags with different Item types.

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

Because most compilers would not permit the actual argument to be 42 (an int).

What technique is used to provide the capability to step through items of a container class?

An iterator.

Which of the following stack operations could result in stack underflow?

pop

declare a stack of characters while ( there are more characters in the word to read ) { read a character push the character on the stack } while ( the stack is not empty ) { write the stack's top character to the screen pop a character off the stack } What is written to the screen for the input "carpets"?

steprac

What is the value of the postfix expression 6 3 2 4 + - *:

Something between -15 and -100

Which of the following applications may use a stack?

All of the above

Suppose that p is a pointer to a node in a linked list of strings, and the data of that node is the string “help”. What is the value of that data after these two statements:p->data( ).erase(3,1);p->data( ).append("lo");

hello

Which is the best description of unification?

Unification is the compiler’s term for determining how to instantiate a template function through the template parameter. If a template parameter does not appear in the parameter list of a template function, the compiler will generate the message “Failed unification.”

The bag implementation(Section 6.2) uses the copy function from the facility. Why do we need to write the full name std::copy in the implementation of the bag template, but we write the shorter version, copy, in the implementation of the ordinary class?


The implementation file is included in the header file, and you should never put a using directive in a header file. Therefore, our new bag implementation file does not have a using directive for std, and we must write the full name std::copy.

Suppose you have two iterators, s and t, over the same container, and both *s and *t are 42. Will (s==t) always be true?

Not necessarily. In order for (s==t) the two iterators must be in the exact same spot in the container.

Which of the description of the operator * function of Node Iterator in section 6.5 is more appropriate?

It returns the actual item in the node.

If we have a local variable called insert_ptr, which is a pointer to a node as following.node *insert_ptr;How will you declare this variable in a template version of the function?

node *insert_ptr;


Suppose that p is a pointer to a const node in a linked list of strings, and the data of that node is the string “help”. What would happen when encounter these two statements?p->data( ).erase(3,1);p->data( ).append("lo");

If p is a pointer to a const node, then the activation of p->data( ) will use the const version of data, and the erase or append functions will not be allowed.

Which of the following description of the evaluate_stack_tops function in section 7.2 is the most appropriate?

Before come to this function, the top of the operations stack contains +, -, *, or /, and the numbers stack contains at least two numbers.At this functiont the top two numbers have been popped from the numbers stack, and the top operation has been popped from the operations stack. The two numbers have been combined using the operation (with the second number popped as the left operand). The result of the operation has then been pushed back onto the numbers stack.

If the size function for the linked-list version of thestack.function is not constant time, what would be an approach that is constant time?

It is not a constant time. For a constant time implementation, we could maintain another private member variable to continually keep track of the list length. This variable would be updated each time an item is pushed or popped, and the size function can simply return the current value of this variable.

Suppose a program uses a stack of characters to read in a word and then write the word out backward. Now supposethe input word is DAHL. Which of the following are all the activations of the push, top, and popfunctions that will generate LHAD?

Push a 'D'; push an 'A'; push an 'H'; push an 'L'; top and pop for 'L'; top and pop for 'H'; top and pop for 'A'; top and pop for 'D'

Which on of the following is a simple way to reimplement the array version of the stack without the need for a CAPACITY constant

Use a dynamic array.

What statement of the following is the most appropriate?

An advantage of a linked-list implementation of a stack over an array implementation is that with the linked list there is no preset limit on the number of entries you can add to the stack.

Which of the functions are used in in the Calculator Program (section 7.2)?


a. isdigit function (from the facility )


b. strchr function (from )


c. evaluate_stack_tops function


d. add_values function


e. operator * function


f. operator ++ function


a, b, and c

Consider this prototype for a template function: template void foo(Item x); Which is the right way to call the foo function with an integer argument i?


foo( i );

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?

The template function toolkit header file must have a #include for the implementation file, but the ordinary version does not have this #include.

When you write a template class, where does the template prefix occur?

All of the (A), (B), and (C) are correct.

Here is an INCORRECT pseudocode for the algorithm which is supposed to determine whether a sequence of parentheses is balanced: declare a character stack while ( more input is available) { read a character if ( the character is a '(' ) push it on the stack else if ( the character is a ')' and the stack is not empty ) pop a character off the stack else print "unbalanced" and exit } print "balanced" Which of these unbalanced sequences does the above code think is balanced?

((())

In the linked-list version of the stack class, which operations require linear time for their worst-case behavior?

None of these operations require linear time.

Consider the implementation of the stack using a partially-filled array. What goes wrong if we try to store the top of the stack at location [0] and the bottom of the stack at the last used position of the array?

Both push and pop would require linear time.

In the array version of the stack class (with a fixed-sized array), which operations require linear time for their worst-case behavior?

None of these operations require linear time.

What would be the best statement about when does instantiation of a template parameter occur?

Instantiation of a template parameter occurs when an actual variable of the template class is declared.

Within the definition of the bag template class, can we write bag on its own, or do we need bag?


Within the bag’s template class, bag is correct on its own.

What is the template prefix?

template

What is the best description regarding the advantage of the template function approach?

A template function allows several different usages in a single program because the compiler determines the data type when the template function is used.

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

Because many compilers, an int argument does not provide an exact match to size_t.

When you write a template class, where does the template prefix occur?

For a template class, the template prefix occurs before the template class definition and before each member function implementation.It also must appear before the prototype and implementation of any other template functions that manipulate the template class.

What is the advantage to use the typedef to define a data type in a program?

The typedef approach has a simpler syntax, which makes for easier debugging.

What is the stack top operation?

The stack top operation reads and discards the next character from the input stream.A stack processes characters in the LIFO fashion.

What statement of the following is the most appropriate?

The accessible end of the stack is called the top. Adding an entry to a stack is called a push operation.

Which of the following description of the top function in section 7.3 is the most appropriate?

Before come to ths function: size( ) > 0.At this function The return value is the top item of the stack, but the stack is unchanged.

What is the istream ignore operation?

The ignore operation reads and discards the last character from the input stream. An istream processes characters in the order of LIFO fashion

What does the following whatfor function do? (stack in section 7.3)template Item stack::whatfor( ) const// Libraries used: cassert{ assert(size( ) >= 2); return top_ptr->link( )->data( );}


It is a constant member function(linked-list version) that returns the second element from the top of the stack without actually changing the stack.

What is the typical implementation of an STL stack?

The STL stack is typically implemented using a dynamic array