 You can do it! CSCI A201/A597/I210 Final Examination Summer II 2003 (Good luck and do well!)

Directions:
• Please read the questions carefully and select the ONE ANSWER that is best, then mark the corresponding space on the answer sheet for each question.

• All correct answers are worth exactly one point. Leaving a question unanswered will count as a zero. There is no penalty for guessing. DO NOT MARK more than one answer for each question.

• Don't forget to write your name and student ID on the scannable answer sheet provided. You have 75 minutes to complete the test. The test is open-book, open-notes (no computers).

Good luck and do well!

 1. Consider the following condition. `(x % 4 == 0 || (x % 100 == 0 && x % 400 == 0))` Which of the following represents a simplification of it? `((x % 100) == 0)`   `((x % 400) == 0)`   `((x % 100) != 0)`   `((x % 400) != 0)`   none of the above

 2. Consider the following condition. `! (a == ! false)` Which of the following represents a simplification of it. `a`   `!a`   `a && ! a`   `a || ! a`   none of the above

 3. Consider the following program fragment. `int x = 12, y = 0; if (x < 10) if (x > 5) y = 1; else y = 2;` What value does y have at the end? `0`   `1`   `2`   cannot be determined with the information given   this program fragment would not compile

 4. Consider the following program fragment. `int x = 8, y = 0; if (x < 6) { if (x > 5) y = 1; } else y = 2;` What value does y have at the end? `0`   `1`   `2`   cannot be determined with the information given   this program fragment would not compile

 5. Consider the following program fragment. `int x = 36, y = 8; if (x < 10) { if (x > 5) y = 1; else y = 2; }` What value does y have at the end? `0`   `1`   `2`   cannot be determined with the information given   this program fragment would not compile

 6. Consider the following program fragment. `int x = 8, y = 0; if (x < 10) ; if (x > 5) y = 1; else y = 2; ` What value does y have at the end? `0`   `1`   `2`   cannot be determined with the information given   this program fragment would not compile

 7. How many question marks will be printed by the following code fragment? ```for (int i = -10; i <= 10; i = i + 3) ; System.out.print("?"); ``` `1`   `2`   `3`   `6`   other

 8. How many question marks will be printed by the following code fragment? ```for (int i = 7; i >= 0; i--) System.out.print("?"); ``` `7`   `8`   `9`   `10`   other

 9. Consider the following fragment. What does it print? ```int x = 3, y = 11; x = x + y; x = x - y; y = x - y; System.out.println("(" + x + ", " + y + ")"); ``` `(3, 11)`   `(11, 3)`   `(14, 14)`   `(7, 7)`   other

 10. Consider the following fragment. What does it print? ```int x = 11, y = 3; y = x - y; x = y + x; y = y - x; System.out.println("(" + x + ", " + y + ")"); ``` `(3, 11)`   `(11, 3)`   `(14, 14)`   `(7, 7)`   other

 11. Consider the following fragment. What does it print? ```int[] v = new int; for (int i = 0; i < 10; i++) v[i] = 2 * i; System.out.print(v + v)``` `6`   `8`   `10`   `31`   other

 12. Consider the following fragment. What does it print? ```int[] v = { 2, 4, 6, 8, 10, 12}, m = { 1, 3, 5, 7, 9, 11}; for (int i = 0, sum = 0; i < 10; i++) sum -= (m[i] - v[i]); System.out.print(sum)``` `6`   `36`   `-6`   `-84`   other

 13. Consider the following program. What does it print? ```int[] a = { 0, -1, 1, -2, 2, -3, 3}; int fun = a[a.length - 1]; for (int i = a.length - 1; i >= 0; i--) if (a[i] <= fun) fun = a[i]; System.out.println(fun); ``` `0`   `3`   `-3`   `-1`   other

 14. Consider the following program fragment. ```public class One { public static void main(String[] args) { int size = 10, nuf, fun; for (nuf = 0; nuf < size; nuf++) { for (fun = 0; fun < size; fun++) if ( fun == 0 || fun == size - 1 || __________________ ) System.out.print(" *"); else System.out.print(" "); System.out.println(); } } }``` What's need for the output to resemble an `N` ? ``` ```   `fun == size / 2`   `nuf == size / 2`   `fun + nuf == size - 1`   `fun - nuf == size - 1`   other

 15. Consider the following command line invocation: `java One Two Three Four Five` Assume your program contains this line inside `System.out.println(args[args.length - 1]);` What does this line print? `One`   `Two`   `Three`   `Five`   other

 16. Consider the following code fragment, what does it print? ```String plum = "nectarine"; System.out.println(plum.substring("plum".length(), "peach".length()); ``` `t`   `a`   `ta`   `ar`   other

 17. Consider the following code fragment, what does it print? ```String plum = "nectarine"; System.out.println((char)('A' + plum.length())); ``` `J`   `O`   `K`   `E`   other

 18. Consider the following code fragment, what does it print? ```String plum = "nectarine"; System.out.println((char)('A' + "plum".length())); ``` `O`   `T`   `H`   `E`   `R`

 19. Does this source code compile? ```class Momerath { } class Tove extends Momerath { } class Kroger { public static void main(String[] args) { Tove a = new Momerath(); } }``` `yes`   `no`

 20. Does this source code compile? ```class Tove { } class Momerath extends Tove { } class Kroger { public static void main(String[] args) { Tove a = new Momerath(); } }``` `yes`   `no`

 21. Does this source code compile? ```class Wabe { } class Tove extends Wabe { } class Momerath extends Tove { } class Kroger { public static void main(String[] args) { Wabe a = new Momerath(); } }``` `yes`   `no`

 22. Does this source code compile? ```class Wabe { } class Tove extends Wabe { } class Momerath extends Tove { } class Kroger { public static void main(String[] args) { Tove a = new Wabe(); } }``` `yes`   `no`

 23. You compile and run this code, what is the output? ```class One { int red; One() { this(10); } One (int blue) { red += blue; } int report() { System.out.println(red); } public static void main(String[] args) { One x = new One(20); x = new One(30); x = new One(); x.report(); } } ``` `10`   `20`   `30`   `60`   `other`

 24. If you compile and run this code, what is the output? ```class One { public static void main(String[] args) { int[][] a = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {6, 2, 7} }; System.out.println(a[a - 1]); } } ``` `2`   `4`   `6`   `8`   other

 25. If you compile and run this code, what is the output? ```class One { public static void main(String[] args) { int[] x = {1, 2, 3}; fun(x); // focus here!! System.out.println(-x + x + x); } public static void fun(int value) { value = value - 1; } } ``` `0`   `2`   `4`   `6`   other

 26. If you compile and run this code, what is the output? ```class One { public static void main(String[] args) { int[] x = {1, 2, 3}; fun(x, 0); // focus here!! System.out.println(-x + x + x); } public static void fun(int[] value, int index) { value[index] = value[index] - 1; } } ``` `0`   `2`   `4`   `6`   other

 27. What gets printed when you compile and run the following program? ```class One { public static void main(String[] args) { System.out.println(one(one(two(one(two(1, 2), 1), 2), two(1, 2)), 1)); } public static int one(int b, int a) { return a + b; } public static int two(int b, int a) { return a - b; } } ``` `0`   `2`   `4`   `6`   `other`
 28. Assuming that the following program fragment is syntactically correct... ```int[][] a = new int; ... a = fun(a, a); ``` ... select the correct header for method `fun`.   `int[][] fun(int[] a, int b) `   `int fun(int[] a, int[][] b)`   `int[] fun(int a, int[] b)`   `int fun(int[][] a, int b)`   none of the above

 29. Assuming that the following program fragment is syntactically correct... ```int[][] a = new int; ... a = fun(a, a); ``` ... select the correct header for method `fun`.   `int[][] fun(int[] a, int b)`   `int fun(int[] a, int[][] b)`   `int[] fun(int a, int[] b)`   `int fun(int[][] a, int b)`   none of the above

 30. Assuming that the following program fragment is syntactically correct... ```int[][] a = new int; ... a = fun(a, a); ``` ... select the correct header for method `fun`.   `int[][] fun(int a, int[][] b)`   `int fun(int[] a, int[][] b)`   `int[] fun(int a, int[] b)`   `int fun(int[][] a, int b)`   none of the above

 31. What is the result of attempting to compile and run this code? ```class Alpha { public static void main(String[] args) { System.out.println("... will not run"); } public static void main() { System.out.println("... won't compile"); } } ``` The program compiles and runs and prints ... won't compile   The program compiles but when you try to run it complains about main.   The program does not compile because main is not defined correctly.   The program compiles and runs and prints ... will not run   none of the above

 32. What is the result of attempting to compile and run this code? ```abstract class Alpha { int value; Alpha (int value) { this.value = value; System.out.println(value); } public static void main(String[] args) { Beta f = new Beta(2003); } } class Beta extends Alpha { Beta (int value) { super(value); } }``` The code will compile but nothing will appear in the standard output.   Code won't compile because there's no default no-args constructor for Alpha.   The code will not compile because Alpha is abstract.   The code will compile and run.   none of the above

 33. What is the result of attempting to compile and run this code? ```class Alpha { public static void main(String[] args) { Alpha f = new Beta(); Alpha g = new Gamma(); System.out.println(f.test(g.test(3))); } int test(int i) { return (i + 2); } } class Beta extends Alpha { } class Gamma extends Beta { int test(int i) { return (i + 1); } } ``` The code compiles and runs succesfully, and outputs 3.   The code compiles and runs succesfully, and outputs 4.   The code compiles and runs succesfully, and outputs 5.   The code does not compile.   none of the above

 34. What is the result of attempting to compile and run this code? ```class Alpha { public void main(String[] args) { Beta f = new Beta(3); } Alpha (int i) { } } class Beta extends Alpha { Beta(int i) { } }``` The code does not compile because Beta's constructor is empty.   The code does not compile because Alpha doesn't define a no-args constructor.   The code compiles and runs succesfully.   The code does not compile because Beta does not define a no-args constructor.   none of the above

 35. What is the result of attempting to compile and run this code? ```class Alpha { public static void main(String[] args) { Beta f = new Beta(); } } class Beta extends Alpha { }``` The code does not compile because Beta does not define a no-args constructor.   The code does not compile because Beta does not define any constructors whatsoever.   The code compiles and runs succesfully, with no output.   The code does not compile because Alpha doesn't define a no-args constructor.   none of the above

 36. What is the result of attempting to compile and run this code? ```abstract class Alpha { abstract void complain(String s); } class Beta extends Alpha { void complain(String s) { System.out.println(s); } } class Tester { public static void main(String[] args) { Beta f = new Beta(); f.complain("There's a tomato in every automaton."); } }``` The program will compile and run.   The code will not compile because Beta has no default no-arg constructor.   The code does not compile because Beta is abstract.   The code does not compile because Tester does not inherit from Alpha.   none of the above

`A201/A597 TTFN Exam Thu Aug 14 2003 in LH201`