Java Programming: Data Structures, Algorithms, and Recursion, Exams of Computer Science

A final exam for a university-level java programming course, covering topics such as linked lists, sorting algorithms, recursion, and testing. It includes sample code, problem-solving exercises, and assertion statements.

Typology: Exams

Pre 2010

Uploaded on 09/17/2009

koofers-user-gtu
koofers-user-gtu 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CIS3023 Final Exam Summer 2008 SAMPLE Name: ______________________
1 (15). You have the following definitions for LinkedList and Node:
public class LinkedList {
protected Node head;
public Node getFirst() {
return head;
}
public void setFirst(Node
head) {
this.head = head;
}
}
class Node {
private Object element;
private Node next;
public Node(Object element, Node
next) {
this.element = element;
this.next = next;
}
public Object getElement() {
return element;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
Complete the method below:
// This method does a linear search over a linked list and returns the index of the element
// or -1 if the element is not in the list.
public int doLinearSearch(LinkedList list, Object target) {
}
pf3
pf4
pf5

Partial preview of the text

Download Java Programming: Data Structures, Algorithms, and Recursion and more Exams Computer Science in PDF only on Docsity!

CIS3023 Final Exam Summer 2008 SAMPLE Name: ______________________

1 (15). You have the following definitions for LinkedList and Node:

public class LinkedList { protected Node head;

public Node getFirst() { return head; }

public void setFirst(Node head) { this .head = head; } }

class Node { private Object element; private Node next;

public Node(Object element, Node next) { this .element = element; this .next = next; }

public Object getElement() { return element; }

public Node getNext() { return next; }

public void setNext(Node next) { this .next = next; } }

Complete the method below:

// This method does a linear search over a linked list and returns the index of the element // or -1 if the element is not in the list. public int doLinearSearch(LinkedList list, Object target) {

  1. (20) Part A: Sort the following list using Bubble Sort. Show all steps. Is this sort in- place? Why? No code is required.

Index: 0 1 2 3 4 5 6 Value: 9 3 1 8 5 7 3

Part B: Sort the same list using MergeSort. Show all steps. Is this sort in-place? Why?

4 (15). You want to remove all duplicate words from a String. Assume that the String has only words and spaces, and has no punctuation. You must use an ArrayList to solve this problem. Remember to parameterize your ArrayList (using bracket notation). Complete the method below.

public String removeDuplicates(String words) {

5 (15). You have the following recursive method that calculates the nth Fibonacci number:

public int fib(int num) { if (num == 1 || num == 2) { return 1; } else { return fib(num – 1) + fib(num – 2); } }

Convert this into a method that uses a stack instead of making recursive calls.

public int fib(int num) {

7 (5). Explain what it means for one thread to join another thread.