CS 307 Midterm 1[corrected] Spring 2008

Similar documents
CS 307 Midterm 1 Spring 2009

CS 307 Midterm 1 Fall 2007

CS 307 Midterm 2 Spring 2008

CS 314 Midterm 1 Spring 2014

CS 314 Midterm 1 Fall 2011

CS 307 Midterm 2 Fall 2009

CS 307 Midterm 2 Fall 2008

CS 307 Midterm 2 Fall 2010

CS 307 Midterm 2 Spring 2011

CS 314 Midterm 1 Fall 2012

CS 305j Midterm 2 Fall 2006

CS 314 Exam 1 Spring 2017

CS 314 Midterm 2 Fall 2012

CS 314 Exam 1 Fall 2017

CS 314 Final Fall 2012

CS 314 Exam 1 Spring 2018

CS 314 Exam 1 Spring 2016

CS 314 Midterm 2 Spring 2013

CS 312 Midterm 2 Fall 2013

CS 314 Exam 2 Spring

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

CS 314 Exam 1 Fall 2016

CS 305j Midterm 1 Fall 2007

CS 307 Final Spring 2008

CS 314 Exam 2 Fall 2017

CS 305j Midterm 1 Fall 2008

CS 314 Exam 1 Spring 2015

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

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

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

CS 312 Final Fall 2013

CS 314 Final Fall 2011

CS 307 Final Spring 2009

CS 314 Exam 2 Spring 2018

CS 314 Final Spring 2013 SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION

CS 312 Final Fall Your Name SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION. Your UTEID SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION _

CS 307 Final Fall 2009

CS 307 Final Spring 2010

Lecture 13: Two- Dimensional Arrays

Points off Total off Net Score. CS 314 Final Exam Fall 2016

CS 312 Midterm 1 Spring 2013

CS 312 Exam 2 Fall Circle your TA's Name: Aish Anthony Bri Carla Chris

1. Expressions - 1 point each. -1 for missing or extra " OR.0. differences in capitalization of true and false okay. No limit on points off.

CS 314 Exam 2 Spring 2016

Final Examination CS 125 Introduction to Computer Science Fall Hours

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

CS 312 Midterm 2 Fall 2013

CS 312 Final Fall 2016

CS 307 Final Spring 2011

CS 312 Exam 2 Fall KG Kris Megan Roman Sonika

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Practice Midterm 1. Problem Points Score TOTAL 50

Multiple Choice Questions Consider the following method and code segment.

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

MIDTERM EXAMINATION - CS130 - Spring 2003

PRACTICE MIDTERM EXAM #2

EE 312 Fall 2017 Midterm 1 October 12, 2017

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

CS 139 Practice Midterm Questions #2

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

JAVA MOCK TEST JAVA MOCK TEST II

CS 305j Midterm 1 Fall 2006

CS 113 MIDTERM EXAM 2 SPRING 2013

Give one example where you might wish to use a three dimensional array

Exam 1 - (20 points)

9/19/2018 Programming Data Structures. Polymorphism And Abstract

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

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

public static boolean isoutside(int min, int max, int value)

Building Java Programs

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Question Points Score

Problem Points Score Grader Total 100

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

CS 312 Midterm 2 Fall Jose Stas Aaron

University Interscholastic League. Computer Science Competition

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

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

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

Midterm Exam 2 CS 455, Spring 2015

Computer Science 136. Midterm Examination

Final Exam May 21, 2003

CSE 143 SAMPLE MIDTERM

Week 6: Review. Java is Case Sensitive

Final Exam. Kenneth J. Goldman December 18, Name: Student ID Number: Signature:

CS 312 Final Fall 2015

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

Back public class HelloWorld { public static void main ( String arg[] ) { Front Basic Setup. Java Quick Sheet. ~ 35 Flashcards. 1 AP CS - Rodriguez

CS314 Spring 2014 Midterm 1 Solution and Grading Criteria.

Computer Science 1 Bh

COMP 401 Spring 2013 Midterm 1

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

I. True/False: (2 points each)

Practice Midterm 1 Answer Key

MIDTERM EXAMINATION - CS130 - Spring 2005

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

CS 113 PRACTICE FINAL

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

Transcription:

Points off 1 2 3 4 5 Total off Net Score CS 307 Midterm 1[corrected] Spring 2008 Your Name Your UTEID Circle yours TA s name: Ruchica Mario Vishvas Instructions: 1. Please turn off or silence your cell phones 2. There are 5 questions on this test. 3. You will 2 hours to complete the test. 4. You may not use a calculator or any other electronic devices while taking this test. 5. Please make your answers legible. 6. When code is required, write Java code. 7. When writing a method, assume the preconditions of the method are met. 8. When writing a method you may add helper methods if you wish. 9. When you complete the test show the proctor your UTID and give them the test and any scratch paper. Please leave the room quietly. 1. (2 points each, 30 points total) Short answer questions. Place your answers on the attached answer sheet. For code sample state the output. If the code would cause a syntax error answer "syntax error". If it would cause an exception error answer "exception". If it would result in an infinite loop answer "infinite loop". A. What is the output of the following code? int x = 2; int y = 7; int z = y / x + y * 2; System.out.println( z ); B. What is the output of the following code? double a = 5 / 2; double b = a; a++; System.out.println( b ); CS 307 Midterm 1 Spring 2008 1

C. Consider the following method. public int process(int[] data, int m1, int m2){ int t = 0; for(int i = 0; i < data.length; i++){ if( data[i] < m1 data[i] > m2 ){ t++; return t; What is printed out by the following code? int[] list = {5, 8, 13, 10, -5, 0, 9, 7, 12; System.out.println( process(list, 3, 10) ); D. This question uses the method named process from part C. What is the output of the following code? int[] list2 = null; System.out.println( process(list2, 0, 12) ); E. Consider the following method. public void manip(int[][] mat){ for(int i = 0; i < mat[0].length; i++){ for(int j = i + 1; j < mat.length; j++){ mat[j][i] += mat[j - 1][i]; Consider the following code. int[][] cells = {{1, 2, 3, {-4, 5, 7, {2, 5, 4; manip( cells ); What are the contents of the 2d array of ints named cells after the code above completes? CS 307 Midterm 1 Spring 2008 2

For questions F M consider the following classes. public class School{ private int numstudents; public School(int numstudents){ this.numstudents = numstudents; public int getsize(){ return numstudents; public String tostring(){ return "Size: " + this.numstudents; public void graduate(){ this.numstudents -= this.numstudents / 4; public class HighSchool extends School{ private String classification; public HighSchool(int numstudents, String classification){ super( numstudents ); assert validclassification( classification ); this.classification = classification; public String tostring(){ return "class: " + this.classification; public static boolean validclassification(string s){ boolean result = s!= null && s.length() >= 0 && s.length() <= 5; if( result ) result = s.equals("a") s.equals("aa") s.equals("aaa") s.equals("aaaa") s.equals("aaaaa"); return result; public class University extends School{ private String name; public University(int numstudents, String name){ super(numstudents); this.name = name; public String tostring() { return this.name + ", " + super.tostring(); CS 307 Midterm 1 Spring 2008 3

F. What is the output when method two is called? public static void one(int pop){ pop *= 2; System.out.print( pop + " " ); public static void two(){ School rr = new School(1000); one( rr.getsize() ); System.out.println( rr.tostring() ); G. What is the output when method four is called? public static void three(school s){ s.graduate(); System.out.print( s.tostring() + " " ); public static void four(){ School gt = new School(1000); three(gt); System.out.println( gt.tostring() ); H. What is output when method six is called? public static void five(school s){ s.graduate(); s = new School( 1000 ); public static void six(){ School cp = new School( 100 ); five( cp ); System.out.println( cp ); I. What is the output of the following code? HighSchool schs = new HighSchool(1000, "BB"); System.out.println( schs.tostring() ); CS 307 Midterm 1 Spring 2008 4

J. What is the output of the following code? School ucsd = new University( 1000, "UCSD" ); ucsd.graduate(); System.out.println( ucsd.tostring() ); K. What is the output of the following code? HighSchool katy = new School(100); System.out.println( katy.tostring() ); L. What is the output of the following code? HighSchool austinhigh = new HighSchool(1000, "AAAAA"); austinhigh.graduate(); System.out.println( austinhigh.tostring() ); M. What is the output of the following code? University u = new University(1000, "UT"); School s = u; u.graduate(); System.out.println( s.tostring() ); For questions N and O consider the following interface. public interface TimeStamp{ public int gettime(); N. Does the following class compile? public class Book implements TimeStamp, Comparable{ public int compareto(object other){ return this.gettime() - ((TimeStamp)other).getTime(); O. Consider the following class. public class Trade implements TimeStamp, Comparable{ public Trade(){ // details not shown // other implementation not shown, but class will compile What data types can replace <*1> in the code below so that the code will compile? <*1> sc = new Trade(); CS 307 Midterm 1 Spring 2008 5

2. Arrays and Classes. (20 Points) In class we developed a class to represent a List of ints to demonstrate encapsulation and some of the details of building a class in Java. As discussed in class the internal storage container may have extra capacity. Complete a method for the IntList class, movemintofront. This is an instance method in the IntList class that finds the minimum value in the list and moves it to the front of the list. If there is more than one value equal to the minimum the value closest to the front of the list, index 0, is moved to the front. This method does not change the size of the list, but it may result in some elements being moved. The relative order of all elements except the first occurrence of the minimum must remain the same. You may not use any other methods in the IntList class unless you write them and you may not use methods from the Java Arrays class or the Java ArrayList class. Here are some examples of the result of a call to movemintofront on various lists. The element at the far left is at position 0 in the IntList. Original list Resulting list [0, 3, -2, 4, 5] [-2, 0, 3, 4, 5] [3] [3] [0, 3, -2, 4, 5, -2, 1] [-2, 0, 3, 4, 5, -2, 1] [-2, 0, 0, 0, 3, 3] [-2, 0, 0, 0, 3, 3] [4, 3, 2, 1] [1, 4, 3, 2] public class IntList{ private int[] ivalues; private int isize; public int size(){ return isize; /* pre: size() > 0 post: first occurrence of min value moved to front of list */ public void movemintofront(){ assert size() > 0 : "Failed precondition."; // Complete this method on the next page. CS 307 Midterm 1 Spring 2008 6

/* pre: size() > 0 post: first occurrence of min value moved to front of list */ public void movemintofront(){ assert size() > 0 : "Failed precondition."; // Complete this method. CS 307 Midterm 1 Spring 2008 7

3. Arrays and Classes. (20 points) In class we altered our IntList class to make in a more generic List class. This demonstrated some of the features of inheritance and polymorphism in Java. As discussed in class the internal storage container may have extra capacity. Complete an instance method for the List class isreverse. This method returns true if the calling object has the same elements as the explicit parameter, except the elements are in reverse order. Your method is not to alter either List. You may not use any other methods in the List class unless you write them (Except for the size method.) and you may not use methods from the Java Arrays class or the Java ArrayList class. Here are some examples of the result of a call to isreverse on various lists. Assume the integer values shown are actually Integer objects. Note the List objects may contain values equal to null. Calling Object Explicit Parameter returned value ["A"] ["A"] true ["A"] ["A", "B"] false ["A", "B"] ["B", "A"] true ["A", 1] [1, "A"] true ["A", 1] [2, 1] false [null, "A", "B"] ["B", "A", null] true [null, "A"] ["B", null] false public class List{ private iobject[] ivalues; private int isize; public int size(){ return isize; /* pre: other!= null post: return true if this List contains the same elements as other in reverse order, false otherwise. */ public boolean isreverse(list other){ assert other!= null : "Failed precondition."; // Complete this method on the next page. Recall that in the List class you have access to all List objects' private instance variables, not just the calling object's. CS 307 Midterm 1 Spring 2008 8

/* pre: other!= null post: return true if this List contains the same elements as other in reverse order, false otherwise. */ public boolean isreverse(list other){ assert other!= null : "Failed precondition."; // Complete this method. CS 307 Midterm 1 Spring 2008 9

4. 2D arrays. (15 points) Complete a method that determines if the floor of a room is completely covered in paint or not. The floor is modeled as a 2d grid as shown below. Each cell can either be clean or covered with paint. Initially the entire floor is clean. Paint balls are being tossed into the room from the top, left corner. Each paintball lands in a particular square and covers that square with paint. Each ball also splatters to the right and down. Each ball splatters based on its size. For example, a size 1 ball does not splatter, it only covers the cell it lands in. A size 2 ball covers the cell it lands in and the cell to the right and below. For example, assume a size 1 ball lands in row 0, col 1. It only covers that cell. X 1 Now assume a size 2 ball lands in row 1, col 1. It covers that cell and the cells at row 1, col 2 and row 2, col 1. The landing cell is shown with X 2 and the splattered cells are shown with S 2. X 1 X 2 S 2 S 2 Now assume a size 4 ball (marked X 3 )lands in row 0, col 2. It covers the cells shown below. Note the paint balls only splatter into cells in the same row or column as the cell that is hit. X 1 X 3 S 3 S 3 X 2 S 3 S 2 S 3 S 3 Note, a cell is either covered in paint or it is not. There is no measure of how many times it has been hit or splattered. CS 307 Midterm 1 Spring 2008 10

Complete a method that given the size of the floor and an array of PaintBall objects, returns true if the floor is completely covered with paint, false otherwise. The public portion of the PaintBall class is shown below. public class PaintBall{ //constructors not show // returns the row this PaintBall lands in. The returned value // will be greater than or equal to 0. public int getrow() // returns the column this PaintBall lands in. The returned value // will be greater than or equal to 0. public int getcol() // returns the size of this PaintBall. The returned value will be // greater than 0. public int getsize() Note, it is possible that a PaintBall will be outside the bounds of a given room, in which case it hits the wall and does not get any paint on the floor. Complete the following method. You may assume the preconditions of the method are met. Do not write assert statements to check preconditions. /* pre: numrows > 0, numcols > 0, hits!= null, no elements of hits = null. numrows and numcols specify the size of the room. post: return true if the floor is completely covered in paint, false otherwise. */ public boolean iscovered(int numrows, int numcols, PaintBall[] hits){ // Complete the method on the next page. CS 307 Midterm 1 Spring 2008 11

/* pre: numrows > 0, numcols > 0, hits!= null, no elements of hits = null. numrows and numcols specify the size of the room. post: return true if the floor is completely covered in paint, false otherwise. */ public boolean iscovered(int numrows, int numcols, PaintBall[] hits){ CS 307 Midterm 1 Spring 2008 12

5. (Implementing classes, 15 points) Complete a class that models a University course. A course has the following properties: the number of credits the course is worth. This cannot be negative the course number. This will be an integer value between 0 and 99,999 inclusive. The department the course is taught by. This will be a String. Complete a complete Course class that has the following properties. Instance variables to store all of the relevant information for a course as explained above. A constructor to initialize all values based on parameters. A method to change the number of credits a course is worth. A tostring method. Complete the class below. You may add other methods if you wish. State your preconditions and use assert statements to check them. // more room on the next page if necessary CS 307 Midterm 1 Spring 2008 13

// more room for the Course class if necessary CS 307 Midterm 1 Spring 2008 14

Scratch paper CS 307 Midterm 1 Spring 2008 15

Question 1 answer Sheet Name A. I. B. J. C. K. D. L. E. M. F. N. G. O. H. CS 307 Midterm 1 Spring 2008 16

Scratch Paper CS 307 Midterm 1 Spring 2008 17

Scratch Paper CS 307 Midterm 1 Spring 2008 18

Scratch Paper CS 307 Midterm 1 Spring 2008 19

Scratch Paper CS 307 Midterm 1 Spring 2008 20

Scratch Paper CS 307 Midterm 1 Spring 2008 21