![]() Java Garbage Collector. |
CSCI A201/A597 and I210 Final Examination Spring 2001 |
Good luck and do well!
For the following 5 (five) exercises consider the following method definitions:
Let's getpublic 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; }
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?
| |
| |
Note: To save space I wrote
|
2. What does g(h(2)) evaluate to?
| |
| |
3. What does k(g(2) + h(2)) evaluate to?
| |
| |
4. What does f(0) + f(1) + f(2) evaluate to?
| |
| |
5. What does f(2) evaluate to?
| |
| |
6. "0" is an example of what kind of constant?
| |
| |
7. Assume that x is integer variable. Simplify
the following expression: | |
!(((x - 1) >= 4) && ((x - 1) <= 4)) |
|
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.
Which of the following statements is false?// fragment 1 | // fragment 2 if (x == 5) | if (x == 5) x = x + 1; | x = x + 1; else | if (x != 5) x = 8; | x = 8; |
|
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); } } |
|
10. Assume that
x and
y are
integer variables and the following nested if statement.
Ifif (x > 3) { if (x <= 5) y = 1; else if (x != 6) y = 2; else y = 3; } else y = 4; y has a value of 3 after executing the
above program fragment, then what do you know about x ?
| |
|
|
11. Which of the following expressions does not print a 4 (four)?
| |
|
|
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); |
|
which of these numbers could NOT possibly be contained in x ?
|
13. Given the following array declaration what does a[a[3][2]][1] evaluate to?
| |
int[][] a = { {2, 4, 3}, {1, 3, 2}, {1, 2, 3}, {3, 4, 1} }; |
|
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[3][2]][1] ); } } 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:
| |
What willpublic 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; } fun(3, 3)[2] evaluate to? (Yes, it compiles and runs fine).
|
|
frilled.cs.indiana.edu%cat Six.java class Six { public static void main(String[] args) { System.out.println( fun(3, 3)[2] ); } 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[0] + x[1] + x[2]); } public static void triple(int[] y) { y[2] = 3 * y[2]; } } |
|
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[2]); System.out.println(x[0] + x[1] + x[2]); } public static void triple(int m) { m = 3 * m; } } |
|
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; } } |
|
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 ____[10]; seq[6] = new A(); seq[3] = new B(); } } class B { } class E extends Object { } |
|
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 ____ |
|
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[0]); System.out.print(fun(x) + " "); System.out.print(x[index] + " "); } public static int fun(int[] x) { x[index] = x[index] - 1; return x[index]; } } |
|
What gets printed when you run it as follows:
|
21. Assume that
false ?
| |
|
|
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; } } |
|
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; } } |
|
24. Consider the following code fragment: | |
class One { public static void main(String[] args) { int t = Integer.parseInt(args[1]); if (t >= 2) System.out.print("IN"); if (t <= 3) System.out.print("DIA"); if (t >= 1) System.out.print("NA"); } } |
|
What gets printed when you compile and run it as follows:
Work carefully: there are no typos.java One 1 2 3 |
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(); |
|
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(); } } } |
|
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; } |
|
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; } |
|
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; } |
|
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; } |
|
31. Assume the following declaration:
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); |
|
32. Assume the following declaration:
What does the following code print?
| |
System.out.println(a[2][2]); |
|
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(); } } |
|
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(); } } |
|
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(); } } |
|
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; } } } |
|
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; } } |
|
38. Assuming that the following program fragment is syntactically correct...
...select the correct header for the functionint[] a = new int[10]; : : int n = f38(a, a[0]); : : f38 .
| |
|
|
39. Assuming that the following program fragment is syntactically correct...
...select the correct header for the functionint[] a = new int[10]; : : int[] n = f39(a, a[0]); : : f39 .
| |
|
|
40. Assuming that the following program fragment is syntactically correct...
...select the correct header for the functionint[][] a = new int[10]; : : int n = f40(0, a[0]); : : f40 .
| |
|
|
41. Assume the following declaration:
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); |
|
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); |
|
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); |
|
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); |
|
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); |
|
Tue May 1 17:00:00 EST 2001 (A201/A597/I210, Rawles Hall 100)