CMPSCI 187 Midterm 1 (Feb 17, 2016)

Similar documents
Midterm Exam CS 251, Intermediate Programming March 12, 2014

Midterm Exam CS 251, Intermediate Programming March 6, 2015

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Java Review: Objects

Midterm Exam 2 CS 455, Spring 2011

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CIS Fall Data Structures Midterm exam 10/16/2012

Programming II (CS300)

Midterm Exam CS 251, Intermediate Programming October 8, 2014

Building Java Programs

COMP1008 Exceptions. Runtime Error

SOLUTIONS (INCOMPLETE, UNCHECKED)

Final Exam. COMP Summer I June 26, points

CS S-08 Arrays and Midterm Review 1

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

Programming II (CS300)

CMPSCI 187: Programming With Data Structures. Review for First Midterm 9 October 2011

Building Java Programs

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

CMPSCI 187: Programming With Data Structures. Review for Final Exam David Mix Barrington 10 December 2012

Linked Lists. Chapter 12.3 in Savitch

CS 307 Midterm 2 Fall 2008

Summer Final Exam Review Session August 5, 2009

Generics, Type Safety, and Dynamic Data Structures

Object Oriented Programming 2015/16. Final Exam June 17, 2016

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

CIS 110 Introduction to Computer Programming Summer 2017 Final. Recitation # (e.g., 201):

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

Examination Questions Midterm 1

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CS 113 MIDTERM EXAM 2 SPRING 2013

Computer Science 1 Ah

CS165 Practice Final Exam

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz

No Aids Allowed. Do not turn this page until you have received the signal to start. Read this entire page or you ll miss the bonus question.

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Introduction to Computing II (ITI 1121) Final Examination

Ryerson University Department of Electrical & Computer Engineering COE618 Midterm Examination February 26, 2013

Midterm Exam (REGULAR SECTION)

CS314 Exam 2 - Fall Suggested Solution and Criteria 1

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

CS350: Data Structures Stacks

CSCE 2014 Final Exam Spring Version A

1 Constructors and Inheritance (4 minutes, 2 points)

Arrays. Eng. Mohammed Abdualal

CSE 143 SAMPLE MIDTERM

CS 1322 Test 2 - Version 1 Tuesday June 24 th

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

CS 307 Midterm 2 Spring 2008

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

Computer Science 62. Midterm Examination

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Java Identifiers, Data Types & Variables

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Self-test Java Programming

CS165 Practice Final Exam Answer Key

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

Midterm Exam 2 CS 455, Spring 2013

No Aids Allowed. Do not turn this page until you have received the signal to start.

Object Oriented Programming. Java-Lecture 6 - Arrays

Intro to Computer Science II

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

CE204 Data Structures and Algorithms Part 2

CS 455 Final Exam Spring 2017 [Bono] May 10, 2017

Data Structure. Recitation IV

Introduction to Computing II (ITI 1121) Final Examination

CS61BL Summer 2013 Midterm 2

The list abstract data type defined a number of operations that all list-like objects ought to implement:

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

CSE 131 Introduction to Computer Science Fall Exam II

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS 216 Exam 1 Fall SOLUTION

Introduction to Computer Science II (CSI 1101)

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

Midterm Exam 2 CS 455, Spring 2014

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

INSTRUCTIONS TO CANDIDATES

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Computer Programming, I. Laboratory Manual. Final Exam Solution

Arrays.

Final Exam CS 152, Computer Programming Fundamentals May 9, 2014

Largest Online Community of VU Students

Chapter 1: Introduction to Computers, Programs, and Java

Recursion (Part 3) 1

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

Transcription:

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