Ho-Ho-Ho-Ho! ``` CSCI A201/A597 Midterm Examination Summer II 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. 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!

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; }```
 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`

To verify all this one can try:

```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%```

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

 7. Assume that `x` is an 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).

 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!

 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. What is the output produced by the following three lines (when embedded in a complete program)? ```int x, y; x = 7; y = 5; System.out.println("1" + x + y); ``` `13`   `17`   `75`    `175`   `other`

 14. What is the output produced by the following three lines (when embedded in a complete program)? ```int x, y; x = 9; y = 10; System.out.println("1" + x * y); ``` `0`   `19`   `91`   `1910`    `other`

 15. What is the output produced by the following three lines (when embedded in a complete program)? ```int x, y; x = 9; y = 10; System.out.println("1" * x + y); ``` `0`   `19`   `91`   `1910`    `other`

 16. What is the output produced by the following two lines (when embedded in a complete program)? ```int a; a = 'Q' - 'M'; System.out.println(a); ``` `Q`   `L`   `M`    `4`   `other`

 17. What is the output produced by the following program fragment (when embedded in a complete program)? ```if ('Z' > 'B') System.out.print("1" + 3 / 2); System.out.print(2);``` `12`   `11`   `"1"1.5`   `2.5`    `other`

 18. What is the type of this expression: `"1" + 'a'` `int`   `char`   `boolean`   `double`    `other`

 19. What is the output produced by the following three lines (when embedded in a complete program)? ```int a, b; a = 3; b = 2; System.out.println(b * (a / b) + a % b);``` `1`   `2`    `3`   `4`   `other`

 20. What's the output produced by the following lines (when embedded in a complete program)? ```String quote = "There's a tomato" + "in every automaton!"; System.out.println(quote.substring(29, 35)); ``` `utomat`   `tamotu`   `otomat`   `tomato`    `other`

 21. What's the output produced by the following lines (when embedded in a complete program)? ```String a = "sun"; String b = "dried"; System.out.println( "tomato".substring(b.length() - a.length(), a.length()));``` `a`    `m`   `o`   `t`   `other`

 22. Given `String a = "tomato";` which of the following expressions is NOT of type `String`? `a` `"a"` `'a'` `a + a.length` `a.substring(0, 1) + 'a'` `1`   `2`    `3`   `4`   `5`

 23. What's the output produced by the following lines (when embedded in a complete program)? ```String a = "a"; a.toUpperCase(); System.out.println(a + "a" + a.toUpperCase());``` `aaa`    `aaA`   `AaA`   `AAA`   `other`

 24. Which of the following expressions is NOT logically equivalent to `(a >= b)`? `(b <= a)` `(b < a) || (b == a)` `(b == a) && (b < a)` `(b - a) <= 0 ` `(b <= a) && (a >= b)` `1`   `2`    `3`   `4`   `5`

 25. What's the output produced by the following lines (when embedded in a complete program)? ```int a = 100; if (a >= 100) System.out.print("a "); else if (a >= 10) System.out.print("b "); if (a <= 1000) System.out.print("c ");``` `a `   `a b `    `a c `   `a b c `   `other`

 26. Consider the following expression in which `m` is an `int`. `(m < 10) && (m > 12) ` Which of the following is a valid Java simplification of the expression above? `(m == 11)`   `true`   `(m != 11)`   `false`   `(10 > m > 12) ` `1`   `2`   `3`    `4`   `5`

 27. Assume `a` and `b` are integer variables. If `a` has a value of `2` after executing the following program fragment what was the value of `b` at the beginning of the fragment code? ```if (b > 2 && b < 6) a = 1; else a = 3; if (b % 2 == 0) a = a + 1; ``` `2`   `3`    `4`   `5`   `other`

 28. What is the output produced by the following three lines (when embedded in a complete program)? ```int a, b; a = 8; b = 3; System.out.println(b * a / b + a % b);``` `3`   `5`   `8`   `24`    `other`

 29. What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.) ```int x = 1; if (2 > x) System.out.print(1); else System.out.print(11); if (x < 5) { System.out.print(1); System.out.print(1); } ``` `1`   `11`    `111`   `1111`   `other`

 30. What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.) ```int x = 1; if (x > 2) { if (x < 5) System.out.print(1); } else System.out.print(1); System.out.print(11);``` `1`   `11`    `111`   `1111`   `other`

 31. What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.) ```int x = 2; while (x > 1) x = x - 1; System.out.print(x);``` `1`   `2`   `21`   `0`   `other`

 32. What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.) ```int i; for (i = 0; i < 3; i = i + 1) System.out.print(1); System.out.print(0); ``` `0`    `1110`   `11110`   `101010`   `10101010`

 33. What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.) ```if (2 <= 3) if (0 != 1) System.out.print(0); else System.out.print(1); System.out.print(2); if (2 > 3) if (0 == 1) System.out.print(3); else System.out.print(4); System.out.print(5); ``` `023`   `024`    `025`   `125`   `other`

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

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

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

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

 38. What is the output produced by the following code when embedded in a complete program? ```int x = 10, y = 3; while (x > 0 && y > 0) { x = x - y; } System.out.print(x);``` `0`   `-1`    `-2`   `-3`   `other`

 39. What is the output produced by the following code when embedded in a complete program? ```int x = 10, y = 3; while (x > 0 && y > 0) { x = x - y; y = y + 1; } System.out.print(x);``` `0`   `-1`    `-2`   `-3`   `other`

 40. What is the output produced by the following code when embedded in a complete program? (Note that the fragment may or may not be indented correctly so please think about it carefully.) ```int x; for (x = 0; x < 10; x = x + 2) if (x % 3 == 1) System.out.print(x); System.out.print(x % 3); ``` `4`    `41`   `02402`   `024102`   `other`

 41. Consider the following program, it prints out a pattern. Which of the following letter patterns is closest in shape to the pattern that the program produces? ```public class One { public static void main(String[] args) { int size = 10, x, y; for (x = 0; x < size; x++) { for (y = 0; y < size; y++) if ( y == 0 || y == size - 1 || (x - y == 0)) System.out.print(" *"); else System.out.print(" "); System.out.println(); } } }``` `Z`   `A`    `N`   `Y`   `other`

 42. Consider the following program, it prints out a pattern. Which of the following letter patterns is closest in shape to the pattern that the program produces? ```public class One { public static void main(String[] args) { int size = 10, x, y; for (y = 0; y < size; y++) { for (x = 0; x < size; x++) if ( y == 0 || y == size - 1 || (x + y == size - 1)) System.out.print(" *"); else System.out.print(" "); System.out.println(); } } }``` `Z`   `A`   `N`   `Y`   `other`

 43. 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`

 44. 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!

 45. 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`

 46. 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`

 47. 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`

 48. 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`

 49. 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`

 50. 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`

 51. 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`

 52. 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`

 53. 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`

 54. 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).
 55. 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).
`A201/A597 Midterm Exam Fri Jul 13 2001 in SW119`