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

Similar documents
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 2013 [Bono] Feb. 21, 2013

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

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

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

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

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

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

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

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

Midterm Exam 2 CS 455, Spring 2015

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

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

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

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

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

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

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

Midterm Exam 2 CS 455, Spring 2011

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

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

Midterm Exam 2 CS 455, Spring 2014

Midterm Exam 2 CS 455, Fall 2014

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

CS 113 MIDTERM EXAM 2 SPRING 2013

Midterm Exam 2 CS 455, Spring 2013

Midterm Exam 2 CS 455, Fall 2012

CS 312 Midterm 1 Spring 2013

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

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

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

Midterm Exam 2 CS 455, Fall 2013

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

CS110 Introduction to Computing Fall 2006 Midterm Exam

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

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

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

Practice Midterm 1. Problem Points Score TOTAL 50

MACS 261J Final Exam. Question: Total Points: Score:

CS170 Introduction to Computer Science Midterm 2

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

AP Computer Science Unit 1. Programs

CONDITIONAL EXECUTION

CIS 110 Spring 2013 Make-Up Midterm, 13 February 2013, Answer Key. Miscellaneous

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

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

CIS 110 Introduction to Computer Programming. 13 February 2013 Make-Up Midterm Midterm

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

1. Find the output of following java program. class MainClass { public static void main (String arg[])

For example, when your method is added to the class with the main method below, the output should be:

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

Final Exam. COMP Summer I June 26, points

CS 113 PRACTICE FINAL

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

Midterm Examination (MTA)

Spring % of course grade

CS 312 Midterm 1 Spring 2013

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

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Name Section Number. CS110 Exam #1 *** PLEASE TURN OFF ALL CELLPHONES ***

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

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

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

CS 101 Exam 2 Spring Id Name

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

CS164: Midterm Exam 2

Last Name: Circle One: OCW Non-OCW

1 Inheritance (8 minutes, 9 points)

Week 6 CS 302. Jim Williams, PhD

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

Control Statements: Part 1

Computer Science E-22 Practice Final Exam

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

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

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

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name:

CSCI 201L Written Exam #2. 10% of course grade

CSE413 Midterm. Question Max Points Total 100

Practice Midterm 1 Answer Key

Place your name tag here

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

Lecture 5: Methods CS2301

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

CIS 110 Fall 2016 Introduction to Computer Programming 13 Oct 2016 Midterm Exam

Join the course group CS230-S18! Post questions, answer them if you know the answer!

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

Problem Grade Total

Structure and Interpretation of Computer Programs

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

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm. Answer Key

Question 1. [5 points] Consider the following partially-complete program, which begins on the left and continues on the right: HERE

Transcription:

Name: USC username (e.g., ttrojan): CS 455 Midterm Exam 1 Spring 2015 [Bono] Thursday, Feb. 19, 2015 There are 5 problems on the exam, with 50 points total available. There are 7 pages to the exam, including this one; make sure you have all of them. If you need additional space to write any answers, you may use the backs of exam pages (just 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 at the top of the exam. Please read over the whole test before beginning. Good luck! value score Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 TOTAL 11 pts. 6 pts. 10 pts. 8 pts. 15 pts. 50 pts. 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 new value is a point with coordinates (x+dx, y+dy)

Problem 1 [11 pts.] Consider the following program: (Note: more about Point class on the cover page of the exam.) public class Prob1 { public static void foo(point a, Point b) { Point olda = a; a = b; System.out.println(a + " " + b); b = olda; public static void main(string[] args) { Point p = new Point(5, 10); Point q = new Point(12, 18); foo(p, q); System.out.println(p + " " + q); Part A [7]. 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 and local variable. 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.] 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 >= 60) { return cool ; if (temp >= 70) { return just right ; if (temp >= 80) { return hot ; if (temp >= 90) { return boiling ; return "cold"; // 60 is about 16 C // 70 is about 21 C // 80 is about 27 C // 90 is about 32 C // FYI: "boiling" can be used to mean "very hot" 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.] The following method doesn t work. The method comment describes what it is supposed to do. /** Prints the elements of the array, nums, as an addition expression followed by a newline. If there is only one element, prints just that number followed by newline. If nums has length 0, does nothing. Example output for various calls (shows output for arrays with 1, 2, and 3 elements, respectively): 5 4 + 9 8 + 10 + 6 */ public static void printadditionexpression(int[] nums) { for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + " + "); System.out.println(); Part A [4]. Show two example data sets for which the method prints the wrong result and what the method prints in that case: nums output of printadditionproblem(nums) 1. 2. Part B [6]. 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. 4

Problem 4 [8 pts] Complete the implementation of the class TrangularGenerator, which generates the triangular number sequence, defined below. The kth triangular number is the sum of the numbers from 1 to k. The first six numbers in the sequence are: 1, 3, 6, 10, 15, 21. Computing T k does not require a loop, because there is also a formula for it, shown here: T! =!!!! i = 1 + 2+... +k = k k + 1 2 Furthermore, looking at the sequence, you may notice that it's even easier to generate the next triangular number if you know the previous one (no multiplications involved). For full credit, your class must take advantage of the fact that we are generating these values in sequence, using no loops or multiplications to compute the next value in the sequence. (You can still get partial credit for using the formula.) The example below shows how to use the class: public static void main(string[] args) { TriangularGenerator gen = new TriangularGenerator(); System.out.println(gen.nextInt()); // first time returns T1 (= 1) System.out.println(gen.nextInt()); // 3 (1 + 2) System.out.println(gen.nextInt()); // 6 (1 + 2 + 3) System.out.println(gen.nextInt()); // 10 (1 + 2 + 3 + 4)... [The class interface and space for your answer is given on the next page à ] 5

Problem 4 (cont.) The class interface and method comments appear below, you can just add in your code to complete the class: // TriangularGenerator generates the sequence of triangular numbers. public class TriangularGenerator { // Creates triangular generator object public TriangularGenerator () { // Returns the next triangular number in the sequence. // The first call to it returns T1 (=1). public int nextint() { 6

Problem 5 [15 pts] Write the static Java int method largestdistance which takes an array and returns the largest distance between two elements that have the same value. An element has a distance of zero from itself, and two adjacent elements with the same value have a distance of one. (Thus, if no two elements have the same value then you return a largest distance of 0.) Here are some examples: arr [2,3,6,10] 0 [0,0,0,0,0] 4 [2,3,2,3,2] 4 [3,2,7,7,1] 1 [4,5,100,4,5,100] 3 [1] 0 [] 0 largestdistance(arr) public static int largestdistance(int[] arr) { 7