Midterm Part 2 Sample Questions
• What is the output of the following code segment?
int a, b, c;
a = 10;
b = 5;
c = a++ / –b;
System.out.println(c);
• Express the running time of the following method using a big-oh notation:
public static int aMethod(int[] arr) {
int n = arr.length, total = 0;
for (int j=0; j < n; j++)
for (int k=0; k <= j; k++)
total += arr[j];
return total;
}
You must justify your answer. If you write only an answer, you will lose points even if it is correct.
• Consider the singly linked list data structure we discussed in the class, an example of which is shown below:
Write a pseudocode of addFirst method, which receives the name of a person and add that person at the head of the list.
• Write the pseudocode of a recursive algorithm that finds the maximum element in an integer array A of n elements.
• A queue data structure can be implemented using an array. The following is a part of a queue class definition that uses an array:
public class ArrayQueue
public static final int CAPACITY = 100;
private E[] data;
private int f = 0; // index of the front element
private int sz = 0; // current number of elements
// constructors
public ArrayQueue() { // an empty queue of size 100 is created}
. . .
}
Write the pseudocode of an enqueue(e) method, where e is the new element to be enqueued. Note that if the queue is full (this occurs when the array is full), the method must return null. If the array is not full but the last slot of the array is already occupied, the new element must be added to the first slot of the array.