CSE 143, Winter 2009 Final Exam Thursday, March 19, 2009

Similar documents
CSE 143 Sample Final Exam #3

CSE 143, Winter 2010 Final Exam Thursday, March 18, 2010

CSE 143 Sample Final Exam #1

CSE 143 Sample Final Exam #5

CSE 143: Computer Programming II Autumn Sample Solutions

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010

1. Binary Tree Traversal. 2. Binary Search Tree. 1 of 12. Consider the following tree: Pre-order: In-order: Post-order:

CSE 143: Computer Programming II Spring 2015 Final Exam Solutions

Facebook. / \ / \ / \ Accenture Nintendo

Facebook. / \ / \ / \ Accenture Nintendo

CSE 143 Sample Midterm Exam #4

CSE 143: Computer Programming II Spring 2015 Midterm Exam Solutions

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

CSE 143 Section Handout #13 Practice Midterm #5

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

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

CSE 142, Summer 2013 Midterm Exam, Friday, July 26, 2013

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

CSE 143 Practice Final Exam #0

CSE 143, Winter 2009 Sample Midterm Exam #2

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

CSE 143 SAMPLE MIDTERM

CSE 142, Spring 2010 Final Exam Wednesday, June 9, Name: Student ID #: Rules:

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

CSE 373, Winter 2013 Final Exam, Tuesday, March 19, Good luck! You can do it!

CSE 142, Autumn 2010 Final Exam Wednesday, December 15, Name:

CSE 142, Autumn 2011 Midterm Exam: Friday, November 4, 2011

CSE 142, Autumn 2010 Midterm Exam, Friday, November 5, 2010

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

CSE 142, Summer 2010 Midterm Exam, Friday, July 30, 2010

CS 106X Sample Final Exam #2

CSE 143 Sample Midterm Exam #1

CSE 142, Winter 2007 Final Exam. Name:

CSE 143 Final Part 1, August 18, 2011 Sample Solution

CSE 143 SAMPLE MIDTERM SOLUTION

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

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

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

CSE 142, Spring 2009, Final Exam

Building Java Programs

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

CSE 143 Sample Midterm Exam #7 (11wi)

Second Examination Solution

Data Structures and Algorithms Winter term 2016

Prelim 2. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

Total Score /1 /20 /41 /15 /23 Grader

XC Total Max Score Grader

CSE 143 Lecture 16. Binary Search Trees. read slides adapted from Marty Stepp and Hélène Martin

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

Midterm Exam 2 CS 455, Fall 2014

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

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

UNIVERSITY OF TORONTO Faculty of Arts and Science

TeenCoder : Java Programming (ISBN )

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

CS 61B (Clancy, Yelick) Solutions and grading standards for exam 2 Spring 2001 Exam information

CSE 303, Spring 2009 Final Exam Wednesday, June 10, 2009

CSE 143: Computer Programming II Winter 2019 HW6: AnagramSolver (due Thursday, Feb 28, :30pm)

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CS 314 Exam 2 Fall 2017

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

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

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

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

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

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

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

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

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

ECE 242. Data Structures

CS106X Handout 35 Winter 2018 March 12 th, 2018 CS106X Midterm Examination

CSc 110 Sample Midterm Exam #2

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

CS 151 Name Final Exam December 17, 2014

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

CS106X Final Review. Rachel Gardner and Jared Bitz. slides adapted from Ashley Taylor, Anton Apostolatos, Nolan Handali, and Zachary Birnholz

CSE 143, Winter 2013 Programming Assignment #8: Huffman Coding (40 points) Due Thursday, March 14, 2013, 11:30 PM

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

CompuScholar, Inc. 9th - 12th grades

CS 314 Exam 2 Spring 2018

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

CS 314 Exam 2 Spring 2016

Points off Total off Net Score. CS 314 Final Exam Spring Your Name Your UTEID

Model Solutions. COMP 103: Test May, 2013

Summer Final Exam Review Session August 5, 2009

Section 03: Asymptotic Analysis

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

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

CSE 214 Computer Science II Heaps and Priority Queues

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

CSE 143: Computer Programming II Summer 2015 HW6: 20 Questions (due Thursday, August 13, :30pm)

Problem Description Earned Max 1 PHP 25 2 JavaScript / DOM 25 3 Regular Expressions 25 TOTAL Total Points 100. Good luck! You can do it!

Solution to CSE 250 Final Exam

Created by Julie Zelenski, with edits by Jerry Cain, Keith Schwarz, Marty Stepp, Chris Piech and others.

PRACTICE MIDTERM EXAM #2

Note that if both p1 and p2 are null, equals returns true.

Transcription:

CSE 143, Winter 2009 Final Exam Thursday, March 19, 2009 Personal Information: Name: Section: Student ID #: TA: You have 110 minutes to complete this exam. You may receive a deduction if you keep working after the instructor calls for papers. This exam is open-book/notes. You may not use any computing devices including calculators. Code will be graded on proper behavior/output and not on style, unless otherwise indicated. Do not abbreviate code, such as "ditto" marks or dot-dot-dot... marks. The only abbreviations that are allowed for this exam are: S.o.p for System.out.print, and S.o.pln for System.out.println. You do not need to write import statements in your code. If you enter the room, you must turn in an exam before leaving the room. You must show your Student ID to a TA or instructor for your exam to be accepted. Good luck! Score summary: (for grader only) Problem Description Earned Max 1 Inheritance/Polymorphism 15 2 Inheritance/Comparable Programming 15 3 Linked List Programming 15 4 Searching and Sorting 15 5 Binary Search Trees 15 6 Binary Tree Programming 10 7 Binary Tree Programming 15 X Extra Credit +1 TOTAL Total Points 100 1 of 11

1. Inheritance and Polymorphism Consider the following classes (System.out.println has been abbreviated as S.o.pln): public class Leo extends Don { public void method1() { S.o.pln("Leo 1"); public void method3() { S.o.pln("Leo 3"); method1(); public class Mike extends Leo { public void method2() { S.o.pln("Mike 2"); super.method2(); public class Raph { public void method1() { S.o.pln("Raph 1"); public class Don extends Raph { public void method2() { method1(); S.o.pln("Don 2"); In the table below, indicate in the right-hand column the output produced by the statement in the left-hand column. If the statement produces more than one line of output, indicate the line breaks with slashes as in "a / b / c" to indicate three lines of output with "a" followed by "b" followed by "c". If the statement causes an error, fill in the right-hand column with the phrase "error" to indicate this. Statement var1.method1(); var1.method2(); var1.method3(); var2.method1(); var2.method2(); var2.method3(); var3.method1(); var4.method1(); var4.method2(); var4.method3(); ((Don) var1).method2(); ((Mike) var2).method2(); ((Raph) var3).method1(); ((Don) var3).method2(); ((Leo) var4).method3(); Output The following variables are defined: Raph var1 = new Don(); Leo var2 = new Mike(); Object var3 = new Raph(); Don var4 = new Leo(); 2 of 11

2. Inheritance and Comparable Programming You have been asked to extend a pre-existing class Person that represents a person used as part of a Gale-Shapley stable marriage program (as seen in your HW3). The Person class includes the following constructors and methods: Constructor/Method public Person(String name) public String getname() public void engageto(person partner) public Person getfiancee() public boolean issingle() public Queue<String> getpreferences() public Map<String, Integer> getrankings() public String tostring() Description constructs a person with the given name returns the person's name sets the person to be engaged to the given partner returns the person's fiancée, or null if none returns true if the person has no fiancée returns a queue of preferred partners returns a map of rankings returns a string representation of the person You are to define a new class called Playa that extends this class through inheritance. A Playa should behave like a Person except that it makes mischief by allowing itself to be engaged to multiple persons at the same time, keeping track of a collection of all such fiancées. You should provide the same methods as the superclass, as well as the following new behavior. Constructor/Method public Playa(String name) public int countfiancees() Description constructs a playa with the given name returns the number of fiancées to which this playa is currently engaged The behaviors related to preferences and rankings of potential partners are unaffected by this subclass. Some of the existing behaviors from Person should behave differently on Playa objects: When the engageto method is called, the Playa should still retain the existing engageto behavior (because it maintains important internal state), but it should also keep track of a collection of all engagement partners seen so far. Each partner passed to engageto should become part of this collection. It should not be possible for the same person to appear twice in this collection. If null is passed to engageto, your Playa should instead clear its collection of partners to become single again. (A Playa can become engaged to any person(s), not just other Playas.) The getfiancee method should return the partner to which the Playa most recently became engaged (not counting null). This will occur automatically if the original engageto behavior from Person is retained. The issingle method should return true only if the Playa has no partners in its engagement collection. You must also make Playa objects comparable to each other using the Comparable interface. Playas are compared by their number of fiancées, breaking ties by name. In other words, a Playa object with fewer fiancées in its partner collection is considered to be "less than" one with more fiancées in its collection. If two Playa objects have the same number of fiancées, the one whose name comes first in alphabetical order is considered "less." If the two objects have the same number of fiancées and the same name, they are considered to be "equal." The majority of your grade comes from implementing the correct behavior. Part of your grade also comes from appropriately utilizing the behavior you have inherited from the superclass and not re-implementing behavior that already works properly in the superclass. (Write your answer on the next page.) 3 of 11

2. Inheritance and Comparable Programming (writing space) 4 of 11

3. Linked List Programming Write a method expand that could be added to the LinkedIntList class from lecture and section. The method accepts an integer f as a parameter and replaces every element value i with f copies of the value (i / f). Suppose a LinkedIntList variable list stores the following values: [21, 8, 15, 0, -3, 32] The call list.expand(3); would change the list to store the following elements: [7, 7, 7, 2, 2, 2, 5, 5, 5, 0, 0, 0, -1, -1, -1, 10, 10, 10] If an element of the original list is not evenly divisible by f, as with 8 and 32 above, the resulting list should truncate any fractional component (as is done naturally by integer division). If the parameter value passed is 1, the list is unchanged. If it is 0 or negative, the list should become empty. For full credit, your solution must run in O(N) time where N is the length of the list. You may not call any methods of the linked list class to solve this problem, and you may not use any auxiliary data structure to solve this problem (such as an array, ArrayList, Queue, String, etc). Assume that you are using the LinkedIntList and ListNode class as defined in lecture and section: public class LinkedIntList { private ListNode front; methods public class ListNode { public int data; // data stored in this node public ListNode next; // a link to the next node in the list public ListNode() {... public ListNode(int data) {... public ListNode(int data, ListNode next) {... 5 of 11

4. Searching and Sorting (a) Suppose we are performing a binary search on a sorted array called numbers initialized as follows: // index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 int[] numbers = {-2, 0, 1, 7, 9, 16, 19, 28, 31, 40, 52, 68, 85, 99; // search for the value 5 int index = binarysearch(numbers, 5); Write the indexes of the elements that would be examined by the binary search (the mid values in our algorithm's code) and write the value that would be returned from the search. Assume that we are using the binary search algorithm shown in lecture and section. Indexes examined: Value Returned: (b) Write the elements of the array below after each of the first 3 passes of the outermost loop of a selection sort. int[] numbers = {63, 9, 45, 72, 27, 18, 54, 36; selectionsort(numbers); (c) Trace the complete execution of the merge sort algorithm when called on the array below, similarly to the example trace of merge sort shown in the lecture slides. Show the sub-arrays that are created by the algorithm and show the merging of sub-arrays into larger sorted arrays. int[] numbers = {63, 9, 45, 72, 27, 18, 54, 36; mergesort(numbers); 6 of 11

5. Binary Search Trees (a) Write the binary search tree that would result if these elements were added to an empty tree in this order: Meg, Stewie, Peter, Joe, Lois, Brian, Quagmire, Cleveland (b) Write the elements of your above tree in the order they would be visited by each kind of traversal: Pre-order: In-order: Post-order: 7 of 11

6. Binary Tree Programming Write a method nodesatlevels that could be added to the IntTree class from lecture and section. The method accepts minimum and maximum integers as parameters and returns a count of how many elements exist in the tree at those levels, inclusive. Recall that the root of a tree is at level 1, its children are at level 2, their children at level 3, and so on. The table below shows the results of several calls on an IntTree variable tree: Levels tree Call Value Returned +----+ 1 20 tree.nodesatlevels(2, 4) 8 +----+ / \ tree.nodesatlevels(4, 5) 6 +----+ +----+ 2 76 11 +----+ +----+ tree.nodesatlevels(1, 2) 3 / \ / 3 24 42 98 tree.nodesatlevels(3, 3) 3 / \ \ tree.nodesatlevels(7, 9) 0 4-8 80-2 tree.nodesatlevels(5, 9) 3 \ / \ tree.nodesatlevels(1, 1) 1 5 57 1 27 For example, tree.nodesatlevels(4, 5) returns 6 because -8, 80, -2, 57, 1, and 27 are in that range of levels. Your method should throw an IllegalArgumentException if the minimum passed is less than 1 or is greater than the maximum passed. It is legal for the minimum and/or maximum to be larger than the height of the tree; a tree has 0 nodes at any levels that exceed its height. An empty tree contains 0 nodes at any level. You may define private helper methods to solve this problem, but otherwise you may not call any other methods of the class nor create any data structures such as arrays, lists, etc. Your method should not change the structure or contents of the tree being examined. (The contents of the IntTree and IntTreeNode classes are shown on the next page for reference.) 8 of 11

7. Binary Tree Programming Write a method trim that could be added to the IntTree class from lecture and section. The method accepts minimum and maximum integers as parameters and removes from the tree any elements that are not within that range, inclusive. For this method you should assume that your tree is a binary search tree (BST) and that its elements are in valid BST order. Your method should maintain the BST ordering property of the tree. For example, suppose a variable of type IntTree called tree stores the following elements: tree +----+ 50 +----+ / \ +----+ +----+ 38 90 +----+ +----+ / \ / 14 42 54 / \ \ 8 20 72 \ / \ 26 61 83 The table below shows what the state of the tree would be if various trim calls were made. The calls shown are separate; it's not a chain of calls in a row. You may assume that the minimum is less than or equal to the maximum. tree.trim(25, 72); tree.trim(54, 80); tree.trim(18, 42); tree.trim(-3, 6); 50 54 38 _ +----+ +----+ _ +----+ / \ \ / \ +----+ +----+ 38 54 72 20 42 +----+ +----+ / \ \ / \ +----+ +----+ 26 42 72 61 26 +----+ +----+ / +----+ 61 +----+ Hint: The BST ordering property is important for solving this problem. If a node's data value is too large or too small to fit within the range, this may also tell you something about whether that node's left or right subtree elements can be within the range. Taking advantage of such information makes it more feasible to remove the correct nodes. You may define private helper methods to solve this problem, but otherwise you may not call any other methods of the class nor create any data structures such as arrays, lists, etc. (Write your answer on the next page.) Recall the IntTree and IntTreeNode classes as shown in lecture and section: public class IntTreeNode { public int data; // data stored in this node public IntTreeNode left; // reference to left subtree public IntTreeNode right; // reference to right subtree public IntTreeNode(int data) {... public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) {... public class IntTree { private IntTreeNode overallroot; methods 9 of 11

7. Binary Tree Programming (writing space) 10 of 11

X. Extra Credit Draw a picture of your TA using a data structure in his/her everyday life. For example, you could draw a picture of the TA eating a Stack of pancakes. Make sure to put the TA's name somewhere in the picture so we know who it is. (This is not an art class, so any drawing that appears to reflect more than a few moments' effort will get the point.) 11 of 11