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

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

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

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

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

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

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

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

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

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

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

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

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

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

Midterm Exam 2 CS 455, Spring 2015

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

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

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

Midterm Exam 2 CS 455, Spring 2011

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

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

Midterm Exam 2 CS 455, Fall 2014

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

Midterm Exam 2 CS 455, Spring 2014

Midterm Exam 2 CS 455, Spring 2013

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

Midterm Exam 2 CS 455, Fall 2013

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

New York University Introduction to Computer Science Midterm2 Sample Problems 2013 Andrew I. Case. Instructions:

Chapter 6 Arrays, Part D 2d Arrays and the Arrays Class

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

CSE wi Midterm Exam 2/8/18. Name UW ID #

Objects and State. COMP1400 Week 9. Wednesday, 19 September 12

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

Question: Total Points: Score:

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

Honors Algebra 2 Unit 8 ~ Bits & Pieces

Midterm Exam 2 CS 455, Fall 2012

RECURSION (CONTINUED)

Practice Midterm 1. Problem Points Score TOTAL 50

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

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

Midterm Examination (MTA)

CSC 207H Fall L Java Quiz Duration 25 minutes Aids allowed: none

CSE wi Midterm Exam 2/8/18 Sample Solution

CS 112 Introduction to Programming

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

INFORMATION TECHNOLOGY SPREADSHEETS. Part 1

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

Example Program. public class ComputeArea {

CSCI 102 Fall 2010 Exam #1

STUDENT LESSON A12 Iterations

CONCORDIA UNIVERSITY Summer 2005 Comp 248 /1 Section AA Introduction to Programming Final Examination/A

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

AP CS Unit 3: Control Structures Notes

CS 111X - Fall Test 1

Prelim 1. Solution. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer

Object Oriented Programming. Java-Lecture 6 - Arrays

CS 113 MIDTERM EXAM 2 SPRING 2013

CS 104 (Spring 2014) Final Exam 05/09/2014

Dynamic arrays / C Strings

Question: Total Points: Score:

CSE 331 Spring 2018 Midterm

Java Coding 3. Over & over again!

The experimental guide: comparing automatic test generation with manual test generation from UML state machine diagrams of a vending machine program

Final exam. CS 2110, December 15, 2016, 9:00AM

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Name CIS 201 Midterm II: Chapters 1-8

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

Chapter Goals. Contents LOOPS

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

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

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

SUN Sun Certified Associate for the Java Platform.

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

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

CSE331 Winter 2014, Midterm Examination February 12, 2014

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Week 6 CS 302. Jim Williams, PhD

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

CSE413 Midterm. Question Max Points Total 100

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

Prelim 1. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

Question: Total Points: Score:

Oct Decision Structures cont d

CS 10, Fall 2015, Professor Prasad Jayanti

Prelim One Solution. CS211 Fall Name. NetID

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

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

Prelim 1. Solution. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer

COS 126 General Computer Science Fall Exam 1

XC Total Max Score Grader

Computer Science II (20082) Week 1: Review and Inheritance

CS 314 Final Fall 2012

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

CS 61B: Midterm Exam I

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

Transcription:

Name: USC netid (e.g., ttrojan): CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015 There are 5 problems on the exam, with 58 points total available. There are 10 pages to the exam (5 pages double-sided), including this one; make sure you have all of them. If you need additional space to write any answers, pages 9 and 10 are left blank for that purpose. If you use these pages for answers you just need to direct us to look there. Note: if you give multiple answers for a problem, we will only grade the first one. Avoid this issue by labeling and circling your final answers and crossing out any other answers you changed your mind about (though it s fine if you show your work). Put your name and USC username (a.k.a., netid) at the top of the exam. Also put your netid at the top right of the front side of each page of the exam. Please read over the whole test before beginning. Good luck! value score Problem 1 Problem 2 Problem 3A Problem 3BCD Problem 4 Problem 5 TOTAL 6 pts. 4 pts. 10 pts. 10 pts. 8 pts. 20 pts. 58 pts. Arrays.toString method documentation (from Oracle documentation): public static String tostring(int[] a) Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Elements are converted to strings as by String.valueOf(int). Returns "null" if a is null.

Problem 1 [6 pts.] Consider the following program: public class Prob1 { public static void main(string[] args) { int[] mynums = null; initarray(mynums, 5); System.out.println(Arrays.toString(myNums)); public static void initarray(int[] nums, int size) { nums = new int[size]; for (int i = 0; i < nums.length; i++) { nums[i] = i; Part A [4]. In the space below, draw a box-and-pointer diagram (a.k.a., memory diagram) showing all object variables, objects, and their state as they have changed during the code sequence. This includes showing initarray's parameters. Part B [2]. What is printed by the code? (Documentation for Arrays.toString appears on the exam cover page) 2

Problem 2 [4 pts.] Consider the following static method that returns a boolean telling us whether the given day of the week is a work day. Days are represented using the constants below. Hint: it doesn't do what you might think it does. public static final int SUN = 0; // assume these constants are also public static final int MON = 1; // defined in the same class... public static final int SAT = 6; // Do not change these constants in part B /** Returns true iff the given day is a work day. PRE: day is one of SUN, MON, TUE, WED, THU, FRI, SAT */ public static boolean isworkday(int day) { if ((day!= SAT) (day!= SUN)) { return true; return false; Part A. Suppose we use this method (with no changes made to it) to find out which days of the week someone has to work. How many days a week does the worker have to work? Part B. Modify the method above so it gives results consistent with the normal American Monday through Friday work week, or if it already does that, write "No change", below. Make any changes directly in the code above, using arrows to show where your code should be inserted, crossing out code that you would get rid of, etc. (Answers with numbers in them will not receive full credit.) 3

Problem 3 [20 pts. total] Part A [10]. Complete the implementation of the class VendingMachine, defined on this and the next page. A VendingMachine models the inventory inside a vending machine that can hold three types of candy. One can buy candy, find out how much candy there is, and supply the machine with more candy. (Note: The money part of the transactions are not part of this class.) Here is an example of the use of this class: public static void main(string[] args) { VendingMachine vend = new VendingMachine(); // machine is empty System.out.println(vend.buy(VendingMachine.HERSHEYS, 3); // no product in the machine, machine gives 0 vend.supply(vendingmachine.hersheys, 20); // stock the machine vend.supply(vendingmachine.m_and_ms, 20); vend.supply(vendingmachine.starburst, 20); System.out.println(vend.buy(VendingMachine.HERSHEYS, 3); // customer buys 3 Hersheys bars (prints 3) System.out.println(vend.buy(VendingMachine.HERSHEYS, 18); // customer attempts to buy 18 Hersheys, only receives 17 System.out.println(vend.buy(VendingMachine.HERSHEYS, 3); // prints 0 System.out.println(vend.get(VendingMachine.HERSHEYS));// How many left? 0 vend.supply(vendingmachine.hersheys, 5); // ran out of HERSHEYS, add more System.out.println(vend.buy(VendingMachine.STARBURST, 1); // prints 1 System.out.println(vend.buy(VendingMachine.HERSHEYS, 10); // prints 5... The class interface and method comments appear below and continue onto the next page. We left space so you can add in your code to complete the class: // VendingMachine models the inventory inside a candy machine. // interface invariant: the amount of each kind of candy in the machine // is always non-negative public class VendingMachine { public static final int HERSHEYS = 0; public static final int M_AND_MS = 1; public static final int STARBURST = 2; public static final int NUM_KINDS = 3; // Creates an empty vending machine object public VendingMachine () { 4

Problem 3 (cont.) // PRECONDITION for parameters to get, buy, and supply: // -- kind is one of HERSHEYS, M_AND_MS, and STARBURST // -- quantity > 0 [applies to buy and supply only] // Returns the amount in this machine of the kind of candy specified // PRECONDITION: see above public int get(int kind) { // Attempt to buy the given quantity of the kind of candy specified // from this vending machine. Returns the actual number of pieces // given out, based on the current inventory (i.e., it can't give out // more than is currently in the machine). // PRECONDITION: see above public int buy(int kind, int quantity) { // Add to this vending machine the given quantity of the kind of candy // specified. // PRECONDITION: see above public void supply(int kind, int quantity) { 5

Problem 3 (cont.) Part B [3]. Suppose we now want to add a fourth type of candy in our VendingMachines. What parts of your VendingMachine class code would have to change to handle the fourth candy type? Circle all that apply below (we started it for you): a. comments b. constant definitions c. instance variable definitions d. constructor e. buy method f. supply method Part C [2]. Show the variable definition(s) for data structure that would allow us to store information about all the VendingMachines in a building: Part D [5]. Give the code to initialize your data structure from part C so that there are 10 empty vending machines in the building, or say "already done", if your data structure was implicitly initialized this way by the code you gave for part C. 6

Problem 4 [8 pts. total] The following method doesn t work. The method comment describes what it is supposed to do. /** Reads an indeterminate number of non-negative values from the user, using -1 as a sentinel, and returns the sum of the numbers @param in the scanner to read from @returns the sum of the non-negative numbers read */ public static int sumints(scanner in) { System.out.print("Enter numbers when prompted. "); System.out.println("Use -1 to indicate you are done."); int num = 0; int sum = 0; while (num!= -1) { System.out.print("Enter number (-1 to finish): "); num = in.nextint(); sum += num; return sum; Part A. Show two example data sets for which the method returns the wrong result and what the method returns in that case. The second one of these must be a boundary case. a. numbers entered by the user return value of sumints(new Scanner(Sytem.in)) b. (boundary case) Part B. Fix the code above. Do not rewrite the whole method, but rather make your changes right into the code above, using arrows to show where your code should be inserted, crossing out code that you would get rid of, etc. 7

Problem 5 [20 pts] Write the static Java int[] method divtenfirst which takes an array of numbers and returns an array with the same values as the original, but with all the numbers that are divisible by 10 appearing before all of the numbers that aren't divisible by 10. Other than that, the numbers may appear in any order. The array nums should be unmodified by the method. Here are some examples (because of the ordering flexibility, your code may get different results): nums divtenfirst(nums) [3, 2, 6, 10] [10, 3, 2, 6] [0, 10, 30, 0] [10, 30, 0, 0] [20, 5, 200, 3, 8, 20] [20, 200, 20, 5, 3, 8] [3, 2, 7, 7, 1] [3, 2, 7, 7, 1] [20, 20, 20] [20, 20, 20] [1] [1] [] [] public static int[] divtenfirst(int[] nums) { 8

Extra space for answers or scratch work. If you put any of your answers here, please write a note on the question page directing us to look here. Also label any such answers here with the question number and part, and circle the answer. 9

Extra space for answers or scratch work (cont.) If you put any of your answers here, please write a note on the question page directing us to look here. Also label any such answers here with the question number and part, and circle the answer. 10