1 CSE 142 Final Exam Review Problems
2 Question Types expressions array mystery inheritance mystery file processing array programming Critters classes
3 Expressions 1 Precedence: unary operators !, ++, --, +, - multiplicative operators *, /, % additive operators +, - relational operators, = equality operators ==, != logical and && logical or || assignment operators =, +=, -=, *=, /= In other words :( ) before * / %, before + -, before <>, before &&, before ||
4 Expressions 2 String concatenation: same precedence as integer + -, evaluated left-to-right with other + - operations "3" "3" "33" "334" + 5 "3345" Type promotion: done as needed when int and double are mixed 50 / 6 / /
5 Expression questions Evaluate the following expressions: * 5/3 2.5 * 4 * 3/ "." + (3 + 4) + 2 * 3 482/10/5/2.0 * /5
6 Expression answers Correct answers: * 5/ * 4 * 3/ true "." + (3 + 4) + 2 * 3 "5.76" 482/10/5/2.0 * /5 11.0
7 Array mystery question public static void mystery(int[] list) { for (int i = 2; i < list.length; i++) { list[i] = list[i] + list[i - 1] + list[i - 2]; } For each call below, indicate what value is returned: Method Call Value Returned int[] a1 = {8}; mystery(a1); _______________ int[] a2 = {2, 7, 12}; mystery(a2); _______________ int[] a3 = {3, 0, 1, 4, 7}; mystery(a3); _______________ int[] a4 = {0, 1, 2, 3, 4, 5}; mystery(a4); _______________ int[] a5 = {7, 4, -10, 8, 2}; mystery(a5); _______________
8 Array mystery answer public static void mystery(int[] list) { for (int i = 2; i < list.length; i++) { list[i] = list[i] + list[i - 1] + list[i - 2]; } Method Call Value Returned int[] a1 = {8}; mystery(a1); {8} int[] a2 = {2, 7, 12}; mystery(a2); {2, 7, 21} int[] a3 = {3, 0, 1, 4, 7}; mystery(a3); {3, 0, 4, 8, 19} int[] a4 = {0, 1, 2, 3, 4, 5}; mystery(a4); {0, 1, 3, 7, 14, 26} int[] a5 = {7, 4, -10, 8, 2}; mystery(a5); {7, 4, 1, 13, 16}
9 Inheritance mystery problem public class Bat extends Foo{ public void method1() { System.out.println(“Bat 1"); } public class Foo { public void method1() { System.out.println(“Foo 1"); } public void method2() { System.out.println("Foo 2"); } public String toString() { return “Foo"; } public class Car extends Bat { public void method1() { System.out.println(“Car 1"); } public String toString() { return “Car"; } public class Squid extends Foo { public void method2() { System.out.println(“Squid 2"); } public String toString() { return “Squid"; }
10 What would be the output of the following client code? methodFooBatSquidCar method1 method2 toString Foo[] items = {new Bat(), new Car(), new Foo(), new Squid()}; for (int i = 0; i < items.length; i++) { items[i].method1(); System.out.println(items[i]); items[i].method2(); System.out.println(); }
11 What would be the output of the following client code? methodFooBatSquidCar method1Foo 1Bat 1Foo 1Car 1 method2Foo 2 Squid 2Foo 2 toStringFoo SquidCar Foo[] items = {new Bat(), new Car(), new Foo(), new Squid()}; for (int i = 0; i < items.length; i++) { items[i].method1(); System.out.println(items[i]); items[i].method2(); System.out.println(); }
12 Inheritance mystery answer Foo[] items = {new Bat(), new Car(), new Foo(), new Squid()}; for (int i = 0; i < items.length; i++) { items[i].method1(); System.out.println(items[i]); items[i].method2(); System.out.println(); } The code produces the following output: Foo Foo 2 Car 1 Car Foo 2 Foo 1 Foo Foo 2 Foo 1 Squid Squid 2
13 Programming question tips Recognize which programming tools to use to solve each problem. Repeat actions a specific number of times: for loop. Decide between several logical choices: if/else statements. Repeat an unknown number of times: while loop. Processing input a line at a time, a token at a time, or both Arrays start counting at zero. Look at for loop bounds carefully, (eg. do you mean <= or < ?) Read the problems carefully! Does it want you to print a result, or return it? What values does the method use for computation? Are these values parameters, are they read from a Scanner, etc.? What type of value (if any) does the method return? Have you handled all special cases? What if the integer is 0, or negative? What if the string has no letters? What if there is only one word in the string? Many words? Get your thoughts onto the page. A partial answer is better than none at all. Writing the correct method header will earn at least 1 point. If you can solve all of the problem except one part, leave that part blank or write what you wanted to do as a comment. Keep your eye on the clock. (Even with 1 hour and 50 minutes)