A) Consider a Stack of Integers Cstack. Using Only the Stack Methods Isempty(), Pop

Total Page:16

File Type:pdf, Size:1020Kb

A) Consider a Stack of Integers Cstack. Using Only the Stack Methods Isempty(), Pop

(a) Consider a stack of integers cStack. Using only the stack methods isEmpty(), pop(), pop(int x), and push(int y), write code (in Java or pseudocode) which pops all the elements off cStack and returns the sum of all these elements. (b) How would the code above have to be modified if the stack cStack is to be left unchanged after the sum is found? (In your solution, still only use the stack public methods listed above.)

1. Write a recursive method that has one parameter which is an int value called x. The method prints x asterisks, followed by x exclamation points. Do NOT use any loops. Do NOT use any variables other than x. 3. A queue aQueue of strings contains the following words (separated by commas, front to back): car, house, pig, car, hill What is the status of the queue each of the following actions is taken insuccession? (a) aQueue.dequeue(st) Answer: (b) aQueue.enqueue("nail") Answer: (c) aQueue.getFront(st) Answer: (d) aQueue.enqueue(st) Answer: (e) aQueue.dequeue() Answer: 4. A stack bStack contains the following items 7 8 3 14 5 What is the output to the screen of the following code? int x; while (!bStack.isEmpty()){ bStack.pop(x); if (x>0 && !bStack.isEmpty()) bStack.pop(); cout << x << endl; }

Multiple Choice

Multiple Choice Sections 7.1-7.2 Queues and Their Applications One difference between a queue and a stack is: A. Queues require linked lists, but stacks do not. B. Stacks require linked lists, but queues do not. C. Queues use two ends of the structure; stacks use only one. D. Stacks use two ends of the structure, queues use only one.

If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed? A. ABCD B. ABDC C. DCAB D. DCBA

Which of the following expressions evaluates to true with approximate probability equal to P? (P is double and 0 <= P <= 1). A. Math.random() < P B. Math.random() > P C. Math.random() < P * 100 D. Math.random() > P * 100 Multiple Choice Section 7.3 Implementations of the Queue ADT

Suppose we have a circular array implementation of the queue class, with ten items in the queue stored at data[2] through data[11]. The current capacity is 42. Where does the insert method place the new entry in the array? A. data[1] B. data[2] C. data[11] D. data[12]

Consider the implementation of the Queue using a circular array. What goes wrong if we try to keep all the items at the front of a partially-filled array (so that data[0] is always the front). A. The constructor would require linear time. B. The getFront method would require linear time. C. The insert method would require linear time. D. The isEmpty method would require linear time.

In the linked list implementation of the queue class, where does the insert method place the new entry on the linked list? A. At the head B. At the tail C. After all other entries that are greater than the new entry. D. After all other entries that are smaller than the new entry.

In the circular array version of the Queue class, which operations require linear time for their worst- case behavior? A. getFront B. insert when the capacity has not yet been reached C. isEmpty D. None of these operations require linear time.

In the linked-list version of the Queue class, which operations require linear time for their worst-case behavior? A. getFront B. insert C. isEmpty D. None of these operations require linear time.

If data is a circular array of CAPACITY elements, and rear is an index into that array, what is the formula for the index after rear? A. (rear % 1) + CAPACITY B. rear % (1 + CAPACITY) C. (rear + 1) % CAPACITY D. rear + (1 % CAPACITY)

I have implemented the queue with a circular array, keeping track of front, rear, and manyItems (the number of items in the array). Suppose front is zero, and rear is one less than the current capacity. What can you tell me about manyItems? A. manyItems must be zero. B. manyItems must be equal to the current capacity. C. count could be zero or the capacity, but no other values could occur. D. None of the above.

I have implemented the queue with a linked list, keeping track of a front node and a rear node with two reference variables. Which of these reference variables will change during an insertion into a NONEMPTY queue? A. Neither changes B. Only front changes. C. Only rear changes. D. Both change.

I have implemented the queue with a linked list, keeping track of a front node and a rear node with two reference variables. Which of these reference variables will change during an insertion into an EMPTY queue? A. Neither changes B. Only front changes. C. Only rear changes. D. Both change. Multiple Choice Section 7.4 Priority Queues

Suppose getFront is called on a priority queue that has exactly two entries with equal priority. How is the return value of getFront selected? A. One is chosen at random. B. The one which was inserted first. C. The one which was inserted most recently. D. This can never happen (violates the precondition)

An array of queues can be used to implement a priority queue, with each possible priority corresponding to its own element in the array. When is this implementation not feasible? A. When the number of possible priorities is huge. B. When the number of possible priorities is small. C. When the queues are implemented using a linked list. D. When the queues are implemented with circular arrays. Multiple Choice

Multiple Choice Section 8.1 Recursive Methods

In a single method declaration, what is the maximum number of statements that may be recursive calls? A. 1 B. 2 C. n (where n is the argument) D. There is no fixed maximum

What is the maximum depth of recursive calls a method may make? A. 1 B. 2 C. n (where n is the argument) D. There is no fixed maximum

Consider the following method:

void superWriteVertical(int number)

// Postcondition: The digits of the number have been written,

// stacked vertically. If number is negative, then a negative

// sign appears on top.

{

if (number < 0)

{

System.out.println("-");

superWriteVertical(-number);

}

else if (number < 10)

System.out.println(number);

else {

superWriteVertical(number / 10);

System.out.println(number % 10);

}

}

What values of number are directly handled by the stopping case? A. number < 0 B. number < 10 C. number >= 0 && number < 10 D. number > 10

Consider the following method:

void superWriteVertical(int number)

// Postcondition: The digits of the number have been written,

// stacked vertically. If number is negative, then a negative

// sign appears on top.

{

if (number < 0)

{

System.out.println("-");

superWriteVertical(-number);

}

else if (number < 10)

System.out.println(number);

else

{ superWriteVertical(number / 10);

System.out.println(number % 10);

}

}

Which call will result in the most recursive calls? A. super_write_vertical(-1023) B. super_write_vertical(0) C. super_write_vertical(100) D. super_write_vertical(1023)

Consider this method declaration:

void quiz(int i)

{

if (i > 1)

{

quiz(i / 2);

quiz(i / 2);

}

System.out.print("*");

}

How many asterisks are printed by the method call quiz(5)? A. 3 B. 4 C. 7 D. 8 E. Some other number

This question is appropriate if you studied a flood fill (which was not part of the text). Suppose that a flood fill starts at the point marked with an o in this diagram:

XXXXXXXXXX XX XXXX This is row number 1

XX XX XXXX This is row number 2

XX o XXX This is row number 3

XXXXXXXXXX

Suppose that the first recursive call is always left; the second recursive call is always right; the third recursive call is always up; the fourth recursive call is always down. How will the rows be completely filled? A. Row 1 is filled first, then row 2, then row 3 B. Row 1 is filled first, then row 3, then row 2 C. Row 2 is filled first, then row 3, then row 1 D. Row 3 is filled first, then row 1, then row 2 E. Row 3 is filled first, then row 2, then row 1

In a real computer, what will happen if you make a recursive call without making the problem smaller? A. The operating system detects the infinite recursion because of the "repeated state" B. The program keeps running until you press Ctrl-C C. The results are nondeterministic D. The run-time stack overflows, halting the program

When the compiler compiles your program, how is a recursive call treated differently than a non- recursive method call? A. Primitive values are all treated as reference variables B. Reference variables are all treated as primitive values C. There is no duplication of local variables D. None of the above

When a method call is executed, which information is not saved in the activation record? A. Current depth of recursion. B. Formal parameters. C. Location where the method should return when done. D. Local variables.

Consider the following method:

public static void test_a(int n)

{

System.out.println(n + " ");

if (n>0) test_a(n-2);

}

What is printed by the call test_a(4)? A. 0 2 4 B. 0 2 C. 2 4 D. 4 2 E. 4 2 0

Consider the following method:

public static void test_b(int n)

{

if (n>0)

test_b(n-2);

System.out.println(n + " ");

}

What is printed by the call test_b(4)? A. 0 2 4 B. 0 2 C. 2 4 D. 4 2 E. 4 2 0 Multiple Choice Section 8.2 Fractals and Mazes

Suppose you are exploring a rectangular maze containing 10 rows and 20 columns. What is the maximum number of calls to traverse_maze that can be generated if you start at the entrance and call traverse_maze? A. 10 B. 20 C. 30 D. 200

Suppose you are exploring a rectangular maze containing 10 rows and 20 columns. What is the maximum depth of recursion that can result if you start at the entrance and call traverse_maze? A. 10 B. 20 C. 30 D. 200

What is the relationship between the maximum depth of recursion for traverse_maze and the length of an actual path found from the entrance to the tapestry? A. The maximum depth is always less than or equal to the path length. B. The maximum depth is always equal to the path length. C. The maximum depth is always greater than or equal to the path length. D. None of the above relationships are always true. Multiple Choice Section 8.3 Reasoning about Recursion

What would a suitable variant expression be for the traverse_maze method which could be used to prove termination? Assume N is the number of rows in the maze and M is the number of columns. A. N + M B. N + M + 1 C. N * M D. The number of unexplored spaces in the maze.

What technique is often used to prove the correctness of a recursive method? A. Communitivity. B. Diagonalization. C. Mathematical induction. D. Matrix Multiplication.

Consider the usual algorithm to convert an infix expression to a postfix expression. Suppose that you have read 10 input characters during a conversion and that the stack now contains these symbols:

| | | + | | ( | bottom |___*___| Now, suppose that you read and process the 11th symbol of the input. Draw the stack for the case where the 11th symbol is: A. A number:

B. A left parenthesis:

C. A right parenthesis:

D. A minus sign:

E. A division sign: Multiple Choice

Multiple Choice Sections 6.1-6.2 Stacks and Their Applications Entries in a stack are "ordered". What is the meaning of this statement? A. A collection of Stacks can be sorted. B. Stack entries may be compared with the '<' operation. C. The entries must be stored in a linked list. D. There is a first entry, a second entry, and so on.

The operation for adding an entry to a stack is traditionally called: A. add B. append C. insert D. push

The operation for removing an entry from a stack is traditionally called: A. delete B. peek C. pop D. remove

Which of the following stack operations could result in stack underflow? A. is_empty B. pop C. push D. Two or more of the above answers

Which of the following applications may use a stack? A. A parentheses balancing program. B. Keeping track of local variables at run time. C. Syntax analyzer for a compiler. D. All of the above.

Consider the following pseudocode:

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 ) { pop a character off the stack write the character to the screen } What is written to the screen for the input "carpets"? A. serc B. carpets C. steprac D. ccaarrppeettss

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? A. ((()) B. ())(() C. (()())) D. (()))()

Consider the usual algorithm for determining whether a sequence of parentheses is balanced. What is the maximum number of parentheses that will appear on the stack AT ANY ONE TIME when the algorithm analyzes: (()(())(()))? A. 1 B. 2 C. 3 D. 4 E. 5 or more

Consider the usual algorithm for determining whether a sequence of parentheses is balanced. Suppose that you run the algorithm on a sequence that contains 2 left parentheses and 3 right parentheses (in some order). What is the maximum number of parentheses that will ever appear on the stack AT ONE TIME during the computation? A. 1 B. 2 C. 3 D. 4 E. 5 or more Multiple Choice Section 6.3 Implementations of the Stack ADT

Suppose we have an array implementation of the stack class, with ten items in the stack stored at data[0] through data[9]. The CAPACITY is 42. Where does the push method place the new entry in the array? A. data[0] B. data[1] C. data[9] D. data[10]

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? A. Both peek and pop would require linear time. B. Both push and pop would require linear time. C. The Stack could not be used to check balanced parentheses. D. The Stack could not be used to evaluate postfix expressions.

In the linked list implementation of the stack class, where does the push method place the new entry on the linked list? A. At the head B. At the tail C. After all other entries that are greater than the new entry. D. After all other entries that are smaller than the new entry.

In the array version of the Stack class, which operations require linear time for their worst-case behavior? A. is_empty B. peek C. pop D. push when the stack is below capacity E. None of these operations require linear time.

In the linked-list version of the Stack class, which operations require linear time for their worst-case behavior? A. is_empty B. peek C. pop D. push E. None of these operations require linear time. Multiple Choice Section 6.4 More Complex Stack Applications What is the value of the postfix expression 6 3 2 4 + - *: A. Something between -15 and -100 B. Something between -5 and -15 C. Something between 5 and -5 D. Something between 5 and 15 E. Something between 15 and 100

Here is an infix expression: 4+3*(6*3-12). Suppose that we are using the usual Stack algorithm to convert the expression from infix to postfix notation. What is the maximum number of symbols that will appear on the stack AT ONE TIME during the conversion of this expression? A. 1 B. 2 C. 3 D. 4 E. 5

Which of these is the correct big-O expression for 1+2+3+...+n? A. O(log n) B. O(n) C. O(n log n) D. O(n²)

Which of the following formulas in big-O notation best represent the expression n²+35n+6? A. O(n³) B. O(n²) C. O(n) D. O(42)

Here is some code for an integer variable n: while (n > 0) { n = n/10; // Use integer division } What is the worst-case time analysis for the above loop? A. O(1) B. O(log n) C. O(n) D. O(n²)

Express the formula (n - 2)*(n - 4) using big-O notation: A. O(1) B. O(8) C. O(log n) D. O(n) E. None of the above Solutions to selected problems 3. (a) aQueue.dequeue(st) Answer: house, pig, car, hill (b) aQueue.enqueue("nail") Answer: house, pig, car, hill, nail (c) aQueue.getFront(st) Answer: house, pig, car, hill, nail (d) aQueue.enqueue(st) Answer: house, pig, car, hill, nail, house (e) aQueue.dequeue() Answer: pig, car, hill, nail, house 4. 7 -3 14 5. (a) int sum = 0; while (!cStack.isEmpty()){ int x; cStack.pop(x); sum += x; } (b) In one way to solve this problem, we must implement a temporary stack to save all the items in cStack as they are popped o . Then push all the saved items back onto cStack. The modified code would then be int sum = 0; Stack tempStack; while (!cStack.isEmpty()){ int x; cStack.pop(x); sum += x; tempStack.push(x); } while (!tempStack.isEmpty()){ int x; tempStack.pop(x); cStack.push(x); }

Recommended publications