EXAMINATION INSTRUCTIONS

advertisement

Unit Exam #2 CSE116 Introduction to Computer Science II Spring 2016

EXAMINATION INSTRUCTIONS

This examination has 7 pages; check that you have a complete paper.

Each candidate should be prepared to produce, upon request, his or her SUNY/UB card.

This examination has 5 questions. Answer all questions.

You have 60 minutes to complete this examination. Use your time accordingly.

READ AND OBSERVE THE FOLLOWING RULES:

► Names are pre-printed on the exam booklets. Ensure that you have YOUR exam.

► Sign, using your usual signature, in the space provided on the back cover.

► All of your writing must be handed in. This booklet must not be torn or mutilated in any way, and must not be taken from the examination room.

► Show all of your work in arriving at an answer, unless instructed otherwise. Partial credit will be awarded as appropriate.

► Candidates are not permitted to ask questions of the invigilators, except in cases of supposed errors or ambiguities in examination questions.

► CAUTION – Candidates guilty of any of the following, or similar, dishonest practices shall be immediately dismissed from the examination and shall be liable to disciplinary action.

Making use of any books, papers or memoranda, calculators or computers, audio or visual cassette players, or other memory aid devices, other than those explicitly authorised by the examiners.

Speaking or communicating with other candidates.

Purposely exposing written papers to the view of other candidates. The plea of accident or forgetfulness shall not be received.

--------------------------- DO NOT WRITE BELOW THIS LINE! ---------------------------

Q1 Q2 Q3 Q4 Q5 TOTAL %

/10 /10 /10 /10 /10 /50 /100

1

Unit Exam #2 CSE116 Introduction to Computer Science II Spring 2016

Question 1 [10 marks – 2 each]

In the code on the below and on the next page, clearly circle and identify by letter one (and only one) example of each of the items in the list on the left. If you believe no example exists in the given code, write “ no example ” next to that item.

A. inner class definition

B.

C. a type variable

D. anonymous class instantiation – no example

expression of type Throwable

E. argument list package multiset; import java.util.Collection; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; public class MultiSet<E> implements Collection<E> { private E[] _store; private int _size; private long _modCount; public MultiSet() {

_modCount = 0;

_store = (E[]) (new Object[10]);

_size = 0;

_modCount++;

}

@Override public boolean add(E e) {

_store[_size] = e;

_size = _size + 1; if (_size == _store.length) {

E[] temp = (E[]) (new Object[_store.length * 2]); for (int i=0; i<_store.length; i=i+1) {

}

} temp[i] = _store[i];

_store = temp;

_modCount++; return true;

}

@Override public boolean contains(Object obj) { if (obj == null) { for (int i=0; i<_size; i=i+1) { if (obj == _store[i]) {

} else {

}

} return true; for (int i=0; i<_size; i=i+1) {

}

} return false; if (obj.equals(_store[i])) {

} return true;

}

@Override public int size() {

} return _size;

2

Unit Exam #2

}

CSE116 Introduction to Computer Science II

@Override public boolean remove(Object obj) { if (obj == null) { for (int i=0; i<_size; i=i+1) { if (obj == _store[i]) {

_store[i] = _store[_size-1];

_store[_size-1] = null;

_size--;

_modCount++; return true;

}

}

} else { for (int i=0; i<_size; i=i+1) { if (obj.equals(_store[i])) {

_store[i] = _store[_size-1];

}

_store[_size-1] = null;

_size--;

_modCount++; return true;

}

} return false;

}

@Override public boolean isEmpty() { return _size == 0;

}

@Override public Iterator<E> iterator() { return new MultiSetIterator();

} private class MultiSetIterator implements Iterator<E> { private int _index; private long _modCountOfMultiSetAtIteratorCreation; public MultiSetIterator() {

_index = 0;

_modCountOfMultiSetAtIteratorCreation = _modCount;

}

@Override public boolean hasNext() { if (_modCountOfMultiSetAtIteratorCreation != _modCount) { throw new ConcurrentModificationException();

} return _index < _size;

}

@Override public E next() { if (_modCountOfMultiSetAtIteratorCreation != _modCount) { throw new ConcurrentModificationException();

} if (hasNext()) {

E temp = _store[_index];

_index++;

} return temp;

}

}

throw new NoSuchElementException()

;

// rest of code omitted

Spring 2016

3

Unit Exam #2 CSE116 Introduction to Computer Science II

Question 2 [10 marks]

Spring 2016

Consider the following (partial) definition of the Tile class in the current team project. Define the rotateClockwise method.

} package code; import java.util.ArrayList; public class Tile { public static final int N = 0; // North public static final int E = 1; // East public static final int S = 2; // South public static final int W = 3; // West public static final int T = 0; public static final int I = 1; public static final int L = 2; private ArrayList<Boolean> _paths; public Tile(int type) {

_paths = new ArrayList<Boolean>(); switch (type) { case T: _paths.add(true);_paths.add(true);_paths.add(true);_paths.add(false); break; case I: _paths.add(true);_paths.add(false);_paths.add(true);_paths.add(false); break; case L: _paths.add(true);_paths.add(true);_paths.add(false);_paths.add(false); break; default: throw new IllegalArgumentException("Unknown type of tile");

}

} public boolean canMoveTo(Tile that, int direction) {

} return this._paths.get(direction) && that._paths.get((direction+2)%2); public void rotateClockwise() {

// DEFINE THIS METHOD SO THAT WHENEVER CALLED THE TILE IS ROTATED 90 DEGREES

// CLOCKWISE.

_paths.add(N,_paths.remove(W));

}

4

Unit Exam #2 CSE116 Introduction to Computer Science II

Question 3 [10 marks]

Spring 2016

Study the (partial) Queue implementation given below (code not relevant has been omitted): public class Queue<E> { private LRStruct<E> _front; private LRStruct<E> _back; public QueueByCompositionWithLRStruct() {

_front = new LRStruct<E>();

}

_back = _front;

@Override public void enqueue(E item) {

_back.insertFront(item);

}

_back = _back.getRest();

@Override public E dequeue() { if (isEmpty()) { throw new EmptyQueueException("Cannot dequeue an empty queue."); } else {

E item = _front.getDatum();

_front = _front.getRest();

} } } return item;

After the following code has been executed,

Queue<Integer> q = new Queue<>(); q.enqueue(1); q.enqueue(2); the following diagram represents the current state of the Queue.

( GREEN – ADDED/CHANGED , RED – REMOVED ) q

_back

_front

Queue object

_state

LRS object

_rest

_datum LRS object

NonEmptyState object

_state

_rest

_state

_rest

_state

_datum LRS object

EmptyState object

_datum

NonEmptyState object

LRS object

NonEmptyState object an Integer object holding the value

1 an Integer object holding the value

2 an Integer object holding the value

3

Add to and edit the above object diagram as needed so it shows the structure of the Queue after the following operations have been performed: q.dequeue(); q.enqueue(3);

IMPORTANT: assume that no garbage collection occurs (i.e. keep objects that are eligible for garbage collection in the diagram!)

5

Unit Exam #2 CSE116 Introduction to Computer Science II Spring 2016

Question 4 [10 marks]

Recall the ArrayList-based implementation of QUEUE Stack we did as an in-class exercise. Assume the front of the ArrayList is at index 0 and the back is at index size()-1. Answer the following questions:

Did the ENQUEUE push and DEQUEUE pop operations affect the front or the back of the ArrayList? [2 points]

ENQUEUE – back / DEQUEUE – front (or ENQUEUE – front / DEQUEUE – back)

Assuming that no resize of the ArrayList was triggered, what was the efficiency of ENQUEUE push? Did it require time constant or linear in the size of the queue? You MUST justify your answer to receive any credit. [4 points]

If choice was ENQUEUE – back / DEQUEUE – front è constant time (no shifting required)

If choice was ENQUEUE – front / DEQUEUE – back è linear time (existing data must be shifted to make room)

Now suppose we had made the opposite decision about which end of the ArrayList ENQUEUE push and

DEQUEUE pop affected. Again assuming that no resize of the ArrayList was triggered, what was the efficiency of DEQUEUE pop? Did it require time constant or linear in the size of the queue? You MUST justify your answer to receive any credit. [4 points]

Regardless of choice above, if the way the ends of the queue are mapped onto the ArrayList is changed AND we look at dequeue rather than enqueue, then the time will be the same as above

(constant time if choice was ENQUEUE – back / DEQUEUE – front, linear time if choice was ENQUEUE

– front / DEQUEUE – back).

6

Unit Exam #2 CSE116 Introduction to Computer Science II

Question 5 [10 marks]

Spring 2016

Complete the definition of the following LRStruct<String> visitor. When executed on an

LRStruct<String> it must return a HashSet<String> of the Strings from the LRStruct that have length less than or equal to the supplied argument. For example:

LRStruct<String> list = new LRStruct<String>(); list.insertFront("Fred"); list.insertFront("Wilma"); list.insertFront("Betty"); list.insertFront("Wilma"); list.insertFront("Pebbles");

HashSet<String> answer = list.execute(new ExamVisitor(), 5);

System.out.println(answer);

must produce the following output (though order is not important, since we're dealing with a

HashSet):

[Wilma, Betty, Fred]

Complete the definition of the visitor below:

public class ExamVisitor implements IAlgo<HashSet<String>,String,Integer> {

@Override

public HashSet<String> emptyCase(LRStruct<String> host, Integer maxLength) {

return new HashSet<String>();

}

@Override

public HashSet<String> nonEmptyCase(LRStruct<String> host, Integer maxLength) {

HashSet<String> answer; answer = host.getRest().execute(this,maxLength); if (host.getDatum() <= maxLength) { answer.add(host.getDatum());

} return answer;

} }

7

Download