CS 12300 Final Exam Review The final exam is on Thursday, December 13. The final exam is over the whole semester. The exam is over the following sections from the textbook. Sections 1.2, 1.3, 1.4, 1.5, Sections 2.1, 2.2, 2.3, 2.4, 2.5, Sections 3.1, 3.2, 3.3, Sections 4.1, 4.2, 4.3, 4.4, Sections 5.1, 5.2, 5.3, Sections 7.1, 7.2, 7.3, Sections 8.1, 8.2, 8.3. The topics covered since the second exam are: 1) while-loop do-while-loop fence-post loops boolean type 2) arrays, array reference variables, array objects, array initialization, for-each loop aliases, array parameters to methods, array return value from methods. 3) objects, object reference variables, classes, instance variables (fields) instance methods (instead of static methods), dot notation, constructors. Here is a list of the Self-Check Problems that have been assigned from Chapters 5 and 7. Chapter 5: 1, 2, 5, 10, 11, 12, 13, 14, 17, 18, 19, 21, 22, 24, 25. Chapter 7: 1, 3, 4, 5, 7, 10, 15, 16, 17, 19, 20, 21. Below are review problems for the material we have covered since the second exam. For the final exam you should also study Exam 1 and Exam 2 plus the review problems and Self-Check Problems for those two exams. Problem 1) What does the following code fragment print out? String s = "************"; int i = 0; while ( i < 11 ) { System.out.println( s.substring( i++ ) ); } while ( i > 0 ) { System.out.println( s.substring( i-- ) ); } Problem 2) Let s be the following string of nine spaces. String s = " "; For each part below, write a single while-loop that uses s to create the given picture. a) Create the following ten lines of output. * * * * * * * * * * b) Create the following ten lines of output. * * * * * * * * * * c) Create the following six lines of output. * * * * * * * * * * * Problem 3) Write a single while-loop that uses these two strings, String s1 = "************"; String s2 = " "; to create the following 12 lines of output. ************ *********** ********** ********* ******** ******* ****** ***** **** *** ** * Problem 4) The body of the following method has three statements. Rewrite the body so that it has just a single statement which is the simplest statement that will work. (See "Boolean Zen" on pages 344-7 of the textbook.) private static boolean isInside(int n) { boolean inside = false; if (0 < n && n < 100) inside = true; return inside; } Problem 5) Simplify the code for the following method as much as you can. public static int inTheInterval(int n) { if (n < 0) return -1; else if (0 <= n && n <= 1) return 0; else if (n > 1) return 1; return 42; // we never get here } Problem 6) If variable a is a reference to an array, what happens if the following statement is executed? Briefly explain why. System.out.println(a[a.length]); Problem 7) Assume that A is a reference to an array containing integers. Write a for-loop that multiplies every element in A by 10. Problem 8) Write a program segment that declares an array reference variable, constructs an array object, assigns the array object to the array variable, and then uses a loop to fill the array object with the following integers. -50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50. Problem 9) Write a method called sumArray that accepts a reference to an array of doubles and returns the sum of the values stored in the array. Problem 10) Write a method called sumSubArray that accepts a reference to an array of doubles and two ints. If the two ints are valid indices, then the method should return the sum of the values stored in the array whose indices are between the two ints (inclusive). If the two ints are not valid indices, then the method should return zero. Problem 11) Suppose that the variables a and b are references to arrays of integers. What is the value of b[0] and b[1] after the following statements have been executed? Explain why. Your explanation should include a simple drawing of Java’s memory. a[0] = 1; b[0] = 2; b = a; b[1] = 3; a[1] = 4; Problem 12) After the following code has executed its last line, how many array objects have reference variables referring to them? Explain your answer wit a drawing of Java's memory. int[] a = {21, 11, 10, 9}; int[] b = new int[6]; int[] c = a; a = new int[5]; b = a; int[] d = b; Problem 13) Show the output of the following program. Explain what the second for-loop is doing. public class Problem { public static void main(String[] args) { int[] a = new int[8]; for (int i = 0; i < a.length; i++) { a[i]=i+1; System.out.println(a[i]); } int result = 0; for (int i = 0; i < a.length / 2; i++) { result += a[i] * a[a.length-i-1]; } System.out.println("Result = " + result); } } Problem 14) Suppose that the parameter A is always a reference to an array of size n >= 2 containing integers from 1 to n-1, inclusive, with exactly one number repeated. Write the method public static int findRepeatedNumber(int[] A) that returns the value of the repeated number in the array A. (Hint: Use two nested for-loops.) Problem 15) Suppose I have defined a class called Thing. The following line performs three distinct steps. What are they? How would you rewrite this single line as two lines? What distinct steps do each of those two lines perform? Thing rose = new Thing();