Presentation is loading. Please wait.

Presentation is loading. Please wait.

Information and Computer Sciences University of Hawaii, Manoa

Similar presentations


Presentation on theme: "Information and Computer Sciences University of Hawaii, Manoa"— Presentation transcript:

1 Information and Computer Sciences University of Hawaii, Manoa
Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

2 2 3 Question 1 String x = new String(“cat”);
String y = new String(“cat”); String z = x; How many objects are created? How many references? Is x == y true or false Is x.equals(y) true or false 2 3

3 Question 2 Write the List<E> interface:
/** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

4 Question 2 Write the List<E> interface:
/** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

5 Question 2 Write the List<E> interface:
/** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

6 Question 2 Write the List<E> interface:
/** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

7 Question 2 Write the List<E> interface:
/** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

8 Question 2 Write the List<E> interface:
/** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

9 Question 2 Write the List<E> interface:
/** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

10 Question 2 Write the List<E> interface:
/** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

11 Question 3 Does Student inherit all the non-private methods of Person? Does Student inherit all the non-private methods of GradeLevel? Is Student required to provide all the methods of Person? Is Student required to provide all the methods of GradeLevel? Is Student a subclass of Person or GradeLevel (circle one) public class Student extends Person implements GradeLevel { yes no no yes*

12 Question 4 Complete this implementation of the remove method.
public class MyArrayList<E> implements List<E> { protected E[] data; protected int size; public E remove(int index) { if (index <0 || index >= size) { throw new IndexOutOfBoundsException(“bad index “ + index); }

13 Question 4 Complete this implementation of the remove method.
public class MyArrayList<E> implements List<E> { protected E[] data; protected int size; public E remove(int index) { if (index <0 || index >= size) { throw new IndexOutOfBoundsException(“bad index “ + index); } E returnValue = data[index];

14 Question 4 Complete this implementation of the remove method.
public class MyArrayList<E> implements List<E> { protected E[] data; protected int size; public E remove(int index) { if (index <0 || index >= size) { throw new IndexOutOfBoundsException(“bad index “ + index); } E returnValue = data[index]; for (int i = index; i < size – 1; i++) { data[i] = data[i + 1];

15 Question 4 Complete this implementation of the remove method.
public class MyArrayList<E> implements List<E> { protected E[] data; protected int size; public E remove(int index) { if (index <0 || index >= size) { throw new IndexOutOfBoundsException(“bad index “ + index); } E returnValue = data[index]; for (int i = index; i < size – 1; i++) { data[i] = data[i + 1]; size--; return returnValue;

16 Question 5 Tell me (briefly) everything wrong with this method.
public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

17 Question 5 Tell me (briefly) everything wrong with this method.
public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

18 Question 5 Tell me (briefly) everything wrong with this method.
public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

19 Question 5 Tell me (briefly) everything wrong with this method.
public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

20 Question 5 Tell me (briefly) everything wrong with this method.
public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

21 Question 5 Tell me (briefly) everything wrong with this method.
public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

22 Question 6 What is the Big-O of the following code?
double result = 0.0; for (int i = 5; i < 2n; i++) { result = result * i; for (int j = n – 2; j > 1; j--) { int temp = j – i; result += temp; }

23 Question 6 What is the Big-O of the following code?
double result = 0.0; for (int i = 5; i < 2n; i++) { n – 6 result = result * i; for (int j = n – 2; j > 1; j--) { int temp = j – i; result += temp; }

24 Question 6 What is the Big-O of the following code?
double result = 0.0; for (int i = 5; i < 2n; i++) { n – 6 result = result * i; for (int j = n – 2; j > 1; j--) { n - 3 int temp = j – i; result += temp; } (2n – 6) * (n – 3)

25 Question 6 What is the Big-O of the following code?
double result = 0.0; for (int i = 5; i < 2n; i++) { n – 6 result = result * i; for (int j = n – 2; j > 1; j--) { n - 3 int temp = j – i; result += temp; } 2n2 – 9n - 18

26 Question 6 What is the Big-O of the following code?
double result = 0.0; for (int i = 5; i < 2n; i++) { n – 5 result = result * i; for (int j = n – 2; j > 1; j--) { n - 3 int temp = j – i; result += temp; } O(n2)

27 Question 7 The super class of all Java classes is
__________________________. Object

28 Question 8 The Node class for a single-linked list has references to the data and to the next and previous Nodes? True of False. (circle one)

29 Question 9 Add the class variable(s) and implement the hasNext() and next() methods for this Linked List class: public class MyLinkedList<E> implements List<E> { protected LinkedNode<E> head; // singly-linked list private class MyIterator implements Iterator<E> { public boolean hasNext() { } public E next() { // may throw java.util.NoSuchElementException

30 Question 9 Add the class variable(s) and implement the hasNext() and next() methods for this Linked List class: public class MyLinkedList<E> implements List<E> { protected LinkedNode<E> head; // singly-linked list private class MyIterator implements Iterator<E> { private LinkedNode<E> next; public boolean hasNext() { } public E next() { // may throw java.util.NoSuchElementException

31 Question 9 Add the class variable(s) and implement the hasNext() and next() methods for this Linked List class: public class MyLinkedList<E> implements List<E> { protected LinkedNode<E> head; // singly-linked list private class MyIterator implements Iterator<E> { private LinkedNode<E> next; public boolean hasNext() { return next != null; } public E next() { // may throw java.util.NoSuchElementException

32 Question 9 Add the class variable(s) and implement the hasNext() and next() methods for this Linked List class: public class MyLinkedList<E> implements List<E> { protected LinkedNode<E> head; // singly-linked list private class MyIterator implements Iterator<E> { private LinkedNode<E> next; public boolean hasNext() { return next != null; } public E next() { // may throw java.util.NoSuchElementException if (!hasNext()) { throw new java.util.NoSuchElementException(); }

33 Question 9 Add the class variable(s) and implement the hasNext() and next() methods for this Linked List class: public class MyLinkedList<E> implements List<E> { protected LinkedNode<E> head; // singly-linked list private class MyIterator implements Iterator<E> { private LinkedNode<E> next; public boolean hasNext() { return next != null; } public E next() { // may throw java.util.NoSuchElementException if (!hasNext()) { throw new java.util.NoSuchElementException(); } E returnValue = next.data; next = next.next; return returnValue;

34 Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { }

35 Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { int pass = 1; boolean exchanged = false; }

36 Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { int pass = 1; boolean exchanged = false; do { } while (exchanged); }

37 Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { int pass = 1; boolean exchanged = false; do { exchanged = false; for (int i = 0; i < data.length – pass; i++) { } } while (exchanged);

38 Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { int pass = 1; boolean exchanged = false; do { exchanged = false; for (int i = 0; i < data.length – pass; i++) { if (c.compare(data[i], data[i + 1] > 0) { } } while (exchanged);

39 Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { int pass = 1; boolean exchanged = false; do { exchanged = false; for (int i = 0; i < data.length – pass; i++) { if (c.compare(data[i], data[i + 1] > 0) { exchanged = true; E temp = data[i]; data[i] = data[i + 1]; data[i + 1] = temp; } } while (exchanged);

40 Question 11 A Java interface is a(n) ______________________ between the interface designer and the programmer who codes a class that implements the interface. (circle one) precondition postcondition message contract

41 Question 12 When looping over a circularly linked list, how do you know when you have reached the end? temp.next == head; or temp == tail;


Download ppt "Information and Computer Sciences University of Hawaii, Manoa"

Similar presentations


Ads by Google