cmpu102 midterm practice/sample questions see wiki schedule for complete set of topics being covered (through week 6). *OOP concepts *JVM basics *data fields/variables *data types: POD and class types *operators (including short hand operators) * if/else *switch/case *loops: for, while, do...while *special statements return, break, continue. *keywords *new keyword *static keyword *final keyword *this keyword *constructors //(20 pts.) The following questions refer to exception handling. //Assume there are no syntax errors in any of the //code fragments (A through D). //(A) (5 pts.) What is the output of the following code fragment? try { int num1 = Integer.parseInt("14"); System.out.println("Okay 1"); int num2 = Integer.parseInt("one"); System.out.println("Okay 2"); } catch (NumberFormatException nfe) { System.out.println("Error"); } (B) (5 pts.) What is the output of the following code fragment? try { int number = Integer.parseInt("-30"); if (number < 0) { throw new Exception("No negative numbers please."); } } catch (Exception e) { System.out.println("Error: "+ e.getMessage()); } catch (NumberFormatException nfe) { System.out.println("Cannot convert to int."); } (C) (5 pts.) Consider the following code fragment: try { int number = Integer.parseInt(JOptionPane.showInputDialog(null, "Input:")); if ( number != 0 ) { throw new Exception("Number not zero."); } } catch (NumberFormatException nfe) { System.out.println("Cannot convert to int"); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } (D) (5 pts.) Determine the output when the input is: a) -1 ______________________ b) 0 ______________________ c) 12XY ______________________ // //(20 points, 1 point each) Answer the following questions by circling T (true) or F (false) a) T or F: Java provides 8 numerical primitive types. short, int, long, float, double, boolean, char, byte... see chart in links section. b) T or F: When passing arguments to a method, the type of the argument must be assignment compatible with the type of the parameter declared in the method. c) T or F: When the keyword "void" is used in a method declaration, it means that there will be no value returned by the method. d) T or F: Each Java class type has a set of literal constant values that can be stored as the value of a variable for that type. e) T or F: Naming a variable A3xYz would cause a syntax error. f) T or F: Java belongs to the category of computer languages known as "machine" languages. g) T or F: The number of vertical spaces (blank lines) between statements in a Java program is irrelevant. h) T or F: The Java Virtual Machine (JVM) allows for Java programs to be built once and run on any operating system (OS). i) T or F: A return statement can be present in any method. j) T or F: Java programs do not need to import the java.lang package in order to use the shorthand notation for classes defined in that package. k) T or F: Naming an int variable 8Ball would cause a syntax error. l) T or F: Once all syntax errors are corrected, a Java program will execute as intended. l) T or F: Once all syntax errors are corrected, a Java program will execute as coded. m) T or F: Given the following statements: char letter = '3'; int number = 8; the subsequent state- ment int result = letter + number; will return a syntax error. n) T or F: The value returned, if the value of the int variable count is 0 and the value of the int variable limit is 10, as the result of evaluating the expression (count == 0) && (limit < 20) is true. o) T or F: An if decision statement must be followed by at least one else statement with which it is paired //(10 points) Write a method called multEven that takes an int n as input and returns an int that is the product //of all even whole numbers between 2 and n. Use a while loop in your solution. // (5 points) Circle 5 syntax errors in the following program and explain why each is an error: // 1. /* 2. Program Problems 3. */ 4. class Problem 1{ 5. public static myMethod(String st) 6. { 7. i = 5; 8. double backUp = 1.0/5; 9. return backUp 10. } 11. } //(5 points) Give the output of the following code if the call to this method is: ==> PetStore.buyCompanion(0); (assume there are no syntax errors) public class PetStore { public static void buyCompanion(int k) { switch (k + 3) { case 1: System.out.println("Puppy"); case 2: System.out.println("Macaw"); break; case 3: System.out.println("Kitten"); case 4: System.out.println("Goldfish"); break; default: System.out.println("Cricket"); break; } } } (15 points) Rewrite the class in the last problem using an if, else if, else multibranch statement instead of a switch to get the same results as the switch statement.--_mixed 007462DE8525851A_--