CS170 Introduction to Computer Science Midterm 2 03/25/2009 Name: Solution You are to honor the Emory Honor Code. This is a closed book and closednotes exam, and you are not to use any other resource than yourself in completing this exam. You have 50 minutes to complete this exam. The point values for each problem are indicated both below and beside each problem. Read each problem carefully, and review your answers! Problem Points Earned 1 20 2 30 3 15 4 15 5 20 6 30 7 30 Total points 160
(20 pts) 1. Each of the questions below is worth 2 points. Select the correct answer (each question has only one correct answer). 1.1 Analyze the following code: public class Test { public static void main(string[] args) { int[] x = {1, 2, 3, 4; x = new int[2]; for (int i = 0; i < x.length; i++) { System.out.print(x[i] + " "); A) The program displays 0 0 B) The program displays 0 0 0 0 C) The program displays 0 0 3 4 D) The program displays 1 2 3 4 1.2 Consider the following code fragment: int[] list = new int[10]; for (int i = 0; i <= list.length; i++) { list[i] = (int)(math.random() * 10); A) The code has a runtime error indicating that the array is out of bound B) The loop body will execute 10 times, filling up the array with zeros C) The loop body will execute 10 times, filling up the array with random numbers D) list[5] will always have value 5 1.3 Given the following statement: int[] list = new int[10]; list.length has the value: A) 9 B) 10 C) 11 D) list.length is illegal expression 1.4 is a construct that defines objects of the same type A) a method B) a class C) an object D) a data field 1.5 What is the output of the following code? public class Test { public static void main(string[] args) { String s1 = new String("Welcome to Java!"); String s2 = new String("Welcome to Java!"); if (s1.equals(s2)) { System.out.println("s1 and s2 have the same contents"); else { System.out.println("s1 and s2 have different contents"); A) s1 and s2 have the same contents B) s1 and s2 have different contents
1.6 Suppose s is a string with the value "fluffy". What will be assigned to x if you execute the following code? char x = s.charat(2); A) l B) u C) f D) This statement is illegal 1.7 What is the return value of "SELECT".substring(0, 5)? A) SELE B) ELE C) SELEC D) SELECT 1.8 When you pass an object to a method, the method receives A) the reference to the object B) the size of the object C) a copy of the object D) This cannot be done in java 1.9 The default constructor has no arguments. A) true B) false 1.10 The constructor can be executed only when new object is created. A) true B) false 2 (30 pts) Write a method called getlargest() that takes one argument, an array of integers and returns an int value. The method should return the largest item from the array passed as an argument. To get the full credit do not sort the array. public static int getlargest(int[] arr) { int max = arr[0]; for (int i= 1; i < arr.length; i++) { return max; if (max < arr[i]) max = arr[i]; The following class is used in questions 3 7
public class Point { private int x; private int y; public Point() { public Point(int x, int y) { this.x = x; this.y = y; public int getx() { return x; public int gety() { return y; public void mystery(point otherpoint) { int newx = (x + otherpoint.getx()) / 2; int newy = (y + otherpoint.gety()) / 2; x = newx; y = newy; 3 (15 pts) Identify all data fields, their types and visibility modifiers in the class Point x of type int and with modifier private y of type int and with modifier private 4 (15 pts) How many constructors does the Point class have? Identify the parameters of each of the constructors. Two: Point() no arguments and Point(x, y) two int arguments
5 (20 pts) Explain the behavior of mystery method The method updates coordinates of given point to the midpoint between this point and otherpoint. 6 (30 pts) Declare a new method in class Point called distance. The method takes one argument of type Point and returns a double. The result should be a distance between this point and point passed as an argument. Hint: use Math.sqrt(double val) to get square root of given number. See the mystery method for hint how to use Point as an argument of a method inside Point class. If you have 2 points with coordinates (x 1,y 1 ) and (x 2,y 2 ), the distance between those points is defined as follows: d (x 1 x 2 ) 2 (y 1 y 2 ) 2 public double distance(point p) { int difx = p.getx() x; int dify = p.gety() x; return Math.sqrt(difX*difX + dify*dify);
7 (30 pts) Write a class called Perimeter. The class will only have a main method (so that you can run it typing java Perimeter ). The main method will have the following behavior. It will create an array of 10 objects of type Point (defined above). The points in the array should have random x and y coordinates (in the range 0 10). Once you create the array, you will calculate and print sum of distances between points in the array as follows: distance between first and second point + distance between second and third point + distance between third and fourth point + + distance between last and first point You can assume that the class Point provides a method called distance that takes one argument, another instance of class Point and returns the distance between the points: public double distance(point p); Method returns distance between this point and Point p. In that case, to get distance between two points, p1 and p2 you can use: double dst = p1.distance(p2); Hint: Be careful when creating an array of objects. Remember that creating an array does not create actually objects inside that array it has to be done manually. public class Perimeter { public static void main(string[] args) { Point[] points = new Point[10]; for (int i = 0; i < point.length; i++) { points[i] = new Point((int)(Math.random() * 11), (int)(math.random() * 11)); double sum = 0; for (int i = 0; i < point.length 1; i++) { sum += points[i].distance(points[i+1]); sum += points[0].distance(points[points.length 1]); System.out.println(sum);