Computer Science E-119 Practice Midterm

Similar documents
Computer Science E-22 Practice Final Exam

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

COS 226 Midterm Exam, Spring 2009

CS 112 Practice Midterm Solutions

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

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

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

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

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

CS171 Midterm Exam. October 29, Name:

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

DATA STRUCTURES AND ALGORITHMS

Computer Science Foundation Exam

Computer Science Foundation Exam

Overview of Sorting Algorithms

Unit #2: Recursion, Induction, and Loop Invariants

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

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

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

CSL 201 Data Structures Mid-Semester Exam minutes

COS 226 Algorithms and Data Structures Fall Midterm

CSCE 2014 Final Exam Spring Version A

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

CSE 2320 Section 002, Fall 2015 Exam 2 Time: 80 mins

Unit #3: Recursion, Induction, and Loop Invariants

COS 226 Algorithms and Data Structures Fall Midterm

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

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

CS 216 Exam 1 Fall SOLUTION

COS 226 Midterm Exam, Spring 2011

Computer Science Foundation Exam. May 6, Computer Science. Section 1A. No Calculators! KEY. Score: 50

Solution to CSE 250 Final Exam

Section 1: True / False (2 points each, 30 pts total)

Shell Sort. Biostatistics 615/815

Chapter 17 Recursion

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Q1 Q2 Q3 Q4 Q5 Q6 Total

Cpt S 122 Data Structures. Sorting

CS 112 Midterm Exam Fall 2016

CSE 373 Spring Midterm. Friday April 21st

ECE264 Fall 2013 Exam 1, September 24, 2013

COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

ECE264 Fall 2013 Exam 3, November 20, 2013

Introduction. Sorting. Definitions and Terminology: Program efficiency. Sorting Algorithm Analysis. 13. Sorting. 13. Sorting.

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Computer Science Foundation Exam

Matriculation number:

Introduction to Computer Science Midterm 3 Fall, Points

COS 126 General Computer Science Spring Midterm 1

Computer Science Foundation Exam

Review of the Lectures 21-26, 30-32

Measuring algorithm efficiency

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #12

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

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

Computer Science 1 Ah

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Spring 2002: Fundamental Structures of Computer Science

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Midterm 2. Read all of the following information before starting the exam:

15-213/18-243, Summer 2011 Exam 1 Tuesday, June 28, 2011

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

Fast Bit Sort. A New In Place Sorting Technique. Nando Favaro February 2009

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Largest Online Community of VU Students

FORM 2 (Please put your name and form # on the scantron!!!!)

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

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

Sorting. Weiss chapter , 8.6

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

Sorting Algorithms. + Analysis of the Sorting Algorithms

Department of Computer Science Admission Test for PhD Program. Part I Time : 30 min Max Marks: 15

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

LECTURE 17. Array Searching and Sorting

Computer Science Foundation Exam

C0MP1921/2011/2091 SAMPLE Final Exam Data Structures and Algorithms/Data Organisation/Computing 2

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Practice final for EECS 380, 2001: Prof Markov

Final Exam. COMP Summer I June 26, points

EECE.2160: ECE Application Programming Fall 2017

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening.

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

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

Computer Science Foundation Exam

Chapter 17 Recursion

CS 373: Combinatorial Algorithms, Spring 1999

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Computer Science Foundation Exam

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #10

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

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Transcription:

Name Computer Science E-119 Practice Midterm This exam consists of two parts. Part I has 5 multiple-choice questions worth 3 points each. Part II consists of 3 problems; show all your work on these problems so that partial credit may be awarded if your final answer is wrong but your reasoning is partially correct. You have one hour to complete the exam. The questions are worth a total of 50 points. In order to properly budget your time, plan on spending approximately one minute per point. You may use any notes, books, or other references at your disposal. However, do not waste too much time leafing through your materials. You may not use a personal computer or other computing device. Do all your work on the exam itself. Write clearly. Good luck! Problem Max. Score SCORE I 15 II-1 12 II-2 11 II-3 12 TOTAL 50 Answers to Part I: question #: 1 2 3 4 5 first choice: second choice: David G. Sullivan, Ph.D. page 1 of 12

Part I. Multiple-choice (3 pts. each). Specify up to two answers. Write up to two answers in the table at the bottom of the cover sheet. You will get one point for any problem where your first choice is wrong but your second choice is correct. 1. The following array is to be sorted in ascending order: 25 56 18 20 12 9 15 Which algorithm will cause the array to be ordered 18 20 12 9 15 25 56 at an intermediate stage in the sorting process? A. Shell sort (initial increment = 3) B. insertion sort C. bubble sort D. quicksort (initial pivot = 20) E. selection sort 2. The following method/function sorts an array of floating-point numbers: public static void sort(double a[]) { int i, j, n; double temp; n = a.length; for (i = 1; i < n; i++) { temp = a[i]; j = i; while (j >= 1 && a[j-1] > temp) { a[j] = a[j-1]; j--; a[j] = temp; The number of times that the condition a[j-1] > temp is evaluated grows in proportion to what function of n? A. n B. n! C. n*log(n) D. log(n) E. n 2 David G. Sullivan, Ph.D. page 2 of 12

3. Consider the following function/method: int test(int a, int b) { if (a < b) return 0; else return (1 + test(a-b, b)); What is returned by the call test(15, 4)? A. 1 B. 2 C. 3 D. 4 E. 5 4. Given the array of integers arr shown below 13 7 27 2 18 33 9 11 22 8 what is the output of the following statements? int[] p = new int[10]; int[] q = new int[10]; for (int i = 0; i < 10; i++) p[i] = arr[i]; q = p; p[4] = 20; System.out.println(arr[4] + + q[4]); A. 20 20 B. 18 18 C. 2 2 D. 18 20 E. 2 20 David G. Sullivan, Ph.D. page 3 of 12

5. You have a singly linked list constructed out of nodes defined as follows: public class Node { public int datum; public Node next; In all of functions shown below, the parameter first refers to the first node in the linked list, if there is one, and has the value null otherwise. Which of the following functions correctly inserts a value x at the front of the linked list and returns a reference to the new front of the linked list? I. public Node insertfront(node first, int x) { first = new Node(); first.datum = x; first.next = first; return first; II. public Node insertfront(node first, int x) { Node n = new Node(); n.datum = x; n.next = first; return n; III. public Node insertfront(node first, int x) { Node n = new Node(); first = n; n.datum = x; n.next = first; return first; A. I only B. II only C. III only D. I and II E. II and III David G. Sullivan, Ph.D. page 4 of 12

PART II: Answer all three questions in the space provided. II-1. Sorting (12 points total; 3 points per part) The array below is to be sorted in ascending order. 17 53 71 62 36 46 41 23 12 a. After the initial partition step of the version of quicksort discussed in lecture, with 36 as the pivot, how would the array be ordered? b. Assume that the initial iteration of Shell sort uses an increment of 3. After that initial iteration, how would the array be ordered? c. After the initial iteration of bubble sort, how would the array be ordered? d. On how many of the passes of insertion sort would the inner loop be skipped? Explain your answer briefly. David G. Sullivan, Ph.D. page 5 of 12

II-2. Linked Lists (11 points total) The diagram below shows a linked list of characters, along with two variables that each store a reference/pointer to one of the nodes in the list. head B A S E null The nodes in the linked list could be implemented using the following class: public class CharNode { public char val; public CharNode next; q a. 2 points On the diagram, circle the memory location specified by the expression head.next.next b. 2 points What is the value of the expression from part a? c. 3 points Write one or more lines of code that remove the node containing the character 'S' from the list. d. 4 points Modify the diagram above to reflect the results of executing the following lines on the original version of the list (before the 'S' was removed): q = q.next; q.next = head; David G. Sullivan, Ph.D. page 6 of 12

II-3. Recursion and Algorithm Analysis (12 points total) a. 8 points Write a recursive method named sumreciprocals that takes as its only argument a non-negative integer, n, and returns a double value that is the sum of the reciprocals of the integers from 1 to n. For example, sumreciprocals(2) should return 1.5, which is 1/1 + 1/2, and sumreciprocals(4) should return approximately 2.0833, which is 1/1 + 1/2 + 1/3 + 1/4. You do not need to perform any error-checking on the value of the parameter. No use of iteration is allowed. public static double sumreciprocals(int n) { b. 4 points Let t(n) be the number of times that sumreciprocals() is invoked for a given value of the parameter n. Write a recurrence relation for t(n) a formula or set of formulas that defines t in terms of itself. David G. Sullivan, Ph.D. page 7 of 12

S-I. Multiple Choice Supplemental Practice Problems 1. Which of the following statements is not true about the increments used in Shell sort? A. The increments should be decreasing. B. The increments should be relatively prime. C. The final increment should be 1. D. The increments should be prime numbers. E. All the statements above are true. 2. Here is an array that has just been partitioned by the first step of quicksort: 3 0 2 4 5 8 7 6 9 Which of the following statements is correct? A. 5 could be the pivot, but 7 could not be. B. 7 could be the pivot, but 5 could not be. C. Neither 5 nor 7 could be the pivot. D. Either 5 or 7 could be the pivot. 3. The diagram below suggests how we could implement a double-ended linked list, in which we maintain a reference to both the first and last nodes in the linked list. front 35 17 29 null rear Which one of the following operations would be inefficient to carry out when there are a large number of elements in the linked list? A. insertion at the end to which front refers B. insertion at the end to which rear refers C. deletion from the end to which front refers D. deletion from the end to which rear refers E. test for an empty linked list David G. Sullivan, Ph.D. page 8 of 12

4. You have a singly linked list constructed out of nodes defined as follows: public class Node { public int datum; public Node next; In the function shown below, the parameter f refers to the first node in the linked list, if there is one, and has the value null otherwise. The intent of the function is to remove the last node of the linked list. public void removelast(node first) { Node p, q; p = first; q = p.next; while (q.next!= null) { p = q; q = q.next; p.next = null; Which of the following describes the class of linked lists for which this function works correctly? A. No linked lists B. All nonempty linked lists C. All linked lists with more than one node D. Empty linked lists and all linked lists with more than one node E. All linked lists 5. Through experiment, you determine that selection sort performs 5000 comparisons when sorting an array of some size k. If you doubled the size of the array to 2k, approximately how many comparisons would you expect it to perform? A. 5000 B. 10000 C. 20000 D. 40000 E. the value would depend on the contents of the array David G. Sullivan, Ph.D. page 9 of 12

6. Through experiment, you determine that selection sort performs 5000 moves when sorting a array of some size k. If you doubled the size of the array to 2k, approximately how many moves would you expect it to perform? A. 5000 B. 10000 C. 20000 D. 40000 E. the value would depend on the contents of the array 7. A stack S of integers initially contains the following data: 6 (top) 2 7 3 The following code is then executed: int x = S.pop(); int y = S.pop(); int z = S.pop(); S.push(x + y); int w = S.pop(); S.push(w + z); After this code has been executed, what are the contents of the stack? A. 8 B. 10 10 8 C. 15 D. 10 3 8 6 3 E. The stack is empty. David G. Sullivan, Ph.D. page 10 of 12

8. A program for recursive backtracking includes a method similar to this one: void key_function(int i) { for (int alternative = i; alternative < n; alternative++) { do_alt(alternative); ************* undo_alt(alternative); The line ************* should be replaced by: A. do_alt(alternative + 1); B. key_function(alternative); C. key_function(alternative + 1); D. key_function(i); E. key_function(i + 1); S-II-1. Sorting a. Describe, and illustrate with a simple example, the distinctive feature of the Shellsort algorithm. b. Let s say that you need to sort a collection of data that is stored in a singly linked list. Would Shellsort be a good algorithm to adapt for this problem? Why or why not? David G. Sullivan, Ph.D. page 11 of 12

S-II-2. Recursion The Fibonacci sequence is a famous sequence of integers that begins as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, The terms in this sequence can be defined as follows: (i) term 0 is 0 (ii) term 1 is 1 (ii) all subsequent terms in the sequence can be obtained by summing the previous two terms in the sequence: term n = term n 1 + term n 2 for all n >= 2 For example: term 3 = term 2 + term 1 = 1 + 1 = 2 term 5 = term 4 + term 3 = 3 + 2 = 5 a. Write a recursive method for finding the nth term in the Fibonacci sequence: public static int fib(int n) { b. Let t(n) be the number of times that your fib() method is invoked for a given value of the parameter n. Write a recurrence relation for t(n) a formula or set of formulas that defines t in terms of itself. David G. Sullivan, Ph.D. page 12 of 12