CS 12300 Exam 2 Review The exam is on Thursday, November 8. The exam is over the following sections from the textbook, Sections 3.1, 3.2, 3.3, Sections 4.1, 4.2, 4.3, 4.4. Here is a list of the Self-Check Problems that have been assigned from Chapters 3 and 4. Chapter 1: 1, 2, 3, 4, 5, 8, 10, 12, 13, 14, 15, 16, 19, 20, 21, 23, 24, 25, 26. Chapter 2: 1, 2, 4, 7, 8, 12, 13, 15, 17, 18, 19, 20, 21, 22, 23, 27, 29. The topics on the exam are: 1) method parameters a) declaring a method with parameters b) calling a method with parameters 2) method return value a) declaring a method with a return value b) calling a method with a return value 3) the Math class 4) definition of an object 5) using objects a) using common methods from the String class b) using common methods from the Scanner class 6) if-statements 7) if-else-statements 8) nested if-else-statements 9) the Character class and how it differs from the String class 10) using return-statements with if-statements and for-loops Here are a few more review problems. Problem 1) Suppose that String s = "one two three four"; What are the values of the following expressions? (a) s.indexOf("e") (b) s.lastIndexOf("e") (c) s.substring( s.indexOf("e"), s.lastIndexOf("e") ) (d) s.indexOf("z") (e) s.substring(3,8).trim().length() (f) s.substring(0,3) + "2" + s.substring(8) (g) s.substring(4,5).toUpperCase() + s.substring(5,7) Problem 2) Suppose the String variable ssn holds a social security number in the form "xxx-xx-xxxx". Write an expression whose value is the last four digits of the social secuity number contained in ssn. Problem 3) Consider the following code fragment. if ( p < 0 ) System.out.println( "p is negative" ); if ( p == 0 ) System.out.println( "p is zero" ); if ( p > 0 ) System.out.println( "p is positive" ); Rewrite this code fragment using nested if-else-statements. Problem 4) Give the output of the following program segment if: (a) x=1 and y=1, (b) x=2 and y=2. if (x > 1) if (y > 2) Sysytem.out.println(2*x-y); else Sysytem.out.println(2*x+y); else if (y > 2) Sysytem.out.println(x-y); else Sysytem.out.println(x+y); Problem 5) Explain what the following program segment does. Then add a pair of braces so that what the resulting code prints out is always correct. if ( x == y ) if ( x == 0 ) System.out.println("x and y are both zero"); else System.out.println("x and y are not equal"); Problem 6) Explain the possible values that the variable w can have after the following code is executed. Assume that u and v are int's declared elsewhere in the program. int w = 0; if (u > 0) w++; if (v > 0) w++; Problem 7) Explain the possible values that the variable x can have after the following code is executed. Assume that u and v are int's declared elsewhere in the program. int x = 0; if (u > 0) x++; else if (v > 0) x++; Problem 8) Explain the possible values that the variable y can have after the following code is executed. Assume that u and v are int's declared elsewhere in the program. int y = 0; if (u > 0) y++; else y--; if (v > 0) y++; Problem 9) The following method contains a single error. Describe the error and show how to fix it. private static int findLastDigit(int n) { if (n > 0) return n % 10; else if (n < 0) return -n % 10; } Problem 10) Fill in the blanks so that the following method definition is correct. public static ________ max2( _______ a, int ______ ) { int result = a; if (b > a) result = ________; _________ result; } Problem 11) 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 Problem 12) The following method contains a single error. Describe the error and show how to fix it. private static boolean isMillionaire(int netWorth) { if (netWorth >= 1000000) return true; } Problem 13) 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 14) Suppose a program contains the following code fragment with line numbers given on the left. 101: Scanner console = new Scanner(System.in); 102: System.out.print("Please enter five numbers: "); 103: int n1 = console.nextInt(); 104: int n2 = console.nextInt(); 105: double n3 = console.nextdouble(); 106: double n4 = console.nextdouble(); 107: double n5 = console.nextdouble(); (a) Suppose that the user, when prompted by line 103, types the following (and hits the enter-key). 12 34 56 78.9 103 What data is contained in the Scanner object after line 104 executes? (b) Suppose that the user, when prompted by line 103, types the following (and hits the enter-key). 12 34 56 Which line of code will cause the next prompt to appear for the user? Problem 15) Write a single (not nested) for-loop that uses these two strings, String s1 = "************"; String s2 = " "; to create the following 12 lines of output. (Hint: Draw this output using substrings of the two given strings.) ************ *********** ********** ********* ******** ******* ****** ***** **** *** ** * Problem 16) 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 17) 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(); } } *************** *************** *************** * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ******** * * ******** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * ** *************** *************** ***************