You are on page 1of 7

Dual Studies Program

Information Technology Department Grade


Data StructureI (1820203)
Mid Term Exam

Duration: One Hour


Student Name:
Reg. #:

Part 1: [24 points]Multiple Choice. Writyour answers in the table below.


1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

Complexity and BigO notation:


1. The time factor when determining the efficiency of algorithm is measured by

Counting microseconds
a. Counting the number of statements
b. Counting the number of key operations
c. Counting the kilobytes of algorithm

2. The worst case occur in linear search on an array when

a. Item is the first element in the array


b. Item is somewhere in the middle of the array
c. Item is not in the array at all
d. Item is the last element in the array or is not there at all

3. If f(n) = n3 - 10n logn + 100 where n is the problem size then

a. f(n) is O(n3)
b. f(n) is O(n2)
c. all the above
d. none of the above

4. The best big-O function that bounds the following function: F(n)= 1 + 2 + ... + n is

a. O(n2)
b. O(n)
c. O(logn)
5. What is the running time for the following code:
for i = 1 to n do begin
for j = i+1 to n do
print "*";
print carriage return
end
a. O(n)
b. O(n2)
c. O(n3)

6. What is the running time for the following code:


i = 64
while (i > 1) do
begin
print i
i = floor(i/2) /* comment: floor truncates to nearest integer */
end
a) O(n)
b) O(1)
c) O(logn)
d) O(nlogn)

Arrays and Linkes Lists:


7. Linked lists are best data structures
a. for relatively permanent collections of data
b. for the size of the structure and the data in the structure are dynamically changing
c. for both of above situation
d. for none of above situation

8. which of the following is NOT true


a. Arrays are dense lists and static data structure
b. data elements in linked list need to be stored in adjacent space in memory
c. linked lists are collection of the nodes that contain information part and next pointer
d. Arrays are best for relatively permanent collections of data

9. Adding an element to an array list at index i requires:


a. Shifting the elements from position i to the end of the array list after adding the new
element.
b. Shifting the elements from position i+1 to the end of the array list after adding the
new element.
c. Shifting the elements from position i+1 to the end of the array list before adding the
new element.
d. Shifting the elements from position i to the end of the array list before adding the new
element.
e.

10. The running time for adding element at position i to an array list is:
a. O(n)
b. O(n2)
c. O(1)

Recursion:

11. Consider the following method:

void superWriteVertical(int number)


// Postcondition: The digits of the number have been written,
// stacked vertically. If number is negative, then a negative
// sign appears on top.
{
if (number < 0)
{
System.out.println("-");
superWriteVertical(-number);
}
else if (number < 10)
System.out.println(number);
else
{
superWriteVertical(number / 10);
System.out.println(number % 10);
}
}

Which call will result in the most recursive calls?

a. super_write_vertical(-1023)
b. super_write_vertical(0)
c. super_write_vertical(100)
d. super_write_vertical(1023)

12. In a real computer, what will happen if you make a recursive call without making the
problem smaller?

a. The run-time stack overflows, halting the program


b. The operating system detects the infinite recursion because of the "repeated
state"
c. The program keeps running until you press Ctrl-C
d. The results are nondeterministic

Part two: [25 points]

Write the code for the following procedures:


a. DeleteNumber(Num):delete the number Num from an array, the array is not sorted.
Hint: We must shift some numbers.

DeleteNumber(Num)
{

For( i=0; i<=array.lenght; i++)


If array[i] == Num{
For( j = i; j< arraySize, j++)
Array[j] = array[j+1];
}

b. AddEmployee(EmployeeName):add a new employee to an array of alphabetically


ordered names . Hint: We must shift some names

AddEmployee(EmployeeName)
{
For( i=0; i<=array.lenght; i++)
If array[i] > studentName{
For( j = arraySize, j>=i ; j--)
Array[j+1] = array[j];
Array[i] = studentName;
}

c. InsertBefore (x, y) that inserts a node to the linked list with element y before the
node containing element x.

LinkedListNode newNode = new LinkedListNode;


newNode.element = y;

Current = head;
prev = head;
While(current != null)
If (current.element = x){

newNode.next = current;
prev = newNode;
}
Prev = current;
Current = current.next;
}
d. countHowMany(x) : counts the number of x occurrences in a linked list.

int boolean countHowMany(int x ){


Entry current = head;
int counter = 0;
while(current != null ){
If(current.element == x)
Counter++;
Current = current.next;
}
return counter;
}
e. hasJava(head): returns true if the list has at least one occurrence of the string “Java”
in the element part of a node. head is the head reference of a linked list. The list might
be empty or it might be non-empty.

boolean hasJava(Entry head){


Entry current = head;
while(current !null ){
If(current.element.equals(“Java”)
Return true;
Current = current.next;
}
return false;
}

You might also like