Exam Paper

Only available on StudyMode
  • Topic: Stack, Abstract data types, Double-ended queue
  • Pages : 5 (881 words )
  • Download(s) : 148
  • Published : January 21, 2013
Open Document
Text Preview
Université d’Ottawa
Faculté de génie École d’ingénierie et de technologie de l’information

University of Ottawa

Faculty of Engineering

School of Information Technology and Engineering

Introduction to Computer Science II (ITI 1121) Final Examination: Solutions Instructor: Marcel Turcotte April 2008, duration: 3 hours

Identification
Student name: Student number: Signature:

Instructions
1. 2. 3. 4. 5. 6. This is a closed book examination; No calculators or other aids are permitted; Write comments and assumptions to get partial points; Beware, poor hand writing can affect grades; Do not remove the staple holding the examination pages together; Write your answers in the space provided. Use the backs of pages if necessary. There are two blank pages at the end. You may not hand in additional pages.

Marking scheme
Question 1 2 3 4 5 6 7 8 Total Points 10 15 15 10 5 10 15 10 90 Score

April 2008

ITI 1121

Page 2 of 25

Question 1

(10 points)

For this question, the classes Queue and Stack both implement the interface LinearCollection. «interface» LinearCollection +add(E item):void +remove():E +isEmpty():boolean

Queue

Stack

Queue, as all the other implementations of a queue, is such that the method add enqueues the item at the rear of the queue, the method remove dequeues the front element, and the method isEmpty returns true if this queue contains no elements. Stack, as all the other implementations of a stack, is such that the method add pushes the item onto the top of the stack, the method remove pops (removes and returns) the top element, and the method isEmpty returns true if this stack contains no elements. A BinarySearchTree was created by adding elements in the order that follows; the resulting tree is shown to the right. BinarySearchTree t; t = new BinarySearchTree(); t.add( t.add( t.add( t.add( t.add( t.add( t.add( t.add( t.add( 3 1 7 9 5 4 6 2 8 ); ); ); ); ); ); ); ); );

3 1 7

2

5

9

4

6

8

A. Circle the answer that corresponds to the following method call: t.traverse( new Queue< Node >() ); A. B. C. D. E. F. G. H. I. 12345678 21465897 23455897 3172594 31275469 37195286 37985641 86495271 98765432 9 3 3 68 8 4 2 3 1

B. Circle the answer that corresponds to the following method call: t.traverse( new Stack< Node >() ); A. B. C. D. E. F. G. H. I. 12345678 21465897 23455897 31725946 31275469 37195286 3798564 86495271 98765432 9 3 3 8 8 4 12 3 1

The source code for the method traverse can be found on the next page.

April 2008

ITI 1121

Page 3 of 25

public class BinarySearchTree< E extends Comparable > { private static class Node { private T value; private Node left = null; private Node right = null; private Node( T value ) { this.value = value; } } private Node root = null; public void traverse( LinearCollection< Node > store ) { if ( root != null ) { store.add( root ); while ( ! store.isEmpty() ) { Node current = store.remove(); System.out.print( " " + current.value ); if ( current.left != null ) { store.add( current.left ); } if ( current.right != null ) { store.add( current.right ); } } System.out.println(); } } public boolean add( E obj ) { ... } }

April 2008

ITI 1121

Page 4 of 25

Question 2

(15 points)

The abstract data type Deque — pronounced “deck” — combines features of both a queue and a stack. In particular, a Deque (“Double-Ended QUEue”) allows for • efficient insertions at the front or rear; • efficient deletions at the front or the rear. Below, you will find a complete implementation of the class Deque that uses a circular array to store its elements, and has an instance variable, size, to keep track of the number of elements. Here are the descriptions of the four main methods of this class. boolean offerFirst( E item ): adds an item at the front of this Deque, returns true if the item was successfully added; boolean offerLast( E item ): adds an item at the rear of this Deque, returns true if the item was sucessfully...
tracking img