CS 12300 Exam 2 Review The exam is on Thursday, November 7. The exam is over the following sections from the textbook, Sections 3.1, 3.2, 3.3, Sections 4.1, 4.2, 4.4. Here is a list of the Self-Check Problems that have been assigned from Chapters 3 and 4. Chapter 3: 1, 2, 3, 4, 5, 6, 9, 10, 12, 13, 14, 15, 16, 18, 19, 20, 21, 23, 24, 25, 26. Chapter 4: 1, 2, 4, 7, 8, 12, 13, 15, 17, 18, 19, 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) the definition of an object 5) using objects (calling methods on objects) a) using common methods from the String class b) using common methods from the Scanner class 6) if-statement 7) if-else-statement 8) nested if-else-statements 9) 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 security number contained in ssn. Problem 3) Suppose the String variable pn is a phone number in the form "xxx-xxx-xxxx". Write an expression whose value is the phone number contained in pn converted into the format "(xxx) xxx-xxxx". So the string "219-989-2273" would be converted to "(219) 989-2273". You can assume that the phone number in pn is formatted correctly. Problem 4) 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 5) 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.println("Please enter a sentence."); 103: String s1 = console.next(); 104: String s2 = console.next(); 105: String s3 = console.nextLine(); 106: String s4 = console.next(); 107: String s5 = console.nextLine(); The user, when prompted by line 103, types the following and hits the enter-key. one, two, three, four (a) What is the value of each of s1 and s2 after line 104 executes? (b) What data is contained in the Scanner object after line 104 executes? (c) Which line of code after line 103 will cause the next prompt to appear for the user? Briefly explain why. Problem 6) 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 7) What is printed out by the following code fragment? int n = 15; if ( 10 <= n && 20 <= n ) { System.out.println( "A" ); } else { System.out.println( "B" ); } if ( 10 < n || 20 < n ) { System.out.println( "C" ); } else { System.out.println( "D" ); } if ( n != 15 ) { System.out.println( "E" ); } else { System.out.println( "F" ); } if ( n == 15 ) { System.out.println( "G" ); } else { if ( n > 0 ) { System.out.println( "H" ); } } Problem 8) 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 9) 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 10) 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 11) 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 12) 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 13) 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 14) 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 15) 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 16) 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 17) 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 18) 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 19) 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 20) 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(); } } *************** *************** *************** * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ******** * * ******** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * ** *************** *************** ***************