Name:_______________________ Covers Chapter 6 CSCI 1301 Introduction to Programming Armstrong Atlantic State University Instructor: Y. Daniel Liang (50 minutes) Part I: Multiple Choice Questions: (1 pts each) 1. Suppose array a is int[] a = {1, 2, 3}, what is a[0] - a[2]? a. b. c. d. 1 2 3 None of the above 2. An array reference variable can be used in which of the following ways? a. b. c. d. 3. a. b. c. d. e. As a local variable As a parameter of a method As a return value of a method All of the above Which of the following are valid array declarations? char[] charArray = new char[26]; int[] words = new words[10]; char[] charArray = "Computer Science"; double[3] nums = {3.5, 35.1, 32.0}; None of the above 4. Consider the following code fragment: int[] list = new int[10]; for (int i = 0; i <= list.length; i++) { list[i] = (int)(Math.random() * 10); } Which of the following statements is true? a. list.length must be replaced by 10 b. The loop body will execute 10 times, filling up the array with random numbers. c. The loop body will execute 10 times, filling up the array with zeros. d. The code has a runtime error indicating that the array is out of bound. 5. Assume the signature of the method xMethod is as follows. public static void xMethod(double[] a) Which of the following could be used to invoke xMethod? 1 a. b. c. d. e. xMethod(5); xMethod({3, xMethod(new xMethod(new None of the 4}); int[2]); double[2]); above. 6. Given the following statement int[ ] list = new int[10]; list.length has the value a. 10 b. 9 c. The value depends on how many integers are stored in list. d. None of the above. 7. Given the following statement int[ ] list = new int[10]; a. The array variable list array of 10 int values. b. The array variable list array of 9 int values. c. The array variable list d. The array variable list e. None of the above. contains a memory address that refers to an contains a memory address that refers to an contains ten values of type int. contains nine values of type int. 8. Given the following declaration: int[ ][ ] m = new int[5][6]; Which of the following statements is true? a. b. of c. d. The name m represents a two-dimensional array of 30 int values. m[2][4] represents the element stored in the 2nd row and the 4th column m. m.length has the value 6. m[0].length has the value 5. 9. In the following code, what is the printout for list2? class Test { public static void main(String[] args) { int[] list1 = {3, 2, 1}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = list2.length - 1; i >= 0; i--) System.out.print(list2[i] + " "); } } a. b. c. d. e. 1 3 0 2 0 2 2 1 1 1 3 1 2 0 3. 2 10. Analyze the following code: public class Test { public static void main(String[] args) { int[] x = {0, 1, 2, 3, 4, 5}; xMethod(x, 5); } public static void xMethod(int[] x, int length) { for (int i = 0; i < length; i++) System.out.print(" " + x[i]); } } a. The program b. The program c. The program d. The program exception. displays displays displays displays 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4. 4 and then raises a runtime exception. 4 5. 4 5 and then raises a runtime 11. Analyze the following code: public class Test { public static void main(String[] args) { int[] x = new int[5]; for (int i = 0; i < x.length; i++) x[i] = i; System.out.println(x[i]); } } a. The program displays 0 1 2 3 4. b. The program displays 4. c. The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBounds exception. d. The program has syntax error because i is not defined in the last statement in the main method. 12. Assume double[] scores = {1, 2, 3, 4, 5}, what value does java.util.Arrays.binarySearch(scores, 3.5) return? a. 0 b. -4 c. 3 d. -3 e. 4 Part II: Show the printout of the following code: a. (1 pts) public class Test { public static void main(String[] args) { int[] a = {1, 2}; swap(a[0], a[1]); System.out.println("a[0] = " + a[0] + " a[1] = " + a[1]); 3 } public static void swap(int n1, int n2) { int temp = n1; n1 = n2; n2 = temp; } } b. (1 pts) public class Test { public static void main(String[] args) { int[] a = {1, 2}; swap(a); System.out.println("a[0] = " + a[0] + " a[1] = " + a[1]); } public static void swap(int[] a) { int temp = a[0]; a[0] = a[1]; a[1] = temp; } } (4 pts) Given the following program, show the values of the array in the following figure: c. public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i; } values[0] = values[1] + values[4]; } } After the array is created d. After the first iteration in the loop is done After the loop is completed After the last statement in the main method is executed 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 (1 pts) Show the printout of the following program: public class Test { 4 public static void main(String[] args) { double[] numbers = {1, 4.3, 5.55, 3.4}; double[] x = numbers; numbers[0] = 300; System.out.println(x[0]); } } Part III: a. (3 pts): Suppose a matrix is created as follows: int[][] matrix = new int[4][5]. 1. Write a statement without using a loop to add its second column. 2. Write a loop to add its second column. b. (3 pts): Write a method that computes the average of the values in an array of doubles. The header of the method is as follows: public static double average(double[] x) 5 c. (3 pts) Write a method that returns the index of the smallest element in an array of integers. If there are more than one such element, return the smallest index. public static int indexOfSmallestElement(int[] list) 6 d. (7 pts): Write a method that returns a new array with no duplicates. The header of the method is as follows: public static int[] removeDuplicate(int[] x) For example, if x is {1, 3, 1, 2, 3, 5}, new array {1, 3, 2, 5} removeDuplicate(x) returns a (Hint: To be given in the class) 7 Key Part I: Multiple Choice Questions: (1 pts each) 1. d 2. d 3. a 4. d 5. d 6. a 7. a 8. a 9. d 10. a 11. d 12. b Part II. (3 pts) Find and fix all errors in the following program: (A) public class Test { public static void main(String[] args) { double[] list = {3, 4, 5, 6.3}; xMethod(list); } public static double xMethod(double[] list) { for (int i=0; i<list.length; i++); { sum += list[i]; } return sum; } } (B) high > low should be high >= low Part III: Show the printout of the following code: a. (2 pts each) public class Test { public static void main(String[] args) { int[] a = {1, 2}; 8 swap(a[0], a[1]); System.out.println("a[0] = " + a[0] + " a[1] = " + a[1]); } public static void swap(int n1, int n2) { int temp = n1; n1 = n2; n2 = temp; } } a[0] = 1 a[1] = 2 b. (2 pts each) public class Test { public static void main(String[] args) { int[] a = {1, 2}; swap(a); System.out.println("a[0] = " + a[0] + " a[1] = " + a[1]); } public static void swap(int[] a) { int temp = a[0]; a[0] = a[1]; a[1] = temp; } } a[0] = 2 a[1] = 1 c. (4 pts) Given the following program, show the values of the array in the following figure: public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i; } values[0] = values[1] + values[4]; } } 9 After the array is created After the first iteration in the loop is done After the loop is completed After the last statement in the main method is executed 0 0 0 0 0 0 0 5 1 0 1 1 1 1 1 1 2 0 2 0 2 2 2 2 3 0 3 0 3 3 3 3 4 0 4 0 4 4 4 4 300 d. Part IV: a. matrix[0][1] + matrix[1][1] + matrix[2][1] + matrix[3][1] int sum = 0; for (int i = 0; i < 5; i++) sum = sum + matrix[i][1]; b. double[] list = new double[0]; System.out.println(list.length); System.out.println(average(list)); /** if x is null, return 0. */ public static double average(double[] x) { if (x == null) return 0; double total = 0; for (int i = 0; i < x.length; i++) total += x[i]; return total / x.length; } c. public class Test { // Main method public static void main(String[] args) { int[] list = {1, 2, 4, 5, 10, 100, 2, -22}; 10 System.out.println("The min is " + minIndex(list)); } public static int minIndex(int[] list) { int min = list[0]; int minIndex = 0; for (int i = 1; i < list.length; i++) if (min > list[i]) { min = list[i]; minIndex = i; } return minIndex; } } d. public class Test { public static int[] removeDuplicate(int[] x) { int[] temp = new int[x.length]; int tempSize = 0; for (int i = 0; i < x.length; i++) { // Check if x[i] is already in temp boolean duplicate = false; for (int j = 0; j < tempSize; j++) if (x[i] == temp[j]) duplicate = true; if (!duplicate) { // Add x[i] into temp temp[tempSize] = x[i]; tempSize++; } } int[] result = new int[tempSize]; System.arraycopy(temp, 0, result, 0, tempSize); return result; } public static void main(String[] args) { int[] x = {1, 3, 1, 2, 3, 5}; int[] result = removeDuplicate(x); for (int i = 0; i < result.length; i++) System.out.print(result[i] + " "); } } 11