CSE 143: Computer Programming II Spring 2015 Final Exam Solutions

Similar documents
CSE 143: Computer Programming II Autumn Sample Solutions

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

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

CSE 143: Computer Programming II Spring 2015 Midterm Exam Solutions

CSE 143 Sample Final Exam #3

Facebook. / \ / \ / \ Accenture Nintendo

CSE 143 Sample Final Exam #1

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

CSE 143 Practice Final Exam #0

CSE 143 Sample Final Exam #5

Facebook. / \ / \ / \ Accenture Nintendo

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

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

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

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

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

CSE 143 SAMPLE MIDTERM

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

ITI Introduction to Computing II

ITI Introduction to Computing II

CSE 143 SAMPLE MIDTERM SOLUTION

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

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

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

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

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

Building Java Programs

CSE 143, Winter 2009 Sample Midterm Exam #2

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

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

We have the pointers reference the next node in an inorder traversal; called threads

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

Solution to Test of Computer Science 203x Level Score: / 100 Time: 100 Minutes

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

CSE 142, Winter 2007 Final Exam. Name:

CSE 143 Sample Midterm Exam #4

CSE 143 Sample Final Exam #6 Key

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

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

CSE 214 Computer Science II Heaps and Priority Queues

Name CPTR246 Spring '17 (100 total points) Exam 3

Points off Total off Net Score. CS 314 Final Exam Spring 2017

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

CSE 143 Sample Midterm Exam #1

CSE 142, Spring 2009, Final Exam

CS 314 Exam 2 Spring 2016

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

Computer Sciences 302 Exam 2 Information & Sample Exam

CS 314 Exam 2 Fall 2017

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

Introduction to Computer Science II (ITI 1121) Final Examination

CS 314 Exam 1 Fall 2016

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

CSE 143 Section Handout #13 Practice Midterm #5

This exam is open book. Each question is worth 3 points.

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

Linked Lists. References and objects

Final Exam. COMP Summer I June 26, points

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

CS 314 Exam 2 Spring

CSE 143 Sample Midterm Exam #7 (11wi)

CS 101 Exam 2 Spring Id Name

Today s Goals 2. Get familiar with the idea of references (things that point to objects) So far, the only real data structure we ve seen is arrays

1. AVL Trees (10 Points)

CSE 143X. Accelerated Computer Programming I/II

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

CS 206 Introduction to Computer Science II

Section 01: Solutions

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

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

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

CSE 143. Computer Programming II

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

Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes O(n) time in a binary

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

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

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

Introduction to Computing II (ITI 1121) Final Examination

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

Final Examination CSE 100 UCSD (Practice)

CSE 1223: Exam II Autumn 2016

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Good Luck! CSC207, Fall 2012: Quiz 3 Duration 25 minutes Aids allowed: none. Student Number: Lecture Section: L0101. Instructor: Horton

CS171 Midterm Exam. October 29, Name:

Data Structures and Algorithms Winter term 2016

CSCI 1226 A Test #1. Wednesday, 10 October, 2018 Name: Student #: General Instructions Read and follow all directions carefully.

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

C Sc 227 Practice Final Summer 13 Name 200 pts

Computer Science II Fall 2009

C Sc 227 Practice Final Summer 12 Name 200pt

1 / 20 2 / 20 3 / 20 4 / 20 5 / 20

Transcription:

CSE 143: Computer Programming II Spring 2015 Final Exam Solutions Name: Sample Solutions ID #: 1234567 TA: The Best Section: A9 INSTRUCTIONS: You have 110 minutes to complete the exam. You will receive a deduction if you keep working after the instructor calls for papers. This exam is closed-book and closed-notes. calculators. You may not use any computing devices including 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. You may not use scratch paper on this exam. If you need extra space, use the back of a page. 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. If you get stuck on a problem, move on and come back to it later. Problem Points Score Problem Points Score 1 6 5 13 2 4 6 15 3 8 7 20 4 14 8 20 Σ 100 Page 1 of 10

Mechanical Missions. This section tests whether you are able to trace through code of various types in the same way a computer would. 1. I m Rooting For You! [6 points] Write the elements of the tree in the order they would be seen by pre-order, in-order, and post-order traversals. 1 2 3 4 5 10 7 8 6 9 Pre-order: 1, 2, 4, 7, 8, 3, 5, 10, 6, 9 In-order: 2, 7, 4, 8, 1, 5, 3, 9, 6, 10 Post-order: 7, 8, 4, 2, 5, 9, 6, 10, 3, 1 Page 2 of 10

2. I Do Not Like Them [4 points] Draw a picture of the binary search tree that would result from inserting the following words into an empty binary search tree in the following order: (1) Horton (2) SamIAm (3) Grinch (4) Lorax (5) Gertrude (6) CatInTheHat (7) Sneetch Solution: Grinch Gertrude CatInTheHat Horton SamIAm Lorax Sneetch Page 3 of 10

3. Collections Mystery [8 points] 1 public static void mystery(list<integer> list) { 2 Map<Integer, Integer> result = new TreeMap<Integer, Integer>(); 3 Iterator<Integer> it = list.iterator(); 4 5 while (it.hasnext()) { 6 int j = it.next(); 7 if (it.hasnext()) { 8 int k = it.next(); 9 result.put(k, j); 10 result.put(j, k); 11 12 13 System.out.println(result); 14 For each of the following, fill in the output printed when mystery is called on the given Collection. Input Collection Output (a) [1, 1] {1=1 (b) [200, 200, 300, 400] {200=200, 300=400, 400=300 (c) [1, 2, 1, 2] {1=2, 2=1 (d) [9, 8, 7, 6, 5, 4] {4=5, 5=4, 6=7, 7=6, 8=9, 9=8 Page 4 of 10

4. Polymorphism Mystery [14 points] Consider the following classes: What does each of these lines output? 1 public class Ninja extends Robot { 2 public void method1() { 3 System.out.println("Ninja 1"); 4 5 6 public void method3() { 7 System.out.println("Ninja 3"); 8 9 10 11 public class Pirate { 12 public void method1() { 13 System.out.println("Pirate 1"); 14 15 16 public void method2() { 17 System.out.println("Pirate 2"); 18 this.method1(); 19 20 21 22 public class Robot extends Pirate { 23 public void method1() { 24 System.out.println("Robot 1"); 25 super.method1(); 26 27 28 29 public class Alien extends Pirate { 30 public void method3() { 31 System.out.println("Alien 3"); 32 33 Consider the following variables: 1 Pirate var1 = new Ninja(); 2 Pirate var2 = new Robot(); 3 Pirate var3 = new Pirate(); 4 Object var4 = new Robot(); 5 Robot var5 = new Ninja(); 6 Object var6 = new Alien(); Statement Output (a) var1.method1(); Ninja 1 (b) var2.method1(); Robot 1 / Pirate 1 (c) var3.method1(); Pirate 1 (d) var4.method1(); error (e) var5.method1(); Ninja 1 (f) var2.method2(); Pirate 2 / Robot 1 / Pirate 1 (g) var3.method2(); Pirate 2 / Pirate 1 (h) var6.method2(); error (i) ((Robot)var1).method3(); error (j) ((Alien)var6).method3(); Alien 3 (k) ((Ninja)var4).method1(); error (l) ((Pirate)var6).method2(); Pirate 2 / Pirate 1 (m) ((Pirate)var4).method1(); Robot 1 / Pirate 1 (n) ((Ninja)var3).method3(); error Page 5 of 10

Programming Pursuits. This section tests whether you synthesized various topics well enough to write novel programs using those topics. 5. Daisy, Daisy... [13 points] Define a class Bicycle that represents a bicycle. Your class should have the following public methods: Bicycle(fixedWheel, gears) isfixedwheel() getspeed() changespeed(howmuch) Constructs a Bicycle based on if it is a fixed-wheel bicycle or not and how many gears it has. All bicycles should store the current speed which should be initialized to 1.0mph. This constructor should throw an IllegalArgumentException if fixedwheel is true and gears is not equal to one. It should also throw an IllegalArgumentException if gears is less than 1. This method returns true if the bicycle is a fixed-wheel bicycle and false otherwise. This method returns the current speed of the bicycle. This method takes in a multiplier for how much more or less the bicyclist is pedaling. If s is the old speed, then the new speed should be howmuch * s. tostring() If the bicycle is a fixed-wheel, prepend Fixie. Otherwise, prepend <gears>-gear bicycle. Then, the remainder of the result should be (speed: <speed>mph). Below are some examples of Bicycles: 1 Bicycle fixie = new Bicycle(true, 1); 2 Bicycle bike = new Bicycle(false, 8); Example Output Method Call fixie.isfixedwheel() Return Value true fixie.getspeed() 1.0 fixie.changespeed(1.55444) fixie.getspeed() 1.55444 fixie.tostring() Fixie (speed: 1.55444mph) bike.changespeed() 0.155 bike.tostring() 8-gear bicycle (speed: 0.155mph) We would like to compare bikes by coolness factor. So, your Bicycle class should implement the Comparable interface. Cooler bicycles should be considered greater. Implement the Comparable<E> interface by considering aspects in the following order: Fixed-wheel bicycles are the coolest. Otherwise, faster bicycles are cooler. Otherwise, bicycles with more gears are cooler. Page 6 of 10

Solution: 1 public class Bicycle implements Comparable<Bicycle> { 2 private boolean fixedwheel; 3 private int gears; 4 private double speed; 5 6 public Bicycle(boolean fixedwheel, int gears) { 7 if (fixedwheel && gears!= 1 gears < 1) { 8 throw new IllegalArgumentException(); 9 10 this.fixedwheel = fixedwheel; 11 this.gears = gears; 12 this.speed = 1.0; 13 14 15 public boolean isfixedwheel() { 16 return this.fixedwheel; 17 18 19 public double getspeed() { 20 return this.speed; 21 22 23 public void changespeed(double howmuch) { 24 this.speed = howmuch; 25 26 27 public String tostring() { 28 if (this.fixedwheel) { 29 return "Fixie (speed: " + this.speed + " mph)"; 30 31 else { 32 return this.gears + " gear bicycle (speed: " + this.speed + " mph)"; 33 34 35 36 public int compareto(bicycle other) { 37 if (this.fixedwheel &&!other.fixedwheel) { 38 return 1; 39 40 else if (!this.fixedwheel && other.fixedwheel) { 41 return 1; 42 43 44 int result = ((Double)this.speed).compareTo(other.speed); 45 if (result == 0) { 46 result = ((Integer)this.gears).compareTo(other.gears); 47 48 49 return result; 50 51 Page 7 of 10

6. Oddly Even [15 points] Write an IntTree method counttolerantparents that returns the number of nodes that have exactly one even child and exactly one odd child. For example, if a variable t stores a reference to the following tree: t.root 1 2 3 8 4 9 6 5 10 7 A call to t.counttolerantparents() should return 3, because 1, 4, 3 all have one even child and one odd child. public class IntTree { private class IntTreeNode { public final 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) {... public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) {... private IntTreeNode root; // Write Your Solution Here Solution: 1 public int counttolerantparents() { 2 return counttolerantparents(this.root); 3 4 5 private int counttolerantparents(inttreenode current) { 6 if (current == null) { 7 return 0; 8 9 else if (current.left!= null && current.right!= null && 10 (current.left.data % 2!= current.right.data % 2)) { 11 return 1 + counttolerantparents(current.left) 12 + counttolerantparents(current.right); 13 14 else { 15 return counttolerantparents(current.left) + 16 counttolerantparents(current.right); 17 18 Page 8 of 10

7. Can You Do The Limbo? [20 points] Write an IntTree method compresstree that compresses a tree by removing all parents that have exactly one child from the tree. public class IntTree { private class IntTreeNode { public final 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) {... public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) {... private IntTreeNode root; // Write Your Solution Here Solution: 1 public void compresstree() { 2 this.root = compresstree(this.root); 3 4 5 private IntTreeNode compresstree(inttreenode current) { 6 if (current!= null) { 7 if (current.left!= null && current.right == null) { 8 current = compresstree(current.left); 9 10 else if (current.left == null && current.right!= null) { 11 current = compresstree(current.right); 12 13 else { 14 current.left = compresstree(current.left); 15 current.right = compresstree(current.right); 16 17 18 return current; 19 Page 9 of 10

8. Wibble Wobble [20 points] Write a LinkedIntList method wobble that rearranges a list by moving all the values in even-numbered positions to the end of the list (and otherwise preserving list order). If the list is empty or only has one element, the list should remain unchanged. Example Output Before list.wobble() After list.wobble() [1, 2, 3, 4] [2, 4, 1, 3] [2, 4, 6, 8] [4, 8, 2, 6] [3, 5, 7, 9, 11] [5, 9, 3, 7, 11] public class LinkedIntList { private class ListNode { public final int data; // data stored in this node public ListNode next; // reference to the next node public ListNode(int data) {... public ListNode(int data, ListNode next) {... private ListNode front; // Write Your Solution Here Solution: 1 public void wobble() { 2 if (front!= null && front.next!= null) { 3 ListNode otherfront = front; 4 ListNode otherback = front; 5 front = front.next; 6 ListNode current = front; 7 while (current.next!= null && current.next.next!= null) { 8 otherback.next = current.next; 9 otherback = current.next; 10 current.next = current.next.next; 11 current = current.next; 12 13 if (current.next!= null) { 14 otherback.next = current.next; 15 otherback = current.next; 16 17 current.next = otherfront; 18 otherback.next = null; 19 20 Page 10 of 10