CSE 143 Sample Final Exam #3

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

CSE 143 Sample Final Exam #1

CSE 143 Sample Final Exam #5

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

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

Facebook. / \ / \ / \ Accenture Nintendo

CSE 143: Computer Programming II Autumn Sample Solutions

CSE 143 Sample Midterm Exam #4

Facebook. / \ / \ / \ Accenture Nintendo

CSE 143: Computer Programming II Spring 2015 Final Exam Solutions

CSE 143 Section Handout #13 Practice Midterm #5

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

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

CSE 143 Practice Final Exam #0

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

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

CSE 143 SAMPLE MIDTERM

CSE 143, Winter 2009 Sample Midterm Exam #2

Building Java Programs

CS 106X Sample Final Exam #2

CSE 143 Sample Midterm Exam #1

CSE 143 SAMPLE MIDTERM SOLUTION

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

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CSE 143 Sample Midterm Exam #7 (11wi)

CSE 143: Computer Programming II Spring 2015 Midterm Exam Solutions

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

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

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

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

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

CSc 110 Sample Midterm Exam #2

Summer Final Exam Review Session August 5, 2009

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

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

Midterm Exam 2 CS 455, Fall 2014

CSE 214 Computer Science II Heaps and Priority Queues

CSE 143 Lecture 22. The Object class; Polymorphism. read slides created by Marty Stepp and Ethan Apter

Section 03: Asymptotic Analysis

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

INF2220: algorithms and data structures Series 1

Data Structures and Algorithms Winter term 2016

Second Examination Solution

Model Solutions. COMP 103: Test May, 2013

Computer Science 302 Spring 2007 Practice Final Examination: Part I

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

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

AP Computer Science AB 2007 Free-Response Questions

CompuScholar, Inc. 9th - 12th grades

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

Comp Intermediate Programming EXAM #2 April 03, 2002 Rice University - Instructors: Cox & Nguyen

Section: Inheritance Heck

ECE 242. Data Structures

PRACTICE MIDTERM EXAM #2

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

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CSc 335 Inheritance Hell

Prelim 2, CS2110. SOLUTION

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

Algorithms. Deleting from Red-Black Trees B-Trees

TeenCoder : Java Programming (ISBN )

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

EXAMINATIONS 2016 TRIMESTER 2

CSE 143X. Accelerated Computer Programming I/II

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

CS 314 Exam 2 Spring 2016

CSCI 200 Lab 11 A Heap-Based Priority Queue

Linked Lists. References and objects

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

CSE 143 Sample Midterm Exam #8 (12wi)

Maximum Grade: 5 points out of 10 of the exam.

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

CS 314 Exam 2 Fall 2017

CmpSci 187: Programming with Data Structures Spring 2015

Solution to CSE 250 Final Exam

Comp Intermediate Programming EXAM #2 March 30, 2005 Rice University - Instructors: Cox & Nguyen

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

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Dartmouth College Computer Science 10, Fall 2014 Solution to Final Exam A

APCS Unit 5 Exam. Assuming all four classes have a default constructor, which of the following statements would result in an error from the compiler?

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Introduction to Computing II (ITI 1121) Final Examination

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

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

a graph is a data structure made up of nodes in graph theory the links are normally called edges

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

1. (9 points) Recall the definition of the ListNode class: public class ListNode { int data; ListNode next; }

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

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

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

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

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

1) Holiday Lights 30 Points

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

CSIS 10B Lab 2 Bags and Stacks

Language Features. 1. The primitive types int, double, and boolean are part of the AP

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

CSE 2123 Recursion. Jeremy Morris

Transcription:

CSE 143 Sample Final Exam #3 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. Inheritance and Comparable Programming. You have been asked to extend a pre-existing class Person that represents a person used as part of an online dating/marriage system. 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.

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 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) {...

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 state of the elements of the array below after each of the first 3 passes of the outermost loop of the selection sort algorithm. 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);

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 tree above in the order they would be visited by each kind of traversal: Pre-order: In-order: Post-order:

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: Level 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. 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

7. Binary Tree Programming. Write a method trim that could be added to the IntTree class from lecture and section. The method accepts minimum/maximum integers as parameters and removes from the tree any elements that are not in that range, inclusive. For this method, 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.