Summer Session 2004 Prelim I July 12, CUID: NetID:

Similar documents
Prelim One Solution. CS211 Fall Name. NetID

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

About this exam review

CS2110 Assignment 2 Lists, Induction, Recursion and Parsing, Summer

CmpSci 187: Programming with Data Structures Spring 2015

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

CS61B, Fall 2011 Final Examination (corrected) P. N. Hilfinger

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

Second Examination Solution

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

CIS 110 Introduction to Computer Programming. 7 May 2012 Final Exam

TREES Lecture 12 CS2110 Fall 2016

1. Data types ( =13 points)

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Question: Total Points: Score:

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

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

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

Exam Informatik D-MATH/D-PHYS :00 11:00

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

Topic 6: Inner Classes

Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006

In addition to the correct answer, you MUST show all your work in order to receive full credit.


CS/ENGRD2110: Final Exam

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

Final Examination CS 125 Introduction to Computer Science Fall Hours

ECE 244 Programming Fundamentals Fall Lab Assignment #5: Binary Search Trees

Project Compiler. CS031 TA Help Session November 28, 2011

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

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Computer Science Foundation Exam

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

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

CSE143 Summer 2008 Final Exam Part A KEY August 21, 2008

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

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

Computer Science E-22 Practice Final Exam

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

ECE264 Fall 2013 Exam 1, September 24, 2013

COM S 211/ENGRD 211 May 15, 2003

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

TREES Lecture 10 CS2110 Spring2014

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

CS 307 Final Fall 2009

Sample Questions for Midterm Exam 2

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

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

Tips from the experts: How to waste a lot of time on this assignment

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Computer Science Foundation Exam

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

Computer Science 62. Midterm Examination

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

RAIK 183H Examination 2 Solution. November 10, 2014

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

CS 307 Final Spring 2011

Data Structures CSCI C343, Fall 2016 [B]

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

Computer Science 1 Ah

COS 126 Midterm 1 Written Exam, Fall 2009

XC Total Max Score Grader

Summer Final Exam Review Session August 5, 2009

CS 314 Final Fall 2011

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

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

Exam 1 - (20 points)

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

Compilers Project 3: Semantic Analyzer

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

RAIK 183H Examination 2 Solution. November 11, 2013

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

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

ECE264 Spring 2013 Final Exam, April 30, 2013

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

CS2110 Assignment 3 Inheritance and Trees, Summer 2008

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

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

Computer Science 136. Midterm Examination

CS1800 Discrete Structures Final Version A

Lecture 16. Lecture

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

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

CS 314 Final Fall 2012

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

CS 307 Final Spring 2008

CS 307 Final Spring 2010

CSC 210, Exam Two Section February 1999

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSE 131 Introduction to Computer Science Fall Exam II

Question: Total Points: Score:

Procedural Abstraction

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

Transcription:

COM S / ENGRD 211 Computers and Programming Summer Session 2004 Prelim I July 12, 2004 Name: CUID: NetID: You have one hour and fifteen minutes to do this exam. All programs must be written in Java. Where applicable, show your work so we can award partial credit for wrong answers. The last page of this exam has a few questions about the course. You can tear this off and fill it out after the exam (which will also preserve anonymity). If you do so, hand it to one of the staff and make sure your bonus points are noted on your exam cover. Problem Score Grader 1. References (10 points) 2. Induction (10 points) 3. Recursion (15 points) 4. DL-Lists (20 points) 5. SL-Lists (20 points) 6. Trees (25 points) TOTAL (100 points) Feedback Form (+ 4 points)

1. References (10 points) Consider the code at the right. Now consider it again carefully. What are the final values for variables w, x, y, and z? In other words, what gets printed by main()? (Partial credit can be given for wrong answers if you give short explanations.) public class A { static void reset(string s) { s = nil ; static String change(string s) { s.replace( r, b ); return s; static String caps(string s) { s = s.touppercase(); return s; public static void main(string args[]) { String w = "ack"; reset(w); String x = "car"; String y = change(x); String z = caps(x); System.out.println(w); System.out.println(x); System.out.println(y); System.out.println(z);

2. Induction (10 points) Use induction to prove the following result. Your answer must state clearly (i) the base case or cases; (ii) the inductive hypothesis; (iii) the inductive step; and (iv) a conclusion. For full credit, you must show your work, including algebra. So show lots of work. 1 1! + 2 2! + 3 3! +... + n n! = ( n + 1)! 1 for all n 1

3. Recursion (15 points) Consider the recursive function hy(a, n, b) defined by hy(a,n,b) = 1 + b if n = 0 a if n = 1, b = 0 a if n > 1, b = 1 hy(a, n-1, hy(a, n, b-1)) otherwise (a) (10 points) Write a public static method that takes three integers a, n, and b as parameters and computes the value of hy(a,n,b), and prints diagnostic output as follows. At the beginning of the method, before any computation is performed, the values of the three parameters are printed. At the end of the method, after all computation is performed and just before it returns, the values of the parameters are printed again along with the return value. For example, calling hy(4, 1, 2) should print invoke hy(4, 1, 2)... other lines deleted... return 6 from hy(4, 1, 2) You may assume that all three parameters are non-negative. (b) (5 points) Show the full output for your method when invoked as hy(4, 1, 2).

This page left empty.

4. Doubly Linked Lists (20 points) Recall the notion of a doubly-linked list without header. One such list is shown below. Each cell is a DListCell instance containing an Object elt, and two DListCell references prev and next. p "3" "2" "4" "1" "0" List containing sequence "3", "2", "4", "1", "0" Write the DListCell class. All fields in DListCell should be private. You must provide enough functionality so that the main() method below works as indicated, and produces the list in the figure above with variable p referring to the cell on the left. At minimum this involves writing constructors, getter routines, and the insert method. Your parameters should match those shown in the code, with the data being an Object. The insert method inserts a single new item after a given item in the list, and has no return value. All your methods may assume that all parameters are non-null, but otherwise they must work for any valid input. E.g., insert must be able to insert a single new cell after the first cell in a list, after the last cell in a list, or after any other cell. public static void main(string args[]) { DListCell p = new DListCell( 0 ); // list p contains only 0 for (int i = 1; i <= 3; i++) { p = new DListCell( +i, p); // create new first cell // list p now contains 3, 2, 1, 0 DListCell.insert(p.getNext(), new DListCell( 4 )); // insert 4 after 2 // list p now contains 3, 2, 4, 1, 0

This page left empty.

5. Singly-Linked Lists (20 points) Write a static method shufflemerge which given two singly-linked lists p and q, merges them together by taking elements alternately from the two lists. First, an element is taken from p, then one from q, then one from p, and so on. If either list is empty (null) or runs out of elements early, the rest of the elements are taken from the other list. An example is shown below, as well as the signatures to the public methods in the ListCell class from lecture. Your method have the following signature: public static ListCell shufflemerge(listcell p, ListCell q); and meet these requirements: The lists are merged in place. That is, you must modify p and q, rather than allocate new list cells. Return a reference to the (possibly-empty) merged list, as well. You will notice that the original reference p is normally returned, or q if p was null. Do not use a header (the List class discussed in lecture) on the lists. No credit will be given if you allocate new cells or a new list that is, do not allocate any new ListCell objects (not even temporary ones). You can use as many method-local variables (e.g, temporary references) as you need. Do not use any static class variables or other helper methods. All of your code must be contained within shufflemerge. Hint: Partial credit will be given if you can at least give an outline of an algorithm for solving this problem. It can be solved recursively in as few as 3 or 4 lines of Java code. There are also straightforward iterative solutions. reference p passed to shufflemerge reference q passed to shufflemerge "3" "2" "1" "0" reference returned from shufflemerge (same as original refrence p) a" "3" b" 2" "1" "0" a" b" Shuffle-merging two lists in-place.

public class ListCell { // signatures of all public methods and constructors public ListCell(Object elt, ListCell next); // new cell with given elt and next public Object getelt( ); // get the data from this cell public ListCell getnext(); // get the next cell after this one public void setelt(object elt); // set the data for this cell public void setnext(listcell next); // set the next pointer to a different cell

This page left empty.

6. Trees (25 points) Here is a modified version of the grammar for simple arithmetic expressions given on the homework: Expression : ( Expression + Expression ) Expression : integer Expression : variable // a character in the range a z or A Z We can represent any expression in this grammar by an abstract syntax tree (AST). For example, the expression (x + (3 + 4)) is represented by the binary tree below (at left). In this example, the sub-expression (3 + 4) is constant, and so we might like to optimize the AST by replacing the right sub-tree by a single node. + x + optimize + x 7 3 4 Write a static method optimize which, when given an AST, produces an optimized AST with no constant sub-expressions. The optimized and original AST should not share any storage in common, with the possible exception of (immutable) String objects. That is, you should construct an entirely new tree in your method, and should not modify the original tree in any way. You can also make new copies of the String objects, if you like. The AST is implemented using the TreeCell class, but modified to hold Strings instead of Objects. Each variable, integer, and operation will be stored in the TreeCell as a String instance. You may assume that the original AST is not null, and that each of its TreeCells contain a valid string: either +, a single letter, or an integer. The public methods signatures for the TreeCell class are given below, with other helpful methods from Character and Integer. You are only responsible for writing the optimize method. Hints: An expression with no sub-expressions, or one that is empty, by definition has no constant sub-expressions. So in these cases there is no real work to be done. One way to optimize an expression that does have sub-expressions is as follows: first optimize left and sub-expressions; then, examine the root node and its left child and right child to see if the whole tree can be optimized away.

public class TreeCell { // signatures of all public methods and constructors // construct TreeCell with no children public TreeCell(String elt); // construct TreeCell with given children public TreeCell(String elt, TreeCell left, TreeCell right); public String getelt(); // get element at this node public TreeCell getleft(); // get left child public TreeCell getright(); // get right child public class Character { // signatures of some helpful methods static boolean isletter(char c); // determine if c is a letter static boolean isdigit(char c); // determine if c is a digit public class Integer { // signatures of some helpful methods static int parseint(string s); // parse s as an signed decimal integer

This page left empty.

Course Feedback (1 point per question answered) You may tear off this sheet and give it to the course staff to maintain anonymity. You can also take as much extra time as you need to fill this out. 1. How many hours per week are you spending on the assignments for this course? 2. Do you attend lectures regularly? What can we do to improve the lectures? 3. Do you see the consultants or TAs for help? What can we do to improve consulting? 4. Any other comments? (Assignments, prelim, etc.)