CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

Similar documents
CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

CS 455 Midterm Exam 1 Fall 2013 [Bono] Wednesday, Oct. 2, 2013

CS 455 Midterm Exam 1 Spring 2011 [Bono] Feb. 17, 2011

Midterm Exam 2 CS 455, Spring 2011

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

Midterm Exam 2 CS 455, Spring 2014

CS 455 Midterm Exam 1 Spring 2011 [Bono] Feb. 17, 2011

CS 455 Midterm Exam 1 Spring 2013 [Bono] Feb. 21, 2013

CS 455 Final Exam Fall 2016 [Bono] December 13, 2016

Midterm Exam 2 CS 455, Fall 2012

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

CS 455 Final Exam Fall 2013 [Bono] December 12, 2013

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

Midterm Exam 2 CS 455, Fall 2013

Midterm Exam 2 CS 455, Spring 2015

CS 455 Midterm Exam 1 Spring 2015 [Bono] Thursday, Feb. 19, 2015

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

Midterm Exam 2 CS 455, Fall 2014

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

Midterm Exam 2 CS 455, Spring 2013

CS 455 Final Exam Spring 2017 [Bono] May 10, 2017

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

CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015

ArrayList; Names example

CS 455 Final Exam Spring 2015 [Bono] May 13, 2015

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

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CS 455 Midterm Exam 2 Spring 2017 [Bono] Tuesday, April 4, 2017

Recursion; Side effects

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

Final Exam CS 152, Computer Programming Fundamentals May 9, 2014

University of Massachusetts Amherst, Electrical and Computer Engineering

More on class design

CS 455 MT 2 Sample Questions

CS 113 MIDTERM EXAM 2 SPRING 2013

CS 116 Week 8 Page 1

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CPS 6 Test 1 Fall 2000 Dr. Rodger String class

Introduction to Programming System Design CSCI 455x (4 Units)

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

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

CS 139 Practice Midterm Questions #2

Project Compiler. CS031 TA Help Session November 28, 2011

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

CS1004: Intro to CS in Java, Spring 2005

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

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

ECE242 Data Structures and Algorithms Fall 2008

Core Competency DOO (Design Object-Oriented)

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

CS 10, Fall 2015, Professor Prasad Jayanti

CIS 110 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

Final Exam. COMP Summer I June 26, points

CIS 162 Project 4 Farkle (a dice game)

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

Java Outline (Upto Exam 2)

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

CIT Special final examination

CS2102, D15 Exam 1. Name:

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

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

COS 126 General Computer Science Spring Written Exam 1

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

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)

CS 216 Exam 1 Fall SOLUTION

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications

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

CSE413 Midterm. Question Max Points Total 100

Midterm Exam CS 251, Intermediate Programming October 8, 2014

CSC 222: Object-Oriented Programming. Fall 2015

Inheritance and Interfaces

CMPSCI 145 MIDTERM #2 SPRING 2017 April 7, 2017 Professor William T. Verts NAME PROBLEM SCORE POINTS GRAND TOTAL 100

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

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

UMBC CMSC 331 Final Exam

Title. Java Just in Time. John Latham. October 25, October 25, 2017 Java Just in Time - John Latham Page 1(0/0)

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

MIDTERM EXAMINATION - CS130 - Spring 2003

CS1800 Discrete Structures Final Version A

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

CS164: Midterm I. Fall 2003

(b) This is a valid identifier in Java: _T_R-U_E_. (c) This is a valid identifier in Java: _F_A_L_$_E_

APCS Semester #1 Final Exam Practice Problems

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

Advanced Java Concepts Unit 2: Linked Lists.

CS 112 Midterm Exam Fall 2016

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

CS 314 Midterm 2 Fall 2012

FORM 2 (Please put your name and form # on the scantron!!!!)

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

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Regis University CC&IS CS310 Data Structures Programming Assignment 2: Arrays and ArrayLists

Final Exam. Name: Student ID: Section: Signature:

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

CS 134 Midterm Fall 2006

Transcription:

Name: USC loginid (e.g., ttrojan): CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010 There are 4 problems on the exam, with 55 points total available. There are 7 pages to the exam, including this one; make sure you have all of them. There is also a double-sided one-page code handout that accompanies the exam. If you need additional space to write any answers, you may use the backs of exam pages (just direct us to look there). Remote DEN students only: Do not write on the backs of pages. If additional space is needed, ask proctor for additional blank page(s), put your name on them, and attach them to the exam. Put your name and USC ID number at the top of the exam. Please read over the whole test before beginning. Good luck! value score Problem 1 Problem 2 Problem 3A Problem 3B Problem 4 TOTAL 10 pts. 4 pts. 8 pts. 8 pts. 25 pts. 55 pts.

Problem 1 [10 pts.] Consider the following code fragment: Point p = new Point(5, 10); Point r; Point s = new Point(12, 20); s = p; p.translate(50, 100); System.out.println(s.getX()); p x = 5 y = 10 Part A [6]. To the right of the code, complete the diagram started above so it shows all object variables, objects, and their state as they change during the code sequence. Part B [2]. What is printed by the code? Part C [2]. How many Point objects are created by the code above? 2

Problem 2 [4 pts.] Consider the following version of the Drunkard class from PA1, and a code fragment that uses it. public class Drunkard { public Drunkard(Point startloc, int astepsize) {... [code to init any other fields not shown] Point currentloc = new Point(startLoc); stepsize = astepsize;... [other methods / fields not shown] private Point currentloc; private int stepsize; Drunkard joe = new Drunkard(new Point(10, 20), 5); Part A [2]. The Drunkard class compiles, but has an error. (Hint: it s related to variable declarations.) Show the exact value for joe after executing the code fragment above (i.e., do not fix the error: describe what the code does as is). Use a box-and-pointer diagram (there was an example of such a diagram in Problem 1). Part B [2]. Fix the code (make your changes right in the class code above don t rewrite it). 3

Problem 3 [16 pts. total] Part A [8]. Consider the following code we discussed in lecture to look up a value in an ArrayList using linear search. The current version of the method works on ordered or unordered ArrayLists and it always looks at all size() elements in the ArrayList for unsuccessful searches. Modify the code so that the new version only works on ArrayLists whose elements are in increasing order, and takes advantage of the ordering to, in general, perform better on unsuccessful searches than the old version. That is, it will stop looking when it gets to the part of the ArrayList that has values bigger than the one we are looking for. For example: target: joe namesarr: bob carly john mary peter sam tom we don t need to look past john (at mary, peter, sam, or tom) to figure out that joe is not there. Note: make your changes directly in the code below, or write any additional code to the right, adding arrows to show where the new code belongs. Also, see code handout for compareto method for doing inequality comparisons between Strings. public class Names { /** implementation invariant: values in namesarr are unique and in increasing alphabetical order */ private ArrayList<String> namesarr; /** returns location of target in namesarr or -1 if not found */ private int lookuploc(string target) { [do not change the method header] int loc = 0; while (loc < namesarr.size() && (!target.equals(namesarr.get(loc))) { loc++; if (loc == namesarr.size()) { return -1; else { return loc;... [rest of Names class not shown] 4

Problem 3 (cont.) Part B [8]. Come up with a good set of test cases to thoroughly test our new version of the code (i.e., this should include cases we would have used on the old version as well as ones designed to exercise all parts of the new code). Use the following Names object contents in some or all of your tests: namesarr: bob carly john mary peter sam tom For each test case, a. give the exact input that would be used (target; and namesarr, if different from the namesarr given above), b. show the expected result (i.e., expected return value of the function) for that case, and c. describe what case is being tested by that input. target expected case tested (and possibly result namesarr) 5

Problem 4 [25 pts. total] Consider the following class to simulate a die (i.e., most commonly is 6-sided, plural is dice). Assume someone already implemented the class for us. public class Die { // constructs a die with the given number of sides // pre: numsides >= 1 public Die(int numsides) {... // simulates one throw of the die // returns a random number in the range [1..numSides] public int throw() {... // return number of sides of the die public int getnumsides() {...... Example of using the Die class: Die die = new Die(6); // 6-sided die System.out.println(die.throw()); // rolls a 3 (actual results are random) System.out.println(die.throw()); // rolls a 1 System.out.println(die.throw()); // rolls a 5 In this problem you are going to use the Die class to implement a class to simulate what happens when throwing a pair of dice many times. It will keep track of how many times each possible sum of the two dice occurs. For 6-sided dice the smallest such sum (called a value below) is 2 (rolling two ones), and the largest is 12 (rolling two sixes). Here is an example of using the simulator: DiceSimulator sim = new DiceSimulator(6); // two 6-sided dice sim.run(10000); sim.printresults(); // what happens over 10000 rolls of each die? Sample output produced by printresults (some lines not shown): Results of throwing the dice 10000 times: value number of throws 2 280 3 557 4 834... 12 275 Note: To simplify this problem, you may assume each of the three DiceSimulator member functions will be called only once, as shown in the example above. The class interface is given on the next page, with space for your answer. To get full credit you must solve the problem efficiently, taking advantage of Java features we have learned about. 6

Problem 4 (cont.) public class DiceSimulator { [Note: space for instance vars given first.] // constructs a dice simulator using dice with the given number of sides // pre: numsides >= 1 public DiceSimulator(int numsides) // run the simulation for the given number of throws // pre: numthrows >= 0 public void run(int numthrows) // prints out the results of the simulation: // shows for each of the values from 2 to 2*numSides // how many times we rolled that value // [see sample output for details] public void printresults() 7