
Test 1 (Supplementary)
Data Structure
CS-240
Fall 2004
Answer only 5 questions from below
1. Describe an ADT Priority_Queue which is like a queue we are all familiar with but has additional
structures in it. For instance, each object queuing in it would have a priority value (an integer)
which we assume to be a constant. The queue discipline will not be FIFO (or FCFS) but it would
be priority-driven; the object with highest priority gets out first from such a queue. In your ADT
identification, include at least 5 methods that could be called in a public mode.
2. Consider the following code:
static void pattern(int level) {
if (level == 0) {
System.out.print("*");
}
else {
System.out.print("[");
pattern(level - 1);
System.out.print(",");
pattern(level - 1);
System.out.println("]");
}
}
What would be the output for the following calls: (a) pattern(0), (b) pattern(1),
(c) pattern(2), and (d) pattern(3)
3. We are interested in estimating computational time of a program code. We assume that every
assignment, every arithmetic operation, every logical comparison consumes on an average one
unit of CPU time. Given this, compute the minimum time required to run the following code on a
machine with a single CPU. We assume that the variable n is known in the program.
...
int sum=0;
for (int i=1; i<n; i=2*i)
{
System.out.print(i+" ");
sum +=i;
if (i%2 == 0){
sum=sum*3;
sum=sum-1;
}
}
System.out.println();
System.out.println(sum);
…
Furthermore, indicate how the result will change if the for loop is changed to
for (int i=0; i<n; i=2*i)