Sample 1 Midterm

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Data Strutures (CSBP319)

Midterm Exam – Spring 2020

Student Name Duration 1 Hour


Student ID Section Exam Date 8/04/2020

Questions Marks
Q1 5

Q2 6

Q3 9

Total 20

Instructions
1. Answer all questions.
2. No notes or texts books are permitted in the examination hall.
3. All mobile phones and electronic devices must be switched off.
4. Use your time carefully. If you feel you are stuck, skip to the next
question.
5. Answer questions in the space provided. You may use the rear of a
page if the space provided for a question is not enough.
6. Please hand in the complete questionnaire at the end of the
examination.
7. Any cheating will result in failing the course and lead to disciplinary
actions against you.

All the best!


Q 1: (5 marks, CLO3) What is the output of the following code snippet?

Java program
public static void secret(LinkedList list) {
for (int i = 0; i < list.size()-1; i++) {
if (((String)list.get(i)).charAt(0) == 'A') {
list.set(i, list.get(i+1));
}
}
}

public static void main(String[] args) {


LinkedList list = new LinkedList();
list.add("CSBP319");
list.add("A good");
list.remove();
list.add("A programmer");
if (list.contains("A")) {
list.removeLast();
}
list.add("3 sections");
list.add("Welcome to");
System.out.println(list);
secret(list);
System.out.println(list);
}

Solution
………………………………………………………………………………………………………………………………………………………………………………

[A good, A programmer, 3 sections, Welcome to]

[A programmer, 3 sections, 3 sections, Welcome to]

………………………………………………………………………………………………………………………………………………………………………………

………………………………………………………………………………………………………………………………………………………………………………

………………………………………………………………………………………………………………………………………………………………………………

2/7
Q2. (6 marks, CLO 1)
a) (3 marks) Design a recursive Java method findMin (), which receives as parameters
an array of integers and the starting position in the array, and returns the smallest
value in this array.

Example: if we have int a[]={10, -3, 8, -12, 7, -95, -21};


findMin (a,0) will return -95

Java program
public static …………………………………… findMin (int[] arr, int start) {
private static int findMin(int arr[], int current) {
if (current == arr.length) {
return arr[0]; // or Integer.MAX_VALUE, or any element in the array
} else {
int minSoFar = findMin(arr, current + 1);
if (arr[current] < minSoFar) {
return arr[current];
} else {
return minSoFar;
}
}
}
………………………………………………………………………………………………………………………………………………………………………………
}

b) (3 marks) Consider the following recursive method test:

public static int test(String s, int last) {


if (last < 0) return 0;

if (s.charAt(last) == ‘0’) {
return 2 * test(s, last-1);
}

return 1 + 2 * test(s, last-1);


}

What is the output of: test("11001", 4). Show the trace of execution.
S“11001”, last 4 S“11001”, last 3 S“11001”, last 2 S“11001”, last 1 S“11001”, last 0 S“11001”, last -1
Ret 1+ 2 * test(“11001”, 3) Ret 2 * test(“11001”, 2) Ret 2 * test(“11001”, 1) Ret 1+ 2 * test(“11001”, 0) Ret 1+ 2 * test(“11001”, -1) Ret 0
25 12 6 3 1

3/7
Q3. (9 marks) CLO 3
Consider the following definition of a class MyNode, and a Linked List MyLinkedlist created from
objects of the class MyNode.

public class MyNode { public class MyLinkedlist {


private int number; private Mynode head;
private Mynode next;
public MyLinkedlist() {
public MyNode(int n) { head = null;
number = n; } // Mylinkedlist()
next = null; public boolean isEmpty(){
} // Mynode() //
public void setNext(Mynode nextNode) { } //isEmpty()
next = nextNode;
} // setNext() public void insert(Mynode nextNode) {
public Mynode getNext() { //
return next; } // insert()
} // getNext() public void print() {
public void setNumber(int n) { //
number = n; } // print()
} // setNnumber()
public int getNumber() { public void size() {
return number; //
} // getNumber() } // size()
} // Mynode

} // Mylinkedlist

1. Write the java statements to set the data in the 3rd node in the following linked list to be 20.
(don’t use java.util.LinkedList) (2 marks)

head

2 . 3 . 6 . 8 . 1 .

Head→ 2 → 3 → 6 → 8 → 1 X

…………………………………………………………………………………………………
………
head.getNext().getNext().setNumber(20);

.…………………………………………………………………………………………
…………………………………………………………………………………………………

4/7
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
2. Write a method, sumEven() in the class MyLinkedList, to compute the sum of the even values
stored in the linked list. (3 marks).

Use the methods of the class MyNode to answer this question.

Java program
public int sumEven() {
int sum = 0 ;
current = head;
While (current !=null) {
if (current.getNumber() %2 == 0 )
sum += current.getNumber();
}
current = current.getNext();
}
return sum;
……………………………………………………………………………………….……………………………………………………………………………………………………
………………………………………………….…………………………………………………………………………………………………………………………………………
…………….……………………………………………………………………………………………………………………………………………………….…………….……
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………….…………….……
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………….…………….……
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………………………….…………….……
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………………………

5/7
………………………………………………………………………………………………………………………………………………………………………………………………
} ………………………………………………………………………………………………………………………………………………………………

3. Write a method called evenNodes(), in the class MyLinkedList, that returns an array
of references to the nodes for which the number is even. (4 marks)

Use the methods of the class MyNode and MyLinkedList to answer this question.

Hint: Create an array of MyNode, which has the same size as MyLinkedList. Starting
from the head of the list, each time you find a node of the list for which the number is
even, then assign that node to an element of the array.

Java program
public MyNode[] evenNodes() {
if (head == null) {
MyNode[] nodes = {};
return nodes;
}

MyNode current = head;


MyNode[] nodes = new MyNode[this.size()];
int arrayIndex=0;

While (current !=null) {


if (current.getNumber() %2 == 0 )
nodes[arrayIndex] = current;
arrayIndex++;
}
current = current.getNext();
}
return nodes;
}

Summary of the methods of java.util.LinkedList

boolean add(E e)
Appends the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element at the specified position in this list.
void addFirst(E e)
Inserts the specified element at the beginning of this list.
void addLast(E e)
Appends the specified element to the end of this list.
boolean contains(Object o)
Returns true if this list contains the specified element.
E get(int index)
Returns the element at the specified position in this list.
E getFirst()
Returns the first element in this list.

6/7
E getLast()
Returns the last element in this list.
E remove()
Retrieves and removes the head (first element) of this list.
E remove(int index)
Removes the element at the specified position in this list.
E removeFirst()
Removes and returns the first element from this list.
E removeLast()
Removes and returns the last element from this list.
E set(int index, E element)
Replaces the element at the specified position in this list with the
specified element.
int size()
Returns the number of elements in this list.

7/7

You might also like