Computer Science 62. Midterm Examination

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

Computer Science 136. Midterm Examination

You must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.

CSC 207H Fall L Java Quiz Duration 25 minutes Aids allowed: none

Suppose that we have linked list of integers where each node is represented as: // An item in the list.

Midterm Exam (REGULAR SECTION)

CSE 373 Spring Midterm. Friday April 21st

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

Introduction to Linked Data Structures

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Dynamic Data Structures

Computer Science 136. Midterm Examination

CS 307 Final Spring 2010

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

LinkedList Implementation Mini-project intro

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

CS171 Midterm Exam. October 29, Name:

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

Computer Science 136 Exam 2

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

CSCE 146 Midterm Exam 1 SAMPLE 24 September 2008

CS 10, Fall 2015, Professor Prasad Jayanti

ADTs, Arrays, Linked Lists

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011

Model Solutions. COMP 103: Test May, 2013

16. Dynamic Data Structures

ECE Fall st Midterm Examination (120 Minutes, closed book)

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

COMP 103. Mid Trimester Test 17 August 2006

Test 1: CPS 100. Owen Astrachan. October 1, 2004

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CS 307 Final Spring 2009

OF VICTORIA EXAMINATIONS- DECEMBER 2011 CSC

CSE 331 Midterm Exam 2/13/12

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

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

CS 307 Final Fall 2009

CSE331 Winter 2014, Midterm Examination February 12, 2014

COS 226 Algorithms and Data Structures Fall Midterm

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

Department of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z

Introduction to Computer Science II (ITI 1121) Final Examination

Model Solutions. COMP 103: Test April, 2013

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

CS 314 Final Fall 2011

Building Java Programs

CS 307 Midterm 2 Fall 2008

[TAP:PMUHE] Stack vs Queue

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

"apple" "grape" "grape" "grape" "apple"

Introduction to Computing II (ITI 1121) Final Examination

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

Topic 10: The Java Collections Framework (and Iterators)

CS 216 Exam 1 Fall SOLUTION

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

Examination Questions Midterm 1

Problem 1: Building and testing your own linked indexed list

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

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Linked lists. Yet another Abstract Data Type Provides another method for providing space-efficient storage of data

Linked Structures. See Section 3.2 of the text.

Introduction to Computing II (ITI 1121) Final Examination

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

CS 314 Final Fall 2012

CS 307 Final Spring 2011

CSE 143. Lecture 7: Linked List Basics reading: 16.2

Building Java Programs

CSE 143 SAMPLE MIDTERM

CPSC 35 Midterm Exam

Java Review: Objects

Prelim CS410, Summer July 1998 Please note: This exam is closed book, closed note. Sit every-other seat. Put your answers in this exam. The or

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

CMPT 125: Practice Midterm Answer Key

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

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

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 2

Programming II (CS300)

Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems. COMP Week 8 & 9 1

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

CS 314 Midterm 2 Fall 2012

COMP103 Test. 5 Sept, 2007

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

EXAMINATIONS 2016 TRIMESTER 2

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

CS 221 Review. Mason Vail

Summer Final Exam Review Session August 5, 2009

Section I B COMPUTER SCIENCE SOLUTION

Transcription:

Computer Science 62 Bruce/Mawhorter Fall 16 Midterm Examination October 5, 2016 Question Points Score 1 15 2 10 3 10 4 8 5 9 TOTAL 52 Your name (Please print)

1. Suppose you are given a singly-linked list class that holds strings and that maintains pointers to both the head and the tail of the list. Its fields and constructors are as follows: public class SinglyLinkedList { protected ListNode head; protected ListNode tail; public SinglyLinkedList() { this.head = null; this.tail = null;... The ListNode class looks like this: public class ListNode { private String value; private ListNode next; public ListNode(String value, ListNode next) { this.value = value; this.next = next; public String getvalue() { return this.value; public ListNode getnext() { return this.next; public String setvalue(string newvalue) { this.value = newvalue; public ListNode setnext(listnode newnext) { this.next = newnext;

Please add a new method to the class SinglyLinkedList with header: public void keep(int howmany) { which should modify the list so it only keeps the first howmany elements, dropping the rest of the elements from the list. E.g., if mylist originally contains 10 elements, then executing mylist.keep(6) should result in mylist having only the first 6 elements of the list. You don't need to worry about keeping track of the discarded nodes as long as you cut them off from the rest of the list. a. Write the pre- and post- conditions for the keep method. Just describe them in English. b. List at least one special case that either violates your preconditions or requires special handling. c. Write the code for keep on the next page (you don't need to worry about comments). Remember that you should check your preconditions (you can use "RuntimeError" if you need to throw any exceptions).

public void keep(int howmany) {

2. You have a singly linked list with only a head pointer (see the figure below). The insert() method for the list inserts new values into the list so that the elements remain in sorted order using the obvious algorithm. In other words, after each insertion, the list is in sorted order. a. Assume you are given a sequence of n values to insert one at a time into the list. What do you expect the total worst-case running time to be, using big-o notation, for inserting all of the values into the list? Give a brief (one to two sentence) justification for your answer. b. Suppose that the sequence of n values to be inserted just happen to be in reverse sorted order. E.g., you might be given the elements 47, 23, 19, 13, 7, 6, and finally 2. What do you expect the running time to be, using big-o notation, for inserting all of n values into the list? Give a brief (one to two sentence) justification for your answer.

3. Suppose we have a list of ints held as an ArrayList. As you may recall, the elements of the list are held in an array elts, with instance variable eltsfilled keeping track of the number of elements in the list. The standard implementation of an iterator for an ArrayList presents the elements of the array (one at a time) from slot 0 to slot eltsfilled -1. However, for a particular application we want to design a special iterator (call it inorderiterator) that presents the elements of the list in numeric order. Please describe in words (no Java code necessary) how you would initialize the iterator (i.e., what the constructor would do), and how you would write the hasnext() and next() methods. You may assume that the iterator is an inner class of the ArrayList class (thus giving it access to the instance variables of ArrayList). The initialization of the iterator should take no more than O (n log n) time in the worst case (where n is the size of the list) and the methods next() and hasnext() should each be O(1). The contents of the array elts should not be modified by the construction or use of the iterator. You may use any extra storage needed to create the iterator.

4. An advantage of using stacks and queues is that their limited number of operations allows more efficient representation than more general data structures. Please answer the following questions about the complexity of operations on queues, expressing all answers in big-o notation. A queue may be represented by a circular implementation on an array (or ArrayList) or by a singly linked list with a reference to both the front and rear. Please give the complexity of the following queue operations on a queue of size n for each representation, including one or two sentences justifying your answer. i) Enqueuing an element at the rear of the queue with a circular array: linked list: ii) Dequeing an element from the front of the queue with a circular array: linked list:

5. Short answer a. Describe carefully in words what happens when you insert an element into an ArrayList when it is already filled to capacity. b. We noted that when using Java graphics, we must call the method repaint (which the programmer doesn t write) in order to get the computer to eventually call the method paint, which is the one the programmer actually writes. Please explain why this happens and what data structure that we have discussed in class is used to make this all work. c. Explain how the run-time stack changes when a method is invoked and when the method completes execution.