 Java Garbage Collector. CSCI A201/A597 and I210 Final Examination Spring 2001

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. All incorrect answers count as -1/4 (with a minus). 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!

For the following 5 (five) exercises consider the following method definitions:

```public static int f(int x) {
return h(h(x));
}
public static int g(int x) { return k(k(x)); }
public static int h(int x) { return 1 + k(x); }
public static int k(int x) { return x - 1; }```
Let's get `frilled` involved:
```frilled.cs.indiana.edu%cat One.java
class One {

public static int f(int x) {
return h(h(x));
}
public static int g(int x) { return k(k(x)); }
public static int h(int x) { return 1 + k(x); }
public static int k(int x) { return x - 1; }

public static void main(String[] args) {
System.out.println(g(2));
System.out.println(g(h(2)));
System.out.println(k(g(2) + h(2)));
System.out.println(f(0) + f(1) + f(2));
System.out.println(f(2));
}

}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
0
0
1
3
2
frilled.cs.indiana.edu%```
 1. What does `g(2)` evaluate to? 0    1    2    3   `other` Note: To save space I wrote `other` where I would have put "None of the above."

 2. What does `g(h(2))` evaluate to? 0    1    2    3   `other`

 3. What does `k(g(2) + h(2))` evaluate to? 0    1    2    3   `other`

 4. What does `f(0) + f(1) + f(2)` evaluate to? 0    1    2    3   `other`

 5. What does `f(2)` evaluate to? 0    1    2    3   `other`

 6. `"0"` is an example of what kind of constant? `int`   `char`   `boolean`   `String`   `other`

 7. Assume that `x` is integer variable. Simplify the following expression: `!(((x - 1) >= 4) && ((x - 1) <= 4))` `true`   `false`   `x != 5`   `x == 5`   `other`

Warning: Don't overlook the logical negation (bang!) at the front.

 8. Consider the following two program fragments. Assume that `x` is an int variable. ``` // fragment 1 | // fragment 2 if (x == 5) | if (x == 5) x = x + 1; | x = x + 1; else | if (x != 5) x = 8; | x = 8; ``` Which of the following statements is false? The two fragments are not logically equivalent. `x` always has the value of `8` after executing fragment 2. `x` is always divisible by 2 (even) after executing fragment 1. `x` has either a value of `5` or a value of `8` after executing fragment 2. The first fragment could be simplified as: `x = (x != 5) ? 8 : 6;`

Reminder: Last option above uses the selection operator (page 187 in your text).

Statement 2 is true, that makes 4 false ("I think I said too much..."). The others are blatantly true.

 9. You compile and run this program. What is the output (or outcome)? ```class Wan { static int value; Wan() { value += 1; } public static void main(String[] args) { Wan a = new Wan(); a = new Wan(); a = new Wan(); System.out.println(Wan.value); } } ``` 0    1    2    3   `other`

 10. Assume that `x` and `y` are integer variables and the following nested if statement. ```if (x > 3) { if (x <= 5) y = 1; else if (x != 6) y = 2; else y = 3; } else y = 4;``` If `y` has a value of `3` after executing the above program fragment, then what do you know about `x`? `x != 6` `x == 6` `x == 4` `x != 4` `(x > 3) && (x <= 5)` 1    2    3    4    5
 11. Which of the following expressions does not print a `4` (four)? `System.out.println('M' - 'J' + 1);` `System.out.println((4 * 5 + 2) / 7 + 1);` `System.out.println("012".length() + 1);` `System.out.println(8 - 3 - 2 + 1);` `System.out.println('3' + 1);` 1    2    3    4    5 Note: There are no typos!
```frilled.cs.indiana.edu%cat Four.java
class Four {
public static void main(String[] args ) {
System.out.println('M' - 'J' + 1);
System.out.println((4 * 5 + 2) / 7 + 1);
System.out.println("012".length() + 1);
System.out.println(8 - 3 - 2 + 1);
System.out.println('3' + 1);
}
}
frilled.cs.indiana.edu%javac Four.java
frilled.cs.indiana.edu%java Four
4
4
4
4
52
frilled.cs.indiana.edu%```

 12. After the following statement is executed, `int x = (int)(Math.random() * (30 - 10) + 20);` `22`   `37`   `27`   `42`   `32` which of these numbers could NOT possibly be contained in `x`?

 13. Given the following array declaration what does `a[a]` evaluate to? ```int[][] a = { {2, 4, 3}, {1, 3, 2}, {1, 2, 3}, {3, 4, 1} }; ``` 1    2    3    4    `other`
```frilled.cs.indiana.edu%cat Five.java
class Five {
public static void main(String[] args) {
int[][] a = { {2, 4, 3},
{1, 3, 2},
{1, 2, 3},
{3, 4, 1}
};

System.out.println(

a[a]

);

}
}
frilled.cs.indiana.edu%javac Five.java
frilled.cs.indiana.edu%java Five
3
frilled.cs.indiana.edu%```

 14. If `fun` is defined as below: ```public static int[] fun(int size, int valu) { int[] a = new int[size]; for (int i = 0; i < size; ++i) a[i] = valu - i; return a; }``` What will `fun(3, 3)` evaluate to? (Yes, it compiles and runs fine). 0    1    2    3    `other`
```frilled.cs.indiana.edu%cat Six.java
class Six {
public static void main(String[] args) {
System.out.println(

fun(3, 3)

);
}
public static int[] fun(int size, int valu) {
int[] a = new int[size];
for (int i = 0; i < size; ++i) a[i] = valu - i;
return a;
}
}
frilled.cs.indiana.edu%javac Six.java
frilled.cs.indiana.edu%java Six
1
frilled.cs.indiana.edu%```

 15. What is printed if you compile and run the following program? ```class Nine { public static void main(String[] args) { int[] x = {1, 2, 3}; triple(x); System.out.println(x + x + x); } public static void triple(int[] y) { y = 3 * y; } } ``` `6`   `8`   `10`   `12`   `other`

 16. What is printed if you compile and run the following program? ```class Nine { public static void main(String[] args) { int[] x = {1, 2, 3}; triple(x); System.out.println(x + x + x); } public static void triple(int m) { m = 3 * m; } } ``` `6`   `8`   `10`   `12`   `other`

 17. You compile and run this program. What is the output? ``` class A { public static void main(String[] args) { A a = new A(); B b = new B(); A c = new B(); System.out.println(a.fun() + b.fun() + c.fun()); } int fun() { return 0; } } class B extends A { int fun() { return 1; } } ``` `0`   `1`   `2`   `3`   `other`

 18. What letter should go in both blank spaces for the following program to compile? ```class A extends D { } class D extends B { } class C extends D { public static void main(String[] args) { ____ seq[] = new ____; seq = new A(); seq = new B(); } } class B { } class E extends Object { }``` `A`   `B`   `C`   `D`   `E`

 19. Assume the program above is in a file `E.java` and you want to compile and run it. ```C:\> javac E.java C:\> java ____``` `A`   `B`   `C`   `D`   `E` Fill in the blank with the name of the class that you need to run.

 20. Assume you compile the following program: ```public class A { static int index; public static void main(String[] args) { int[] x = {6, 7, 8, 9, 10}; index = Integer.parseInt(args); System.out.print(fun(x) + " "); System.out.print(x[index] + " "); } public static int fun(int[] x) { x[index] = x[index] - 1; return x[index]; } }``` `7 7`   `7 8`   `8 7`   `8 8`   `other` What gets printed when you run it as follows: `java A 3`

 21. Assume that `expr1` evaluates to `true`, `expr2` evaluates to `false`, and `expr3` evaluates to `true`. Which of the following expressions evaluates to `false`? ` expr2 || expr1 && expr3`   ` expr2 || (expr1 && expr3)`   `(expr2 || expr1) && expr3`   `!expr2 && expr1 && expr3` 1   2   3   4   `other`

 22. What gets printed when you compile and run the following program? ```public class A { public static void main(String[] args) { System.out.println(nuf(fun(5, nuf(fun(4, 3), 2)), 1)); } public static int fun(int a, int b) { return a - b; } public static int nuf(int b, int a) { return a - b; } }``` `-3`   `-2`   `-1`   `0`   `other` There are no typos, so please don't rush!

 23. You compile and run this contrived program. What is the output? ```class Account { public static void main(String[] args) { Account e = new Account(); Account f = new Account(10); System.out.println(e.getBalance() + f.getBalance()); } Account() { this(20); } Account (int n) { balance = n; } int balance; int getBalance() { return balance + 30; } }``` `10`   `30`   `60`   `90`   `other`

 24. Consider the following code fragment: ```class One { public static void main(String[] args) { int t = Integer.parseInt(args); if (t >= 2) System.out.print("IN"); if (t <= 3) System.out.print("DIA"); if (t >= 1) System.out.print("NA"); } } ``` `INNA`   `INDIA`   `INDIANA`   `DIANA`   `other` What gets printed when you compile and run it as follows: `java One 1 2 3` Work carefully: there are no typos.

 25. Exactly how many question marks will appear on the screen when the following program fragment is executed? ```for (int i = 0; i < 10; i += 3) for (int j = 0; j < 10; j += 3) System.out.print("?"); System.out.println(); ``` `4`   `16`   `25`   `36`   `other`

 26. What does the following program's output most closely resemble? ```public class Nine { public static void main(String[] args) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == 9 || j == 0 || 9 == j) { System.out.print(" " + j); } else { System.out.print(" "); } } System.out.println(); } } }``` `C`   `Z`   `U`   `N`   `other`
```frilled.cs.indiana.edu%cat Nine.java
public class Nine {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (i == 9 || j == 0 || 9 == j) {
System.out.print(" " + j);
} else {
System.out.print("  ");
}
}
System.out.println();
}
}
}
frilled.cs.indiana.edu%javac Nine.java
frilled.cs.indiana.edu%java Nine
0                 9
0                 9
0                 9
0                 9
0                 9
0                 9
0                 9
0                 9
0                 9
0 1 2 3 4 5 6 7 8 9
frilled.cs.indiana.edu%```

 27. What does `System.out.println(mix("one", "two"))` print if `mix` is defined as below? ``` public static String mix(String one, String two) { String ans = ""; int olen = one.length(), tlen = two.length(); for (int i = 0; i < olen && i < tlen; i++) { ans = one.charAt(i) + ans + two.charAt(i); } return ans; } ``` `ontowe`   `ewotno`   `owtone`   `enotwo`   `other`

 28. What does `System.out.println(mix("one", "two"))` print if `mix` is defined as below? ``` public static String mix(String one, String two) { String ans = ""; int olen = one.length(), tlen = two.length(); for (int i = 0; i < olen && i < tlen; i++) { ans = two.charAt(i) + ans + one.charAt(i); } return ans; } ``` `ontowe`   `ewotno`   `owtone`   `enotwo`   `other`

 29. What does `System.out.println(mix("one", "two"))` print if `mix` is defined as below? ``` public static String mix(String one, String two) { String ans = ""; int olen = one.length(), tlen = two.length(); for (int i = 0; i < olen && i < tlen; i++) { if (i % 2 == 1) { ans = one.charAt(i) + ans + two.charAt(i); } else { ans = two.charAt(i) + ans + one.charAt(i); } } return ans; } ``` `ontowe`   `ewotno`   `owtone`   `enotwo`   `other` There are no typos so, please, think carefully.

 30. What does `System.out.println(mix("one", "two"))` print if `mix` is defined as below? ``` public static String mix(String one, String two) { String ans = ""; int olen = one.length(), tlen = two.length(); for (int i = 0; i < olen && i < tlen; i++) { if (i % 2 == 0) { ans = one.charAt(i) + ans + two.charAt(i); } else { ans = two.charAt(i) + ans + one.charAt(i); } } return ans; } ``` `ontowe`   `ewotno`   `owtone`   `enotwo`   `other`

 31. Assume the following declaration: `int[][] a = { { 1, 2 }, { 3, 4, 5}, { 6 } }; ` What does the following code print? ```int sum = 0; for (int i = 0; i < a.length; i++) sum += a[i].length; System.out.println(sum); ``` `3`   `6`   `90`   `180`   `other`

 32. Assume the following declaration: `int[][] a = { { 1, 2 }, { 3, 4, 1 }, { 2, 3, 4 }, { 1 } }; ` What does the following code print? `System.out.println(a); ` `1`   `2`   `3`   `4`   `other`

 33. You compile and run this program. What is the output that it produces? ```class One { int a = 8; int b = 1; void fun() { b *= 2; a /= 2; } void report() { System.out.print(a + " " + b + " "); } public static void main(String[] args) { One alpha = new One(); alpha.fun(); alpha.fun(); alpha.fun(); alpha.report(); } }``` `1 8`   `4 2`   `8 1`   `4 8`   `other`

 34. You compile and run this program. What is the output that you obtain? ```class One { int a = 8; int b = 1; void fun() { b *= 2; a /= 2; } void report() { System.out.print(a + " " + b + " "); } public static void main(String[] args) { One alpha = new One(); One beta = new One(); alpha.fun(); beta.fun(); alpha.fun(); beta.report(); } }``` `1 8`   `4 2`   `8 1`   `4 8`   `other`

 35. You compile and run this program. What is the output that you obtain? ```class One { int a = 8; static int b = 1; void fun() { b *= 2; a /= 2; } void report() { System.out.print(a + " " + b + " "); } public static void main(String[] args) { One alpha = new One(); One beta = new One(); alpha.fun(); beta.fun(); alpha.fun(); beta.report(); } }``` `1 8`   `4 2`   `8 1`   `4 8`   `other`

 36. What is the result of attempting to compile and run the following code? ```class Blow { public static void main(String[] args) { int[] x = { 9, 1, 3, 7, 5 }; fun(x); for (int i = 0; i < x.length; i++) System.out.print(x[i]); } public static void fun(int[] a) { for (int i = 0; i < a.length - 1; i++) if (a[i] < a[i + 1]) { int temp = a[i + 1]; a[i + 1] = a[i]; a[i] = temp; } } } ``` `13759`   `93751`   `13579`   `97531`   `other`

 37. What is the result of attempting to compile and run the following code? ```class Example { public static void main(String[] args) { Example e = new Example(); System.out.println(e.fun() + e.fun(1)); } int fun() { return 1 + fun(1); } int fun(int n) { return 1 + n; } } ``` `1`   `3`   `5`   `7`   `other`

 38. Assuming that the following program fragment is syntactically correct... ```int[] a = new int; : : int n = f38(a, a); : :``` ...select the correct header for the function `f38`. `public static int f38(int v, int[] a)` `public static int f38(int[] a, int v)` `public static int[] f38(int v, int[] a)` `public static int[] f38(int[] a, int v)` `1`   `2`   `3`   `4`   `other`

 39. Assuming that the following program fragment is syntactically correct... ```int[] a = new int; : : int[] n = f39(a, a); : :``` ...select the correct header for the function `f39`. `public static int f39(int v, int[] a)` `public static int f39(int[] a, int v)` `public static int[] f39(int v, int[] a)` `public static int[] f39(int[] a, int v)` `1`   `2`   `3`   `4`   `other`

 40. Assuming that the following program fragment is syntactically correct... ```int[][] a = new int; : : int n = f40(0, a); : :``` ...select the correct header for the function `f40`. `public static int f40(int v, int[] a)` `public static int f40(int[] a, int v)` `public static int[] f40(int v, int[] a)` `public static int[] f40(int[] v, int a)` `1`   `2`   `3`   `4`   `other`

 41. Assume the following declaration: `int[][] a = { { 0, 1, 2 }, { 3, 4, 5}, { 6 } }; ` What does the following code print? ```int count = 6; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) if (a[i][j] % 2 == 0) count = 1 - count; System.out.println(count); ``` `7`   `6`   `5`   `4`   `other`

 42. Which of the following numbers is closest to the output of this line if embedded in a complete program, that is then compiled and run? ```int a, b; a = 10; b = 4; System.out.println(b * a / b + a % b);``` `10`   `20`   `30`   `40`   `50`

 43. What is the output produced by the following code when embedded in a complete program? ```if (false) System.out.print(0); else System.out.print(1); boolean x = (1 < 2) && (! (4 < 3)); if (x) System.out.print(2); else System.out.print(3); ``` `02`   `12`   `03`   `13`   `other`

 44. What is the output produced by the following code when embedded in a complete program? ```boolean x; if (x = (false && (false || true))) System.out.print(0); else System.out.print(1); if (! x) System.out.print(2); else System.out.print(3); ``` `02`   `12`   `03`   `13`   `other`
(Think carefully. Yes, it compiles).
 45. What is the output produced by the following code when embedded in a complete program? ```boolean x; if (x = (false && false || true)) System.out.print(0); else System.out.print(1); if (! x) System.out.print(2); else System.out.print(3); ``` `02`   `12`   `03`   `13`   `other`

Short Note on Java Garbage Collectors: The character depicted on the front page is Dilbert's 'sanitation engineer'. He is a mysterious character that has inexplicable knowledge of all subjects from science to philosophy. In Dilbert comic strips he shows up occasionally to solve impossibly complex problems for Dilbert or Dogbert. I hope he will bring you luck on this final exam!
```Tue May 1 17:00:00 EST 2001 (A201/A597/I210, Rawles Hall 100) ```