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

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

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

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

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

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

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

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

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

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

Midterm Exam 2 CS 455, Spring 2014

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

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

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

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

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

Midterm Exam 2 CS 455, Fall 2013

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

Midterm Exam 2 CS 455, Spring 2015

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

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

Midterm Exam 2 CS 455, Fall 2014

Midterm Exam 2 CS 455, Spring 2011

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

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

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

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

University of Massachusetts Amherst, Electrical and Computer Engineering

More on class design

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

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

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

ArrayList; Names example

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Midterm Exam 2 CS 455, Fall 2012

Programming Basics. Digital Urban Visualization. People as Flows. ia

JAVA OPERATORS GENERAL

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, 7:30 PM Total Question Name Short answer

Midterm Exam 2 CS 455, Spring 2013

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

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

Recursion; Side effects

Prelim One Solution. CS211 Fall Name. NetID

Final Exam. COMP Summer I June 26, points

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

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

CS 455 MT 2 Sample Questions

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

XC Total Max Score Grader

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

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

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

New York University Intro to Computer Science (CSCI-UA.101) Fall 2014 Midterm #1 Test G. Instructions:

CS 113 MIDTERM EXAM 2 SPRING 2013

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

Prof. Navrati Saxena TA: Rochak Sachan

Last Name: Circle One: OCW Non-OCW

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

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Key Java Simple Data Types

Java Simple Data Types

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

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

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

CSE 331 Spring 2018 Midterm

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

CSE 142 Wi03 Midterm 2 Sample Solution All Versions Page 1 of 6

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

Homework Set 1- Fundamentals

CS 111X - Fall Test 1

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

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CS 101 Fall 2006 Midterm 1 Name: ID:

CS 101 Spring 2007 Midterm 2 Name: ID:

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

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

University of Palestine. Mid Exam Total Grade: 100

Final Exam COMP Fall 2004 Dec 16, 2004

CS 101 Spring 2006 Final Exam Name: ID:

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

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

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

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

CS 101 Exam 2 Spring Id Name

Computer Sciences 302 Exam 2 Information & Sample Exam

CSE413 Midterm. Question Max Points Total 100

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

CS171 Midterm Exam. October 29, Name:

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

Name CIS 201 Midterm II: Chapters 1-8

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

CSE331 Winter 2014, Midterm Examination February 12, 2014

I. True/False: (2 points each)

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

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

Transcription:

Name: USC NetID (e.g., ttrojan): CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016 There are 5 problems on the exam, with 56 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 or scratch work, pages 9 and 10 are left blank for that purpose. If you use those pages for answers you just need to direct us to look there. Do not detach any pages from this exam. 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! Selected methods of Java Point class: new Point(x, y) Constructs point object with given x and y values. p.translate(dx, dy) Changes x and y values of p by dx and dy, respectively. I.e., if p had coordinates (x, y), its value after the call is a point with coordinates (x+dx, y+dy) [this is a mutator]

Problem 1 [10 pts.] Consider the following program. Note: it uses the Java Point class more information about Point on the cover of the exam. public class Prob1 { public static void foo(point c, Point d) { c = d; d.translate(5, 10); System.out.println(c + " " + d); public static void main(string[] args) { Point a = new Point(12, 24); Point b = new Point(3, 7); foo(a, b); System.out.println(a + " " + b); Part A [6]. 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 foo's parameters. Part B [4]. What is printed by the code? For the purpose of this problem assume a Point is printed as follows: [x, y] 2

Problem 2 [6 pts.] USC NetID: Consider the following static method that is supposed to return a String describing the weather, when given an outside temperature in Fahrenheit. (Approximately equivalent temperatures are also shown in Celsius for those of you who aren t used to Fahrenheit.) It doesn t always do the right thing. public static String getweather(int temp) { if (temp >= 90) { weather = boiling ; // 90 is about 32 C if (temp < 90 && temp >= 80) { weather = hot ; // 80 is about 27 C if (temp < 80 && temp >= 70) { weather = just right ; // 70 is about 21 C if (temp < 70 && temp >= 60) { // 60 is about 16 C weather = cool ; else { weather = cold ; return weather; Do not modify the code. Show two example data values and the result of calling the method on each of them: the first one should be one where the existing method returns an incorrect weather description, and a second one such that the method returns an accurate weather description: temp return value of getweather(temp) 1. (wrong) 2. (right) 3

Problem 3 [10 pts.] Implement the class DigitExtractor, which breaks up an integer into its individual digits. After being initialized with a positive integer, each call to nextdigit() returns the next digit in the integer, starting from the leftmost (most significant) digit. For full credit do not call largestpowerof10 (details below) more than once in the lifetime of a DigitExtractor object. Here is an example of code to use the class. It extracts all of the digits of 27,054: DigitExtractor extractor = new DigitExtractor(27054); while (extractor.hasnextdigit()) { System.out.println(extractor.nextDigit()); Corresponding output: 2 7 0 5 4 You may assume we have already written the following helper method for you that works as described: (i.e., that means you may call this method in your solution) // Returns the largest power of 10 that is less or equal to num. // or put another way: returns a power of 10 that has the same number of // digits as num // PRECONDITION: num > 0 // Examples: num return value of largestpowerof10(num) // 999 100 // 1000 1000 // 1001 1000 private static int largestpowerof10(int num) {... For full credit do not call largestpowerof10 more than once per DigitExtractor object. DigitExtractor hint: use the modulus operator (%) and integer division: The modulus operator gives the remainder of dividing two integers. For example, the result of 17 % 3 is 2 (17 divided by 3 has a remainder of 2). Reminder: Integer division is done with the / operator performed on two integers. For example, the result of 17 / 3 is 5. [Do not write your answer here. The class interface and space for your answer is provided on the next page.] 4

USC NetID: Problem 3 (cont.) Complete the implementation of DigitExtractor details of this problem are on the previous page. // DigitExtractor breaks up a positive integer into its individual digits. public class DigitExtractor { // Creates digit extractor for the given integer. // @param anint the integer to extract from // PRECONDITION: anint > 0 public DigitExtractor (int anint) { // Returns true iff there are more digits left to extract. public boolean hasnextdigit() { // Extracts the the "next" digit in the integer (starts from leftmost // (most significant) digit, and goes rightward) // PRECONDITION: hasnextdigit() // @return the digit public int nextdigit() { // largestpowerof10: see comment on previous page private static int largestpowerof10(int num) { /* already written */ 5

Problem 4 [25 pts. total] Consider a slightly different representation for the Names class than the one we used in lecture. This new version will use a partially filled array, but the array will be filled from the right side instead of the left, although the values will still be in alphabetical order (not reverse alphabetical). Here are the instance variables for this representation: private String[] namesarr; private int startloc; // location of the first name in namesarr Here is an example of such an object containing the names Ann, Bob, Don, Ed: Names namesarr 0 1 2 3 4 5 6 7 8 9 Ann Bob Don Ed startloc 6 Part A [5]. Most of a representation invariant for this class appears below. Fill in the blanks to complete it so it matches the representation described and illustrated above. Hint: the representation invariant is in general terms; it should not involve any of the particular numbers from the example object above. the number of names is if the names object isn't empty the names are in array elements: namesarr[ ] through namesarr[ ] the valid range for startloc is: (you can show as an expression with relational operators such as < or <=, or using [..,..] or [..,..), for example, range notation.) the names in namesarr are in alphabetical order from left to right the names in namesarr are unique Part B [20]. Implement the constructor, numnames, and remove methods of the Names class that appear on the next page (space for your answer is there too) using the representation described here. Note: You may use the helper method lookuploc in your solution (specification shown on next page). Do not implement lookuploc. 6

Problem 4 (cont.) USC NetID: Part B (cont). Implement the following methods of the Names class, using the representation discussed on the previous page (you do not have to complete the rest of the class): // Stores a list of unique names in alphabetical order. Allows // look-up, insert, and removal of elements in the list. public class Names { private String[] namesarr; private int startloc; // location of the first name in namesarr private final static int NOT_FOUND = -1; private final static int INITIAL_CAPACITY = 10; // starting capacity of array // Creates an empty names object public Names() { // Returns the number of names in the list public int numnames() { // Removes target from names, and returns true. // If target wasn't present in names, returns false and no change // made to names. public boolean remove(string target) { // lookuploc returns index of target in namesarr or NOT_FOUND // if it is not present private int lookuploc(string target) {... // do not implement... // other Names methods not shown 7

Problem 5 [5 points] The following method to print out a sentence doesn t quite work as desired: it prints a space between the last word and the period at the end of the sentence. The method comment describes what it is supposed to do. Fix the code below. Do not rewrite the whole method, but rather make your changes right into the code below, using arrows to show where your code should be inserted, crossing out code that you would get rid of, etc. /** Prints out an arraylist of words as a period-terminated sentence with a single space between each word. Here are some examples to illustrate the output format: words output of printsentence(words) ["Hello"] ["Hello", "there"] ["I", "am", "a", "cat"] Hello. Hello there. I am a cat. */ PRECONDITION: words.size() > = 1 public static void printsentence(arraylist<string> words) { for (int i = 0; i < words.size(); i++) { System.out.print(words.get(i) + " "); System.out.println("."); 8

Extra space for answers or scratch work. USC NetID: 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