CSI247
CSI247
Final Examination
Course Code: CSI 247 Subject: COMPUTER SCIENCE
Title of Paper: DATA STRUCTURES
Semester: 1
Time Allowed: 180 MINUTES Reading Time:10 MINUTES Maximum Marks: 100
Student’s Surname _______________________ Firstname ___________________________
Student’s ID _______________
Instructions:
a) No aids of any kind allowed.
b) Answer ALL questions.
c) For section A (multiple choice) write your answers in the front page.
d) Section B & C: Answers must be written in the provided space on the question and
use the back of the page for rough work.
e) READ QUESTIONS CAREFULLY and write legibly.
f) Do not include comments in your programs.
Question Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10
Mark: / 10
Answer
/10
Section B: Short Answers
Section C: Programming
Question Q1 Q2 Mark: /50
Mark /40 /10
/45
Total: 100
Page 1 of 10
Section A: Multiple choice [10 marks]
1. When does a normal queue that is implemented using an array of size 10 get full?
A. When tail = 10
B. When tail = 9
C. When front = tail + 1
D. When tail = front
A. class C implements A, B
B. class C implements A, implements B
C. class C implements A extends B
D. class C implements A & B
A. 0
B. 1
C. Compilation Error
D. Runtime Error
4. What is the worst case Big-O notation of the following code segment?
for ( int i=0; i < n; i++)
System.out.println(i);
for ( int j = 0; j < n; j++)
for ( int k = 0; k < n ; k++)
System.out.println(k);
A. O(1)
B. O(n)
C. O(n2)
D. O(log n)
Page 2 of 10
5. What is the effect of implementing class A constructor private?
class Testing{
public static void main(String args[]) {
Box b1 = new Box();
Box b2 = new Box();
b1.h = 5;
b1.l = 2;
b1.w = 4;
b2.l = b2.h = 9;
b2 = b1;
System.out.println(b2.h);
}
}
A. 2
B. 4
C. 5
D. 9
A. In an array
B. In a structure
C. In contiguous locations in memory
D. Anywhere in memory where the computer has space for them
interface A {
void a();
}
abstract class B implements A {
abstract void b();
}
class C extends B {
//Missing methods
}
Which of these when added to class C will lead to successful compilation of the
above Java program?
A. @Override
public void a() { }
@Override
void b() {}
B. @Override
public void a() { }
@Override
Abstract void b() { }
C. @Override
public void a() { }
D. @Override
void b() { }
10. How will the unsorted array below look like at the end of the first pass when
sorting it using bubble sort?
7 2 9 6 4
A. 2 4 6 7 9
B. 2 7 6 4 9
C. 2 7 6 9 4
D. 2 7 4 6 9
Page 4 of 10
Section B
1. Write a recursive method called power that takes int variables a and b, and
returns ab. [6]
2. Suppose a Java defined ArrayList object called myList has been initialized
correctly and myList has some elements. Write Java code segment that creates an
Iterator on myList called itr and use it to sum elements in the list. Assume the
necessary import statements have been written. [4]
3. Write a static method called sortArray which accepts an array of integer values
and implements the selection sort algorithm in ascending order [13]
Page 5 of 10
4. Assuming an initially empty stack with capacity of 4, fill diagrams below with
correct details to show stack contents and variable top after executing each of the
following instructions. Give comments where the operation leads to a problem
[8]
top
b) push(72)
top
c) pop()
top
d) peek()
top
5. Write a Java generic method frequency that takes a generic arrays of strictly
number types only and a target element. The method must return the number of
times the element appears in the array. [9]
Section C
Page 7 of 10
Page 8 of 10
Page 9 of 10
3. Class MyPatients : [10]
Create an application tester class called MyPatients that uses the PQueue class and its
methods to create and manipulate a queue of patient names (e.g Jacobs Thabo).
Testing PQueue:
Create a queue of patient names called clinic. Using the most appropriate methods
from PQueue class write code to achieve the following operations:
a) Read 3 patient names from keyboard and add them to the queue clinic.
b) Remove two patient names from the queue and display their names with
messages that they have been attended to.
c) Add the patient name Jacobs Thabo
d) Determine if patient name Amano Sebina is in the queue and display an
appropriate message based on the outcome.
e) Display total number of patient names currently in the queue, and their full
details.
Page 10 of 10