[Solved] SOLVED:Second Midterm Exam Review Problems

30 $

File Name: SOLVED:Second_Midterm_Exam_Review_Problems.zip
File Size: 395.64 KB

SKU: [Solved] SOLVED:Second Midterm Exam Review Problems Category: Tag:

Or Upload Your Assignment Here:


1. The following two lines both generate compiler warnings. What is wrong with them?Stack Stack stack2 = new Stack<String();2. While String is a sub-type of Object (that is, every String object “is a” Object object),it is not true that Bag A “is a” B, then everything you can do with B you can also do with A. What can you dowith a Bag3. What is wrong with the following code fragment? How do you fix it?List

String s;for (s : listOfStrings)System.out.println(s);4. (a) Suppose that it is an Iterator object. Write a small piece of Java code that printsall the objects of it to System.out.(b) Suppose that it is an Iterator object that returns Rectangle objects. Write asmall piece of Java code that inserts all the objects of it into a newly created Stackobject.5. The following code is supposed to fill list3 with every possible sum of elements fromlist1 and list2. So list3 should end up holding{11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43, 51, 52, 53}But the code is incorrect. What is wrong? How could you fix it?List

List

List

for (Iterator

for (Iterator

{int n = it1.next();int m = it2.next();list3.add( n + m );}for (int i : list3)System.out.print(i + ” “);System.out.println();6. Here are three ways we might declare a generic class that holds a pair of references totwo objects of the same type. Suppose that p1, p2, and p3 are references to objects oftype Pair1<T, Pair2<T, and Pair3

be used to make comparisons?class Pair1

class Pair2

class Pair3

7. Suppose we perform the following series of stack operations on a single, initially emptystack:push(5), push(3), pop(), push(2), push(8), pop(), pop(), push(9), push(1), pop(), push(7),push(6), pop(), pop(), push(4), pop(), pop().Draw a picture of the stack at the point where it contains the maximum number ofelements (be sure to indicate the top and bottom of the stack). How many of the aboveoperations had been performed at that point?8. Suppose you have a stack s that contains (1 2 3), with 1 being the top-of-stack, and aqueue q that is empty. Using no other variables and only the push() and pop() stackoperations and the add() and remove() queue operations, show a sequence of operationsthat leave the queue q empty and the stack s with each of the following contents.(a) Leave the stack s with the contents (1 3 2) with 1 as top-of-stack.(b) Leave the stack s with the contents (3 1 2) with 3 as top-of-stack.9. Suppose we implement a stack using a partially-filled array. What is wrong with storingthe top-of-stack at location [0] and the bottom of the stack at the last used position ofthe array?10. If we use a partially-filled array to implement a queue, which is better and why? Havingthe front of the queue at location [0] (and the rear at the last used position of thearray), or having the rear of the queue at location [0] (and the front at the last usedposition of the array).11. If we use a linked list with a head and a tail reference to implement a stack, which isbetter and why? Having the top-of-stack at the head of the linked list, or having thetop-of-stack at the tail of the linked list?12. If we use a linked list with a head and a tail reference to implement a queue, which isbetter and why? Having the front of the queue at the head of the linked list, or havingthe front of the queue at the tail of the linked list?13. Here is an incorrect pseudo code for an algorithm which is supposed to determine whethera String of parentheses is balanced:boolean isBlanced( String input ){declare a character stackwhile ( input has more characters ){read a character from inputif ( the character is a ’(’ )push it on the stackelse if ( the stack is not empty )pop a character off the stackelsereturn false}return true}Give an example of an input string that is made up of only the characters ’(’ and ’)’,is unbalanced, but for which this algorithm will return true. Explain what is wrong withthe algorithm. Can this algorithm ever incorrectly return false when its input string isa balanced string?14. Convert the following expression from postfix to infix notation. Use the minimum num-ber of parentheses needed.6 3 2 4 + – *15. Convert the following expressions from infix to postfix notation.1 + 2 + 3 + 41 + (2 + (3 + 4))1 + (2 + 3) + 42 * 3 * (9 + (3 – 1) + 4) * (5 – 1)16. Using the two stack algorithm for evaluating fully parenthesized infix expressions, drawthe contents of the two stacks just after the ’4’ token has been read from the followinginput string and processed by the algorithm.”( ( ( 2 * 3 ) * ( 9 + ( ( 3 – 1 ) + 4 ) ) ) * ( 5 – 1 ) )”17. A Queue object can be implemented using two Stack objects. Below is an outline ofhow to do this. Complete the implementations of the add() and remove() methods.The key idea is that if you push all the elements from one stack onto an empty stack,then the items get reversed and what was the top-of-stack on the original stack becomesthe bottom of the new stack.In the implementation below, one of the stacks is used to implement the add() methodand the other stack is for the remove() method. It should always be the case that oneof the two stacks is empty and the contents of the queue are in the other stack. Byshifting the elements from one stack to the other, you can move the front of the queueto the top of a stack, or you can move the rear of the queue to the top of a stack.import java.util.Stack;public class Queue<T{private Stack

private Stack

public void add(T item){}public T remove( ){T result = null;return result;}}18. A Stack object can be implemented using one Queue object. Below is an outline of howto do this. Complete the implementations of the push() and pop() methods.Hint: To pop an item from the stack, get all of the items from the queue one at a timeand put them at the rear, except for the last one which you should remove and return.import java.util.Queue;public class Stack<T{private Queue

public void push(T item){}public T pop( ){T result = null;return result;}}12-08-2015 at 01:11 h

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] SOLVED:Second Midterm Exam Review Problems
30 $