Your Name: Exam 2. CSC 121 MW Class Lecturer: Howard Rosenthal April 26, 2017 The following questions (or parts of questions) in numbers 1-7 are all worth 3 points each. 1. Answer the following as true or false. a. _F_ Every for iteration loop must be executed at least once. b. _T_ Iterative statements can be nested. c. _T_ Two array reference variables can refer to the same array object. d. _F_ An array in Java can only store primitive data types (i.e. be declared as a primitive data type). e. _F_ A two-dimensional array can hold different value types in different rows. f. _T_ Any iteration that you can perform with a while statement can also be performed with a do-while statement. g. _F_ Java does not allow void methods to be overloaded. h. _F_ All methods must be called from the main method. i. _T_ A list must always be ordered before performing a binary search. j. _F_ The index of an array can be of type long. k. _T_ The output from the java compiler is expressed in Bytecode. 2. Given the following initialization int [][] array1 = new int [8][4]; The value array1.length is 8. 3. The number of bytes reserved for the array double [][] newarray = new double [3][5] is 120 4. Write a statement that declares and instantiates a char array called letterarray of length 14. char [] letterarray = new char[14]; 1
5. Give two different examples of method overloading that display two different ways that methods can be overloaded. (Each example requires two method headers with the same name) a. b. public static void method1() public static void method1(int x) public static double method2(int x, double y) public static double method2(int x, int y) 6. Write a single statement the declares, instantiates and initializes a ragged array of type int called myarray equivalent to the following table: 1 0-5 8 1 1 3-6 9 12 4 int [][] myarray = { {1}, {0,-5}, {8, 1, 1, 3, -6}, {9, 12, 4}}; 2
7. What are the values stored in the array a after the following code executes: int [] a = new int [5]; for (int i = 1; i < a.length; i++) { a[i] = i; a[i] = (a[i-1]+a[i]) +2; } 0, 3, 7, 12, 18 8. What is the printout from the code segment below: int [] ref1 = new int [6]; int [] ref2; for (int i=0; i < 6; i++) ref1[i] = (int)math.pow(i,2); ref2 = ref1; for (int i=0; i < 6; i++) ref2[i] =2*ref2[i]; for (int i=0; i < 6; i++) System.out.print(ref1[i] + ); 0 2 8 18 32 50 3
Programs: It is important to follow the instructions in each problem. Note: The second problem is on page 5. e-mail two.java files to csc121csudh@gmail.com Do not mail these files until you are done with the entire exam. No second mailing will be allowed 1. Compare (16 points) Write a class called Compare with the main method and greaterzero method as described below. The main method does the following: a. Reads in three integer values. b. Calls greaterzero, c. Prints out the three integers and the value that is returned by greaterzero as follows: It is booleanvalue that the integers num1, num2 and num3 are all greater than zero. Note: Italicized parts are replaced with actual values greaterzero does the following: a. Has three int variables as formal parameters b. Returns the value true if all three integers are all greater than 0, false otherwise. It is true that the integers 1, 4 and 7 are all greater than zero. It is false that the integers 0, 3 and 9 are all greater than zero. It is false that the integers 6, -1 and 8 are all greater than zero. It is false that the integers 4, 7 and -2 are all greater than zero. 4
2. CountPositive (32) Write a class CountPositive that does the following: The main method does the following: a. Requests from the user the dimensions for a two dimensional array. b. Calls the method createarray to create a two-dimensional integer array. c. Calls a method fullarray with a reference to the array created by createarray. d. Calls a method called greaterten with a reference to the array created by createarray. e. Prints out the final elements of the two-dimensional array row by row. f. The main method then prints out the final values in the array and a final sentence stating: g. It is (true/false) that there are more entries in the array greater than ten than less than or equal to ten. createarray does the following: a. Accepts the dimensions of a two dimensional int array as parameters. b. Creates a two-dimensional int array using the input parameters. c. Returns a reference to the array that was created. fullarray does the following: a. Has a two-dimensional int array as its formal parameter. b. Fills a two-dimensional int array with random integers from 1 to 20. greaterten does the following: a. Has a two-dimensional int array as its formal parameter. b. It then counts the number of entries greater than ten and returns true if there are more numbers greater than ten in the array. Otherwise it returns false. Please enter the rows and columns in a dimensional integer array 3 4 20 5 5 9 18 4 18 15 9 11 8 20 It is false that there are more entries in the array greater than ten than less than or equal to ten Please enter the rows and columns in a dimensional integer array 3 4 6 6 13 2 16 16 20 18 15 20 12 20 It is true that there are more entries in the array greater than ten than less than or equal to ten 5
6
7
8