CS 112 Midterm Exam Fall 2016

Similar documents
CSE 247 Data Structures and Algorithms Fall Exam I

Final Exam. COMP Summer I June 26, points

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Linked Structures. See Section 3.2 of the text.

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CS 101 Fall 2006 Midterm 1 Name: ID:

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

CS171 Midterm Exam. October 29, Name:

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CS 151 Name Final Exam December 17, 2014

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CSE373 Winter 2014, Midterm Examination January 29, 2014

Lower Bound on Comparison-based Sorting

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE wi: Practice Midterm

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

CSE 143 Sample Midterm Exam #4

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CSE 373 Spring Midterm. Friday April 21st

Sorting race.

CS 101 Spring 2007 Midterm 2 Name: ID:

CS 161 Summer 2009 Homework #1 Sample Solutions

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

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

COMP 250 Fall Midterm examination

CS 151 Name Final Exam December 17, 2014

COS 226 Algorithms and Data Structures Fall Midterm

Data Structures and Algorithms Sorting

McGill University Faculty of Science School of Computer Science. MIDTERM EXAMINATION - Sample solutions. COMP-250: Introduction to Computer Science

CSCE 2014 Final Exam Spring Version A

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

Computer Science E-119 Practice Midterm

Structure and Interpretation of Computer Programs

York University. AP/ITEC Section M INTRODUCTION TO DATA STRUCTURES Winter Midterm Test

CSE143 Notes for Monday, 7/06/15

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 320 Midterm Exam Solution

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Prelim One Solution. CS211 Fall Name. NetID

Computer Science E-22 Practice Final Exam

CS126 Final Exam Review

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Sorting race.

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

Princeton University Computer Science COS226: Data Structures and Algorithms. Midterm, Fall 2013

Total Score /15 /20 /30 /10 /5 /20 Grader

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS 372: Computational Geometry Lecture 3 Line Segment Intersection

Algorithms 1 / 24. Algorithms Sequential Search

Name Section Number. CS210 Exam #1 *** TURN OFF ALL ELECTRONIC DEVICES *** Practice

Problem Set 6 Due: 11:59 Sunday, April 29

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

( ) n 3. n 2 ( ) D. Ο

CIS Fall Data Structures Midterm exam 10/16/2012

ext Total Score /20 /20 /15 /20 /25 /5 Grader

Largest Online Community of VU Students

CSE 332 Winter 2018 Final Exam (closed book, closed notes, no calculators)

Computer Science 302 Spring 2018 Practice Examination for the Second Examination,

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

Structure and Interpretation of Computer Programs

CS 177 Week 15 Recitation Slides. Review

Total Score /15 /20 /30 /10 /5 /20 Grader

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

CSE332 Summer 2012 Final Exam, August 15, 2012

Give one example where you might wish to use a three dimensional array

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

CSE 241 Algorithms and Data Structures Fall Semester, Midterm Exam

COS 226 Midterm Exam, Spring 2009

UNIVERSITY REGULATIONS

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Section 05: Midterm Review

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Data Structures Question Bank Multiple Choice

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

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

CS 314 Final Fall 2012

Practice Midterm Exam Solutions

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

York University AK/ITEC OBJECT-BASED PROGRAMMING. Midterm Test Sample. Examiner: S.Y. Chen Duration: One Hour and Fifteen Minutes

COS 226 Algorithms and Data Structures Fall Midterm Solutions

Final Exam. 1. (8 pts) Consider the following directed graph. If you iterate through all of the vertices as

n 2 ( ) ( ) + n is in Θ n logn

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

CS171 Final Practice Exam

Transcription:

Name: CS 112 Midterm Exam Fall 2016 B There are 7 problems on the exam. The first and last are mandatory, and you may eliminate any one of problems 2 6 by drawing an X through them. Problem 1 is worth 10 points, and all other problems are worth 18 points. Please write in pen if possible. Circle answers when they occur in the midst of a bunch of work. If you need more room, use the back of the sheet and tell me this on the front sheet. Problem One. (True/False MANDATORY) Write True or False to the left of each statement. [ False statements in red ] 1. If a class A implements an interface B, then all the public methods in A must also be specified in B. [A must satisfy AT LEAST the methods in B, but could do extra ones.] 2. In Java you may redeclare a variable inside its scope, as long as the new declaration is in a for loop. [nope] 3. If Stack is a generic data type, then you can declare a new stack of ints using: Stack<int> S = new Stack<int>(); [ Not counted ] 4. An overloaded operator in Java is one that can be used for more than one type of operand. 5. Mergesort is difficult to implement in place (i.e., without using an extra array). 6. When performing a widening conversion you must cast the value to the new type or else you will get an error. [widening conversions cause no problems.] 7. If a class BigInt contains a method getlength() and you see an expression BigInt.getLength(), then you know that getlength() must be declared as static inside the class BigInt. 8. The code in the box will print out a 6. 9. In the Boolean expression (( A B ) && C), if the expression A evaluates to false, then regardless of the value of B, the expression C will not be evaluated. [if B is true, then C must be evaluated.] 10. I have put my name on the top of this exam. J int x = 5; ++x; System.out.println( x++ );

Problem Five. For each of the following algorithms or problems, state the Θ(...N...) estimate for the worst case; if a problem is listed, assume you use the best known algorithm that we studied this semester. (A) Finding the average of the numbers in a linked list. Θ( N ) (B) Finding the largest element in an ordered array. Θ( 1 ) (C) Finding a number in an ordered array. Θ( log(n) ) (D) Insertion Sort on an array that is already sorted in the correct order. Θ( N ) (E) Inserting a new number into an unordered linked list, assuming you don t care about duplicates in the list. Θ( 1 ) (F) Inserting a new number into an unordered linked list, assuming you may NOT insert duplicates into the list. Θ( N ) (G) Inserting a new number into an ordered array. Θ( N ) (H) Finding a number in an ordered linked list. Θ( N ) (I) Finding the largest three elements in an unordered array and printing them out in order. Θ( N ) (J) Mergesort on an array that is already sorted in the correct order. Θ( N log(n) )

Problem Four. Consider this linked list: head1 1 2 3 4 5 Apply the following code to this linked list, and (A) draw a picture of the data structure that results, including what the variables head1, p, q, and head2 point to and (B) give a one-sentence description of what this method does. public Node whatdoesitdo(node head) { if( head == null head.next == null) return null; Node p = head; Node q = p.next; Node head2 = q; while( q.next!= null ) { p.next = q.next; q.next = p.next.next; p = p.next; q = q.next; if(q == null) return head2; p.next = null; return head2; Node head2 = whatdoesitdo( head1 ); (A) Draw the data structure heada --> 3 --> 4 --> 5 -->. headb --> 1 --> 2 -->. (B) Describe in one sentence what it does. The method unzips the list into two lists, putting every other node in another list (starting from position 1, odd numbered nodes in heada and even-numbered nodes in headb.

Problem Six. Consider the program shown below. This problem is similar to one from the Part A homeworks. Suppose a number of insert() and getmax() operations are performed on a MaxQueue M. The put() operations put the integers 0 through 9 onto M in order, and the getmax() operations print the return value. That is, the sequence looks like this: insert(0); insert(1); insert(2);... insert(9); For each of the following sequences, state whether or not they could occur as a result of such a sequence of MaxQueue operations. a. 3 2 1 0 4 5 8 6 7 9 NO b. 9 8 7 6 5 4 2 3 1 0 NO c. 3 2 1 0 4 5 9 8 7 6 YES d. 0 1 3 2 4 6 5 7 9 8 YES

Problem Two. This is about Mergesort and has two parts. (A) Perform Mergesort on the following array, according to the algorithm presented in lecture: (i) show the 2D matrix of values that would be produced as you perform the algorithm (as shown in class), (ii) give the exact number of comparisons between elements in the array that were necessary, and (iii) give the exact number of moves of elements that were necessary. 5 8 7 4 6 2 1 3 # Comps # Moves 5 8 4 7 2 6 1 3 4 8 4 5 7 8 1 2 3 6 6 8 1 2 3 4 5 6 7 8 6 8 Total: 16 24 (B) Give an example of a sequence of 8 numbers (two sorted lists of 4 each, to be sorted as if in the last stage of the mergesort algorithm), in which merging the two lists would take the minimum number of comparisons. 1 2 3 7 4 5 6 8 (one list has largest, other has second-largest) (C) In general, if you have a sequence of N numbers (N an even number) consisting of two sorted sublists of size N/2 each, what is the minimum number of comparisons necessary to merge the two sublists? 1 2 3 4 5 6 7 8 (every number in one list is larger than all in the other list) (D) Give an example of a sequence of 8 numbers (two sorted lists of 4 each, to be sorted as if in the last stage of the mergesort algorithm), in which merging the two lists would take the maximum number of comparisons. N - 1 (generalization of (A) -- last comparison is done with 2 numbers left) (E) In general, if you have a sequence of N numbers (N an even number) consisting of two sorted sublists of size N/2 each, what is the maximum number of comparisons necessary to merge the two sublists? N / 2 (generalization of (B) -- only numbers in smaller list must be compared)

Problem Two. How many times does the following code print out X! overall? Express your answer as a function of N, in terms of Θ(...N...). You must write the Θ(...N...) estimate for each individual for loop first, then figure out the estimate for the three separate nested loops and then give the overall result for the whole page. Show all work! Partial credit will be given for the parts of your answer! for( int j = 1; j < 1000*N; j = j + 2) { Θ(N) for(int k = 10*N; k >= 1; k = k / 2) { Θ(log(N)) Subtotal: Θ(N*log(N)) for( int j = -N; j < 1000; ++j) { Θ(N) for( int m = 2; m < log(n); ++m ) { Θ(log(N)) for( int m = -N; m < N; m = m + (N/2)) { Θ(1) Subtotal: Θ(N*(log(N)+1) = Θ(N*log(N)+N) = Θ(N*log(N)) for( int i = 7; i < N-1; i = i + 2 ) { Θ(N) for( int k = N; k >= 1; k = k / 2 ) { Θ(log(N)) for( int j = 1; j < 1000; j = j * 2 ) { Θ(1) Subtotal: Θ(N*log(N)*1) = Θ(N*log(N)) Total: Θ( N*log(N)+ N*log(N)+ N*log(N) ) = Θ( N*log(N) ) Your answer: The whole page prints out Hi there Θ( N*log(N)) ) times.

Problem Seven. (MANDATORY) In this problem you will add a new method to a stack, which will in effect allow it to be used as a priority queue. You must fill in the missing code in the template below: Here is an alternate way of doing it: