(1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii,...
-
Upload
erick-burns -
Category
Documents
-
view
260 -
download
7
Transcript of (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii,...
![Page 1: (1) Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/1.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/2.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/3.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/4.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/5.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/6.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/7.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/8.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/9.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/10.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/11.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/12.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/13.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/14.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/15.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/16.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/17.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/18.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/19.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/20.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/21.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/22.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/23.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/24.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/25.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/26.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/27.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/28.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/29.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/30.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/31.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/32.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/33.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/34.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/35.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/36.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/37.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/38.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/39.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/40.jpg)
(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.](https://reader033.fdocuments.net/reader033/viewer/2022061510/56649f1e5503460f94c36318/html5/thumbnails/41.jpg)
(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;