0% found this document useful (0 votes)
29 views3 pages

Data Structures Midterm Exam Sample

Uploaded by

omarht2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Data Structures Midterm Exam Sample

Uploaded by

omarht2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Faculty of Information Technology, Data Structures (401212)

Sample Midterm Exam Instructor: Dr. Mohammad Daoud Student ID


Part 1: Select the best answer for each question below. (11 P)
Questions 1-3 refer to this code:

1 Stack s = new LinkedStack();

2 s.push("A"); s.push("B"); s.push("C"); s.push("D"); s.push("E"); s.pop();

3 Object x = s.top();

4 s.pop(); s.pop(); s.push(x);


1. How many members are there in the stack s?
a. 2 b. 3 c. 4 d. 5 e. None of these.
2. What is the second from the top element of the stack s after all this code executes?
a. B b. C c. D d. E e. None of these.
3. What is on the top of the stack s after all this code executes?
a. B b. C c. D d. E e. None of these.
4. Stacks have front and rear
a. True b. False
5. One of the following is a LIFO structure
a. Stack b. Queue c. Array d. none of these
6. One of the following groups are Queue related
a. Push and pop b. enqueue and dequeue c. push, pop, and enlarge d. front, rear, and middle
7. Interfaces are usually used to build the ________ layer
a. logical b. application c. implementation d. GUI
8. Storing information in a linked list structure is a bad idea if we plan to do a lot of deletions.
a. True b. False
9. An abstract class in Java is:
a. an interface
b. class with no fields
c. a class with no methods
d. a class that has an abstract method
e. a class, all of whose methods are abstract
10. The “has-a” relationship between classes is called inheritance
a. True b. false
11. The QueueOverFlowException is potentially thrown by the following queue method(s).
a. enqueue only b. dequeue only c. isEmpty d. the constructors e. enqueue and dequeue
Part 2: Draw Data Structures (8 P)
1. Draw a figure representing our abstract view of the structures created by the following code sequence.
ArrayUnbndQueue<Integer> q = new ArrayUnbndQueue<Integer>(1);
q.enqueue(9);
q.enqueue(8);
q.enqueue(77);
int x= q.dequeue();
q.enqueue(0);

2. Draw a figure representing our abstract view of the structures created by the following code sequence.
ArrayUnbndQueue<Integer> q = new ArrayUnbndQueue<Integer>(10);
q.enqueue(0);
q.enqueue(99);int x= q.dequeue();
q.enqueue(90);
x=q.dequeue();
q.enqueue(0);
3. Draw a figure representing our abstract view of the structures created by the following code sequence.
ArrayStack<ArrayBndQueue<Integer>> s = new ArrayStack<ArrayBndQueue<Integer>>(5);
s.push(new ArrayBndQueue<Integer>());
s.top().enqueue(5);
s.top().enqueue(13);
s.push(new ArrayBndQueue<Integer>());
s.top().enqueue(54);
Object o=s.top().dequeue();
s.push(new ArrayBndQueue<Integer>());
s.top().enqueue(Integer.parseInt(o.toString()));
s.push(s.top());
s.top().enqueue(33);

4. Draw a figure representing our abstract view of the structures created by the following code sequence.
LinkedStack<String[]> ls=new LinkedStack<String[]>();
ls.push(new String[5]);
ls.top()[0]="A";
ls.top()[1]="B";
ls.top()[2]="C";
ls.push(new String[3]);
ls.top()[1]="C";
ls.top()[2]="B";
ls.top()[0]="A";
String[] ss=ls.top();
ls.push(ss);
ls.top()[0]="x";

Part 3: Coding (11 P)


N
5. Create a recursive method that receives a nonnegative integer N and outputs the TriPow(N) Where: TriPow(N) = 3

6. Add the following methods to the ArrayBndQueue (see below code)


i) Shuffling method, the shuffle method will shuffle (randomize the location of) the elements of the queue. Hint: int
x=new Random().nextInt(4); //x can be any number between 0 and 3.
ii) Clear method, that empties the queue
iii) Create a stack: this method creates and returns an ArrayStack for the elements of the queue (first element will
be last and last element will be first)

package ch05.queues;
public class ArrayBndQueue<T> implements BoundedQueueInterface<T>
{
protected final int DEFCAP = 100; // default capacity
protected T[] queue; // array that holds queue elements
protected int numElements = 0; // number of elements n the queue
protected int front = 0; // index of front of queue
protected int rear; // index of rear of queue

public ArrayBndQueue()
{
queue = (T[]) new Object[DEFCAP];

rear = DEFCAP - 1;
}
public ArrayBndQueue(int maxSize)
{
queue = (T[]) new Object[maxSize];

rear = maxSize - 1;
}
public void enqueue(T element)
{
if (isFull())
throw new QueueOverflowException("Enqueue attempted on a full queue.");
else
{
rear = (rear + 1) % queue.length;
queue[rear] = element;
numElements = numElements + 1;
}
}
public T dequeue()
{
if (isEmpty())
throw new QueueUnderflowException("Dequeue attempted on empty queue.");
else
{
T toReturn = queue[front];
queue[front] = null;
front = (front + 1) % queue.length;
numElements = numElements - 1;
return toReturn;
}
}

public boolean isEmpty()


// Returns true if this queue is empty; otherwise, returns false
{
return (numElements == 0);
}

public boolean isFull()


// Returns true if this queue is full; otherwise, returns false.
{
return (numElements == queue.length);
}
}//end of code

Page 1 of 4

You might also like