Professional Documents
Culture Documents
Sample 1 Midterm
Sample 1 Midterm
Sample 1 Midterm
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.
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));
}
}
}
Solution
………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………………………
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.
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;
}
}
}
………………………………………………………………………………………………………………………………………………………………………………
}
if (s.charAt(last) == ‘0’) {
return 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.
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).
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;
}
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