CMPSCI 187 Midterm 1 (Feb 17, 2016) Instructions: This is a closed book, closed notes exam. You have 120 minutes to complete the exam. The exam has 10 pages printed on double sides. Be sure to check both sides of each paper. Please write down your name, student ID, and discussion section number on this cover page, as well as pages 7 and 9. Please write legibly. If we can t read your writing, we won t give you credits. Elegance and simplicity counts. Complicated solutions and code will receive less points than simple and easy to read code. Name: Student ID: Disc. Section No.: Section: 1 2 3 Total Maximum: 40 30 30 100 Score: 1
Section 1. Multiple Choices (20 x 2 = 40 points) ( One correct answer per question. Clearly mark your choice on the opscan sheet). 1. What s the output of the following code? int x = 4, y = 5; float z = x / y; System.out.println(z); (a) 1.25 (b) 1.0 (c) 0.8 (d) 0.0 2. In Java, a reference to an object (a) stores the data of the object. (b) stores the memory address (a pointer) to the object. (c) has a size that varies based on the class of the object. (d) can not be copied. 3. What s the output of the following code? Float x = new Float(1.0); Float y = new Float(x); System.out.println(x == y); (a) 1.0 (b) null (c) true (d) false 4. This line of code: LLCharacterNode[] ns = new LLCharacterNode[50]; (a) creates a single LLCharacterNode object. (b) creates 50 LLCharacterNode objects. (c) creates an array of 50 references of the type LLCharacterNode (d) creates a variable ns with a value of null. 5. If class Triangle implements the Geometry interface, and class RightTriangle extends Triangle, which of the following would not compile? ( a) Geometry g = new Geometry(); (b) Geometry g = new Triangle(); (c) Geometry g = new RightTriangle(); (d) Triangle t = new RightTriangle(); 2
6. A stack is a First-In-First-Out (FIFO) data structure. (a) true (b) false 7. What s the value of the following postfix expression? 10 6 1 2 + * - (a) 14 (b) 6 (c) 4 (d) -8 8. The following code: class DLLNode<T> { DLLNode() { private DLLNode<T> link1; private DLLNode<T> link2; DLLNode<String> a = new DLLNode<String>(); (a) would compile and run just fine. (b) would compile but fail at run-time due to unresolved link references. (c) would not compile because DLLNode<String> cannot be instantiated. (d) would not compile due to duplicate link variables. 9. What s the output of the following code? (assume a pop() that returns the object) LinkedStack<Integer> s = new LinkedStack<Integer>(); s.push(20); s.push(30); s.push(40); System.out.print(s.peek() + " " + s.pop() + " " + s.pop() + " "); (a) 40 40 30 (b) 20 30 40 (c) 20 20 30 (d) 40 30 20 10. The Big-O costs for searching an element in an array of size n, and for searching an element in a linked list of size n are, respectively: (a) O(1) and O(n) (b) O(n) and O(1) (c) O(n) and O(n) (d) O(1) and O(1) 3
11. What does this code output? public class WasteDisposalRobot extends Robot { public void start() { System.out.println("Finding waste."); public class Robot { public void start() { System.out.println("Robot started."); public static void main(string args[]){ Robot robot = new WasteDisposalRobot(); robot.start(); (a) Robot started. (b) Finding waste. (c) It throws a ClassCastException (d) It throws a NullPointerException 12. What does this code output? public class Robot { public static int x; public int y; public static void main(string args[]) { Robot AAA = new Robot(); Robot BBB = new Robot(); AAA.x = 10; BBB.x = 9; AAA.y = 8; BBB.y = 7; System.out.println(AAA.x + BBB.x + AAA.y + BBB.y); (a) 32 (b) 33 (c) 34 (d) 0 4
13. Checked exceptions are: (a) required to be caught or the program won t compile. (b) automatically caught. (c) subclasses of RunTimeException (d) classes that throw NullPointerException 14.Consider the algorithm for delimiter matching (i.e. checking if brackets in an expression are matched and balanced). If the stack is empty when the algorithm tries to pop an element, it means (a) the expression is missing one or more left (opening) delimiters. (b) the expression is missing one or more right (closing) delimiters. (c) at least one pair of left and right delimiters is not of the same type. (d) the algorithm has reached the end of the expression. 15.Assume that curr is a variable that points to a node in the middle of a long linked list. Which of the following inserts a new node nn into the linked list after the curr node? (a) nn.setlink(curr); curr = nn; (b) nn.setlink(curr); curr.setlink(nn); (c) nn.setlink(curr.getlink()); curr = nn; (d) nn.setlink(curr.getlink()); curr.setlink(nn); 16.Which of these are in increasing order of complexity? (a) O(n) < O(log n) < O(n^2) < O(n^3) < O(2^n) (b) O(n) < O(log n) < O(2^n) < O(n^2) < O(n^3) (c) O(log n) < O(n) < O(2^n) < O(n^2) < O(n^3) (d) O(log n) < O(n) < O(n^2) < O(n^3) < O(2^n) 17. What is the Big-O cost of the following method if n is the size of the array? public int add(int[] array) { if (array.length < 100) return 0; int sum = 0; for (int i = 0; i < 100; i++) sum += array[i]; return sum; (a) O(1) (b) O(log n) (c) O(n) (d) O(n^2) 5
18. What is the Big-O cost of the following code ( n is a large positive integer)? int count = 0, i = 0; while (i < n) { count ++; i ++; n --; (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) 19. What is the Big-O cost of the following code ( n is a large positive integer)? int count = 0; for (int i = 1; i < n; i *= 2) { for (int k = i; k <= i + 8; k ++ ) { count ++; (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) 20. What s the output of the following code? class Foo { public static void main(string args[]) { try { System.out.print( 1 ); throw new Exception( 2 ); System.out.print( 3 ); catch(exception e) { System.out.print(e.getMessage()); System.out.print( 4 ); System.out.print( 5 ); (a) 1 3 2 4 5 (b) 1 2 3 4 5 (c) 1 2 4 5 (d) 1 3 2 4 5 6
Name: Student ID: Disc. Section No. : Section 2. Array (15 x 2 = 30 points) A. Complete the following method which reverses the order of elements stored in an int array a. For example, if a[] = {1, 2, 3, 4, 5, calling reverse(a) will make it become {5, 4, 3, 2, 1. If you declare any new variables, they must be of type int. You are NOT allowed to create any new method, any new array, or use any object type (such as Stack or ArrayList). The running time of your method should be no more than O(n). public void reverse(int[] a) { int n = a.length; 7
B. Complete the method below to perform a circular left-shift of elements stored in a. For example, if a[] = {1, 2, 3, 4, 5, calling circularleftshift(a, 1) will circular left-shift the array once, making it {2, 3, 4, 5, 1. In other words, the first element is shifted to the last position, and all other elements are shifted to the left by one position. Calling circularleftshift(a, k) (where k > 0 ) is equivalent to repeat circularleftshift(a, 1) k times. If you declare any new variables, they must be of type int. You are NOT allowed to create any new method, any new array, or use any object type (such as Stack or ArrayList). The running time of your method should be no more than O(k*n) (i.e. linear with respect to k times n). public void circularleftshift(int[] a, int k) { if(k <= 0) return; int n = a.length; k = k % n; // modulo n so that k is always less than n 8
Name: Student ID: Disc. Section No. : Section 3. Linked List (10 x 3 = 30 points) public class LLNode<T> { private T data; private LLNode<T> next; public T getdata() { return data; public void setdata(t data) { this.data = data; public LLNode<T> getnext() { return next; public void setnext(llnode<t> next) { this.next = next; Given the above definition of a generic Linked List node, complete the following generic LinkedList class. Specifically, complete the add, elementat, and combinewith methods. Do NOT use iterators. Do NOT create new methods. public class LinkedList<T> { private LLNode<T> head; // Add a new element to the beginning of the linked list. O(1). public void add(t element) { // Return the k-th element on the list where k is the index // (the first element has index 0 and so on). If the linked // list has less than (k+1) elements, return null. O(n). public T elementat(int k) { // write your code on the next page 9
// Combine this linked list with another linked list. As a // result, this linked list will contain all its original // elements plus all elements in the other linked list. O(n). public void combinewith(linkedlist<t> that) { 10
(Use this page as scratch paper. Do NOT write your final answers on this page) 11
(Use this page as scratch paper. Do NOT write your final answers on this page) 12