CS 12300 Exam 2 Review The exam is on Wednesday, November 2. The exam is over Chapters 4 - 5 from the textbook, https://learn.zybooks.com/zybook/PNWCS12300KraftFall2022 The most important ideas from those chapters are in these sections. Sections 4.1, 4.4, 4.6, 4.13, 4.28, 4.29. Sections 5.2, 5.3, 5.4, 5.5, 5.7, 5.8. Here are a few review problems. Problem 1) What does the following code fragment print out? Briefly explain why. 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) Write a single (not nested) for-loop that uses substrings of these two strings, String s1 = "************"; String s2 = " "; to create the following 12 lines of output. ************ *********** ********** ********* ******** ******* ****** ***** **** *** ** * Problem 3) Let s be the following string of nine spaces. String s = " "; For each part below, write a single (not nested) for-loop that uses substrings of 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 4) Write a code fragment, using nested for-loops, whose output will look like the following. (Hint: Don't try to compute the output numbers from the loop indices. To get the output numbers, just have a counter variable that counts up from 10, and print out the counter.) 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 Problem 5) Write nested for-loops to produce the following output. Your for-loops only need to produce exactly these seven lines (your for-loops can use "magic numbers", there is no size parameter). 6 55 444 3333 22222 111111 0000000 Problem 6) In the following program, complete the definition of the main method so that the program, when it is run, would produce the following output. * * * * * 12345-12345-12345-12345-12345-12345-12345-12345-12345-12345-12345-12345-12345-12345- * * * * * public class Problem6 { public static void methodA() { System.out.print("12345"); } public static void methodB() { System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println("*"); } public static void methodC() { System.out.println("*"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); } public static void main(String[] args) { } } Problem 7) Fill in the blanks so that the following method definition is correct. public static ________ max2( _______ a, int ______ ) { if ( ______ >= ______ ) { return _______ ; } else { _______ b; } } Problem 8) Fill in the blanks so that the following method definition is correct. public static ________ min2( _______ a, double ______ ) { _______ result; if ( a - b < ______ ) { result = _______ ; } else { result = b; } return result; } Problem 9) Write a complete definition for a public static method called closeEnough that takes a double and an integer and returns true if the double is within one unit of the integer. For example closeEnough(3.9, 5) -> false closeEnough(4.1, 5) -> true closeEnough(6.0, 5) -> true closeEnough(6.1, 5) -> false Problem 10) Write a definition for a public static void method that has a parameter called size and prints a line with 1+size number of characters on the line and with the first character being an asterisk, the last character being an equal sign, and the rest of the characters being ampersands. Problem 11) The following method contains a single error. Describe the error and show how to fix it. public static boolean isMillionaire(int netWorth) { if (netWorth >= 1000000) { return true; } } Problem 12) Which of the following methods returns the minimum of its four inputs? Which is easiest to understand? public static int min(int a, int b, int c, int d) { if (a <= b && a <= c && a <= d){ return a; } if (b <= c && b <= d){ return b; } if (c <= d){ return c; } return d; } public static int min(int a, int b, int c, int d) { int min = a; if (b < min){ min = b; } if (c < min){ min = c; } if (d < min){ min = d; } return min; } public static int min(int a, int b, int c, int d) { if (a <= b) { if (a <= c) { if (a <= d){ return a; } else{ return d; } } if (c <= d){ return c; } else{ return d; } } if (b <= c) { if (b <= d){ return b; } else{ return d; } } else if (c <= d){ return c; } return d; } public static int min(int a, int b, int c, int d) { return min2(min2(a, b), min2(c, d)); } public static int min2(int a, int b) { if (a <= b){ return a; } else{ return b; } } Problem 13) What is printed out by the following program segment? for (int i = 0; i < 4; ++i) { for (int j = 0; j < 5; ++j) { if ( (i+j) % 2 == 0 ) { System.out.print("0 "); } else { System.out.print("1 "); } } System.out.print("\n"); } Problem 14) 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. (Hint: When does the method return true and when does the method return false?) public static boolean isInside(int n) { boolean inside = false; if (0 < n && n < 100){ inside = true; } return inside; } Problem 15) Here are two code fragments. Below them are three pictures. These fragments draw two of the three pictures. Determine which fragment draws which picture. Explain your answer by detailing which part of the if-statements draw which part of the pictures. Be very specific about how you can tell which fragment draws which picture. (In each code fragment, assume that n is an odd integer.) // assume that n is odd // assume that n is odd for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int j = 1; j <= n; j++) { if (i==1 || i==n || if (i==1 || i==n || j==1 || j==n || j==1 || j==n || (i==(n/2)+1 && j>=(n/2)+1) || (i==(n/2)+1 && j<=(n/2)+1) || (i==j && i<=(n/2)+1) ) { (i==j && i>=(n/2)+1) ) { System.out.print("*"); System.out.print("*"); }else{ }else{ System.out.print(" "); System.out.print(" "); } } } } System.out.println(); System.out.println(); } } *************** *************** *************** * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ******** * * ******** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * ** *************** *************** ***************