Department of Computer Science CSc 2310: Test 2---KEY FALL 2011 1. Closed book and closed notes. You do not need a calculator. You may use extra paper for scratch work or if you need more room for answers. However, make sure your name is on EVERY piece of paper that you turn in. 2. If you finish before 12:15 am, you may bring your test to the instructor at the front of the room. 3. Budget your time carefully. Read over the entire exam before starting. There are a total of 85 points. 4. Write legibly. If I cannot read it, I cannot give you credit for it. 5. Make sure your answer clearly indicates the result (versus your scratch work). Draw a box or a circle around your answers if needed. 6. Assume any code segment is embedded in a correct program. 7. When showing program output, you do not have to indicate the exact spacing but do show when an output starts on a new line. 8. If anything is ambiguous or unclear, ask the instructor. I pledge that I have neither received nor given unauthorized aid on this examination. Signed: ___________ ______________________________________________ PRINT:_______________________________________________________________ 1 PART 1(20pts): 1. Which one of the following properties do class methods and instance methods not have in common: (3pts) (a) being able to access instance variables (b) being able to have local variables (c) being able to have parameters (d) being able to return a value Answer: (a) being able to access instance variables 2. A class that will be used for creating objects is said to be _______________ . (2pts) Answer: Instantiable 3. Which one of the following classes is not used to create objects: (3pts) (a) Color (b) Font (c) Math (d) String Answer: (c)Math 4. Which way of declaring the main method is illegal? (3pts) (a) public static void main(String args[]) { ... } (b) public static void main(String[] argv) { ... } (c) public static void main(String argv[]) { ... } (d) public static void main(String args) { ... } Answer: (d) public static void main(String args) { ... } 5. Which one of the following is not a primitive type? (3pts) (a) Boolean (b) char (c) float (d) int (e) none of the above? Answer: (e) none of the above 6. How many bits are used to store a value of type short? (3pts) Answer: 16 2 7. Which one of the following expressions correctly generates a random number between 1.5 and 3.5? (3pts) (a) 2 + Math.random() * 1.5 (b) 1.5 + Math.random() * 2 (c) 2.5 + Math.random() (d) Math.random() * 3.5 (e) None of the above Answer: (b) 1.5 + Math.random() * 2 PART 2(38pts): 8. What will be printed when the following code is executed? (5pts) int a = 1, b = 0, c; try { c = a / b; System.out.println("Division completed"); } catch (ArithmeticException e) { System.out.println("You can't catch me!"); } System.out.println("Time to move on"); Answer: You can't catch me! Time to move on 9. The following statement contains a single error. Describe the error and show how to fix it. (5pts) try { remainder = i % j; } catch (ArithmeticException) { System.out.println("Error during remainder operation"); } ANSWER: ArithmeticException e 10. For each of the following shapes, list all methods capable of drawing that shape (using a single method call): 3 Choose from among the following methods: drawLine, drawRect, drawRoundRect, drawOval, drawArc, drawPolygon, and drawPolyline.( each 2pts) (a) (b) (c) (d) (e) Answer: (a) drawPolygon, drawPolyline (b) drawPolyline (c) drawRoundRect (also give credit for drawPolygon and drawPolyline) (d) drawPolygon, drawPolyline (e) drawArc (also give credit for drawPolyline) 11. Write a for statement that will print out the numbers 3, 6, 9, 12, 15, 18, 21, 24, 27, and 30. (5pts) Answer: Here is one of many possible answers: for (int number = 3; number <= 30; number = number + 3) { System.out.println(number); } 12. Convert the following if...else if... structure to a switch structure. That is, write a switch statement that will accomplish the equivalent of the code below. (You may assume choice is an integer variable that already has been assigned some value.) (5 pts) if (choice == 1) System.out.println("Choice 1"); else if (choice == 2) System.out.println("Choice 2"); else if (choice == 3) 4 System.out.println("Choice 3"); else System.out.println("Bad choice"); Answer: This would translate directly to the switch statement: switch (choice) { case 1: System.out.println("Choice 1"); break; case 2: System.out.println("Choice 2"); break; case 3: System.out.println("Choice 3"); break; default: System.out.println("Bad choice"); 13. Show the values of the variables i and j after each series of statements has been executed. (4 pts each) (a) i = 7; j = i-- * 2 + 1; (b) i = 7; j = 2 * i++ + 1; ANSWER (a)Value of i:________ . Value of j: :________. (b) Value of i:________. Value of j: :________ . Answer: (a) Value of i: 6. Value of j: 15. (b) Value of i: 8. Value of j: 15. 5 PART 3(27pts): 14. Examine the following code carefully and write the output on the space provided. (10 pts) class Stars { public static void main(String[] args) { for( int i = 0; i < 3; i++ ) { System.out.print( "*" ); } System.out.println(); for( int i = 1; i < 4; i ++) { for( int j = i; j <= 5; j++ ) { System.out.print( "*" ); } System.out.println(); } } } SOLUTION *** ***** **** *** 15. Show the output of the following program. (5pts) public class Problem80 { public static void main(String[] args) { 6 double f = .375, g; int i = 1; while (true) { g = f * i; if (g == (int) g) break; i *= 2; } System.out.println((int) g + "/" + i); } } Answer: 3/8 16. Show the output of the following program. (12 pts) public class Problem_test2 { public static void main(String[] args) { final String CODES = " ALAAL FLOFL GEOGA MICMI MISMS OHIOH"; String str = " Atlanta, Georgia "; int i = str.indexOf(","); String str1 = str.substring(0, i + 1).trim(); String str2 = str.substring(i + 1).trim(); int j = CODES.indexOf(" " + str2.substring(0, 3).toUpperCase()); String str3 = CODES.substring(j + 4, j + 6); System.out.println(str1 + " " + str3); } } Answer: ATLANTA GA 7 PART 4(10pts): Bonus: Show the output of the following program. (10pts) public class Problem83 { public static void main(String[] args) { if (f("Ada")) System.out.println("Ada"); if (f("Panama")) System.out.println("Panama"); if (f("Madam, I'm Adam")) System.out.println("Madam, I'm Adam"); } private static boolean f(String str) { int j = str.length(); for (int i = 0; i < str.length(); i++) { if (!Character.isLetter(str.charAt(i))) continue; do { j--; } while (!Character.isLetter(str.charAt(j))); if (Character.toUpperCase(str.charAt(i)) != Character.toUpperCase(str.charAt(j))) return false; } return true; } } Answer: Ada Madam, I'm Adam 8