(1) midterm 1 solutions ics 211 cam moore information and computer sciences university of hawaii,...

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

Upload: erick-burns

Post on 04-Jan-2016

260 views

Category:

Documents


7 download

TRANSCRIPT

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

(1)

Midterm 1 Solutions

ICS 211Cam Moore

Information and Computer SciencesUniversity of Hawaii, Manoa

Page 2: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(2)

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

Page 3: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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 ...}

Page 4: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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 ...}

Page 5: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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 ...}

Page 6: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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 ...}

Page 7: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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 ...}

Page 8: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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 ...}

Page 9: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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 ...}

Page 10: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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 ...}

Page 11: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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*

Page 12: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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); }

Page 13: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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];

Page 14: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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]; }

Page 15: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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; }

Page 16: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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;}

Page 17: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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;}

Page 18: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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;}

Page 19: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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;}

Page 20: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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;}

Page 21: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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;}

Page 22: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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; }}

Page 23: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(23)

Question 6 What is the Big-O of the following code?

double result = 0.0;for (int i = 5; i < 2n; i++) { 2n – 6 result = result * i; for (int j = n – 2; j > 1; j--) { int temp = j – i; result += temp; }}

Page 24: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(24)

Question 6 What is the Big-O of the following code?

double result = 0.0;for (int i = 5; i < 2n; i++) { 2n – 6 result = result * i; for (int j = n – 2; j > 1; j--) { n - 3 int temp = j – i; result += temp; }}

(2n – 6) * (n – 3)

Page 25: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(25)

Question 6 What is the Big-O of the following code?

double result = 0.0;for (int i = 5; i < 2n; i++) { 2n – 6 result = result * i; for (int j = n – 2; j > 1; j--) { n - 3 int temp = j – i; result += temp; }}

2n2 – 9n - 18

Page 26: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(26)

Question 6 What is the Big-O of the following code?

double result = 0.0;for (int i = 5; i < 2n; i++) { 2n – 5 result = result * i; for (int j = n – 2; j > 1; j--) { n - 3 int temp = j – i; result += temp; }}

O(n2)

Page 27: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(27)

Question 7 The super class of all Java classes is

__________________________.Object

Page 28: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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)

Page 29: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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

Page 30: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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

Page 31: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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

Page 32: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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(); }

Page 33: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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; }

Page 34: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(34)

Question 10 Implement the following method using the bubble sort algorithm:

public static void bubbleSort(E[] data, Comparator<E> c) {

}

Page 35: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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;

}

Page 36: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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);}

Page 37: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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);}

Page 38: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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);}

Page 39: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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);}

Page 40: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(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)

A) precondition

B) postcondition

C) message

D) contract

Page 41: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

(41)

Question 12 When looping over a circularly linked list, how do you know when you have reached the end?

temp.next == head;ortemp == tail;