 Good luck and do well. CSCI A201/A597/I210 Second Midterm Examination Spring Semester 2002

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 two hours (120 minutes) to complete the test. The test is open-book.

Good luck and do well!

 1. You compile and run the following program. What does it print? ```class One { public static void main(String[] args) { boolean p = false, q = true, r = true; System.out.println(! p || q && r); } } ``` `true`   `false`   `other`

 2. Consider the following condition. `! (x % 3 != 0 || x % 2 != 0) ` Which of the following represents a simplification of it (obtained perhaps using DeMorgan). `((x % 2) == 0)`   `((x % 3) == 0)`   `((x % 6) == 0)`   `((x % 6) != 0)`   `none of the above`

 3. Assume `a` is of type `boolean` and consider the following condition. `(a && !a) == false` Which of the following represents a simplification of it. `a`   `!a`   `true`   `false`   `none of the above`

 4. Consider the following program fragment. `int x = 1, y = 1; if (x < 2) if (x > 1) y = 2; else y = 3; else y = 4; ` What value does y have at the end? `1`   `2`   `3`   `4`   `other`

 5. Consider the following program fragment. `int x = 1, y = 1; if (x > 2) y = 2; y = y + 1; else y = y + 1; ` What value does y have at the end? `1`   `2`   `3`   `4`   `other`

 6. Consider the following program fragment. `int x = 1, y = 1; if (x > 2) { y = 2; y = y + 1; } else y = y + 1; ` What value does y have at the end? `1`   `2`   `3`   `4`   `other`

 7. Consider the following program fragment. `int x = 1, y = 1; if (x < 2) y = 2; else y = y + 1; y = y - 1;` What value does y have at the end? `1`   `2`   `3`   `4`   `other`

 8. How many question marks will be printed by the following code fragment? ```for (int i = -10; i <= 10; i = i + 5) System.out.print("?"); ``` `5`   `10`   `15`   `20`   `other`

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

 10. How many question marks will be printed by the following code fragment? ```for (int i = 7; i >= 0; i--) { if (i % 3 == 0) System.out.print("?"); } ``` `0`   `1`   `2`   `3`   `other`

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

 12. Consider the following fragment. What does it print? ```int x = 2, y = 1; y = x + y; x = y - x; y = y - x; System.out.println("(" + x + ", " + y + ")"); ``` `(1, 1)`   `(1, 2)`   `(2, 1)`   `(2, 2)`   `other`

 13. Consider the following fragment. What does it print? ```int[] v = new int; for (int i = 0; i < 10; i++) v[i] = i % 3; System.out.print(v + v);``` `1`   `2`   `3`   `4`   `other`

 14. Consider the following fragment. What does it print? ```int[] v = { 1, 2, 3, 3, 2, 1}, m = { 6, 5, 4, 4, 5, 6}; int sum = 0; for (int i = 0; i < v.length; i++) sum += (v[i] - m[i]); System.out.print(sum);``` `-18`   `-16`   `16`   `18`   `other`

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

 16. Consider the following program fragment. What would it print? ```int[] a = { 6, 5, 4, 3, 2, 1 }; boolean done = false; while (! done) { done = true; for (int i = 0; i < a.length - 1; i++) if (a[i] > a[i + 1]) { done = false; int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } for (int i = 0; i < a.length; i++) System.out.print(a[i]); System.out.println(); ``` `123456`   `654321`   `543216`   `612345`   `other`

 17. Consider the following program fragment. What would it print? ```int[] a = { 6, 5, 4, 3, 2, 1 }; for (int i = 0; i < a.length - 1; i++) if (a[i] > a[i + 1]) { int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } for (int i = 0; i < a.length; i++) System.out.print(a[i]); System.out.println(); ``` `123456`   `654321`   `543216`   `612345`   `other`

 18. 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 ( nuf == 0 || nuf == size - 1 || __________________ ) System.out.print(" *"); else System.out.print(" "); System.out.println(); } } }``` What's needed for the output to resemble an `Z`? `fun == size - 1`   `fun == 0 `   `fun + nuf == size - 1`   `fun + nuf == 0`   `other`

 19. Consider the following command line invocation: `java One appear ` Assume your program (`One.java`) contains this line inside `System.out.println(args.substring(3));` What does this line print? `appear`   `pear`   `ear`   `pea`   `other`

 20. Consider the following code fragment, what does it print? ```String seven = "eleven"; System.out.println(seven.substring(0, 3)); ``` `sev`   `eve`   `ele`   `lev`   `other`

 21. Consider the following code fragment, what does it print? ```String one = "BC"; System.out.println((char)('A' + one.length())); ``` `A`   `ABC`   `BC`   `C`   `other`

 22. Consider the following code fragment, what does it print? ```String one = "3"; System.out.println((char)('3' + one.length() + 3)); ``` `0`   `3`   `6`   `9`   `other`

 23. Does this source code compile? ```class One { } class Two extends One { } class Three { public static void main(String[] args) { Two a = new One(); } }``` `yes`   `no`

 24. Does this source code compile? ```class Three { } class Two extends Three { } class One { public static void main(String[] args) { Three a = new Two(); } }``` `yes`   `no`

 25. Does this source code compile? ```class One { } class Two extends One { } class Three extends Two { } class Four { public static void main(String[] args) { Two a = new Three(); } }``` `yes`   `no`

 26. Does this source code compile? ```class Four { } class Three extends Four { } class Two extends Three { } class One { public static void main(String[] args) { Three a = new Four(); } }``` `yes`   `no`

 27. You compile and run this code, what is the output? ```class One { int one; One() { this(2); } One (int two) { one += two; } void report() { System.out.println(one); } public static void main(String[] args) { One a = new One(4); a = new One(2); a = new One(); a.report(); } } ``` `0`   `2`   `4`   `6`   `other`

 28. You compile and run this code, what is the output? ```class One { static int one; One() { this(2); } One (int two) { one += two; } void report() { System.out.println(one); } public static void main(String[] args) { One a = new One(4); a = new One(2); a = new One(); a.report(); } } ``` `0`   `2`   `4`   `6`   `other`

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

 30. 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); System.out.println(x - x + x); } public static void fun(int value) { value = 1 - value; } } ``` `6`   `5`   `4`   `3`   `other`

 31. 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); System.out.println(x - x + x); } public static void fun(int[] value) { value = 1 - value; } } ``` `6`   `5`   `4`   `3`   `other`

 32. What gets printed when you compile and run the following program? ```class One { public static void main(String[] args) { System.out.println(fun(fun(fun(1, 2), 3), nuf(4, 5))); } public static int fun(int a, int b) { return b; } public static int nuf(int a, int b) { return a; } } ``` `1`   `2`   `3`   `4`   `other`

 33. What gets printed when you compile and run the following program? ```class One { public static void main(String[] args) { System.out.println(nuf(fun(nuf(fun(1))))); } static int fun(int value) { return value - 1; } static int nuf(int value) { return value + 1; } } ``` `1`   `2`   `3`   `4`   `other`

 34. What does the next fragment print if embedded in a complete program? ```String two = "to".charAt(0) + "two".charAt(1) + "ew"; System.out.println(two); ``` `230ew`   `twew`   `two`   `235ew`   `other`

 35. What does `mix("abc", "def")` return if `mix` is defined as below? ```public static String mix(String one, String two) { String answer = ""; int lengthOne = one.length(), lengthTwo = two.length(); for (int i = 0; i < lengthOne && i < lengthTwo; i++) { answer = one.charAt(i) + (two.charAt(i) + answer); } return answer; } ``` `cbadef`   `abcfed`   `cfbead`   `fedabc`   `other`

 36. What does `mix("abc", "def")` return if `mix` is defined as below? ```public static String mix(String one, String two) { String answer = ""; int lengthOne = one.length(), lengthTwo = two.length(); for (int i = 0; i < lengthOne && i < lengthTwo; i++) { answer = two.charAt(i) + answer + one.charAt(i); } return answer; } ``` `cbadef`   `abcfed`   `cfbead`   `fedabc`   `other`

 37. What does `mix("abc", "def")` return if `mix` is defined as below? ```public static String mix(String one, String two) { String answer = ""; int lengthOne = one.length(), lengthTwo = two.length(); for (int i = 0; i < lengthOne && i < lengthTwo; i++) { answer = one.charAt(i) + answer + two.charAt(i); } return answer; } ``` `cbadef`   `abcfed`   `cfbead`   `fedabc`   `other`

 38. 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[], int)`   `int[] fun(int, int[])`   `int fun(int, int[])`   `int fun(int[], int[])`   `none of the above`

 39. 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[], int)`   `int[] fun(int, int[])`   `int fun(int, int[])`   `int fun(int[], int[])`   `none of the above`

 40. 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[], int)`   `int[] fun(int, int[])`   `int fun(int, int[])`   `int fun(int[], int[])`   `none of the above`

 41. You compile this program ... ```class One { public static void main(String[] args) { System.out.println(args[args.length - args.length()]); } } ``` ... then run it as follows: `java One abc defghi jk lmnopq` What does the program print (if anything)?   `abc`   `def`   `jkl`   `the program compiles but does not run`   `other`

 42. You compile this program ... ```class One { public static void main(String[] args) { System.out.println(args); } } ``` ... then run it as follows: `java One Two Three One Two Three` What does the program print (if anything)?   `One`   `Two`   `Three`   `the program compiles but does not run`   `other`

 43. You compile and run this program. What's the output? ```public class Four { public static void main(String[] args) { Four a = new Four(); a.nuf(1); a.fun(1); a.nuf(2); a.fun(2); a.nuf(3); System.out.println(Four.m + a.n); } void nuf(int p) { this.n = this.n + p; Four.m = Four.m - p; } void fun(int p) { int temp = p; this.n = Four.m; Four.m = temp; } int n; static int m; } ``` `0`   `1`   `2`   `3`   `other`

 44. You compile and run this program. What's the output? ```public class Five { public static void main(String[] args) { Five a = new Five(); Five b = new Five(); a.nuf(1); a.fun(1); b.nuf(2); b.fun(2); a.nuf(3); System.out.println(Five.m + a.n); } void nuf(int p) { this.n = this.n + p; Five.m = Five.m - p; } void fun(int p) { int temp = p; this.n = Five.m; Five.m = temp; } int n; static int m; } ``` `0`   `1`   `2`   `3`   `other`

`A201/A597/I210 Second Midterm Exam Wed Apr 3 2002 in RH100`