Java Garbage Collector. |
CSCI A201/A597 and I210 Final Examination Spring 2001 |
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.
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?
| |
| |
| 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?
|
|
|
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)? | |
|
|
10. Assume that
x and
y are
integer variables and the following nested if statement.
If 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?
| |
|
|
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 will 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? | |
|
|
| 16. What is printed if you compile and run the following program? | |
|
|
| 17. You compile and run this program. What is the output? | |
|
|
| 18. What letter should go in both blank spaces for the following program to compile? | |
|
|
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: | |
|
|
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? | |
|
|
| There are no typos, so please don't rush! | |
| 23. You compile and run this contrived program. What is the output? | |
|
|
| 24. Consider the following code fragment: | |
|
|
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? | |
|
|
| 26. What does the following program's output most closely resemble? | |
|
|
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?
| |
|
|
28. What does System.out.println(mix("one", "two")) print if mix is defined as below?
| |
|
|
29. What does System.out.println(mix("one", "two")) print if mix is defined as below?
| |
|
|
| There are no typos so, please, think carefully. | |
30. What does System.out.println(mix("one", "two")) print if mix is defined as below?
| |
|
|
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? | |
|
|
| 34. You compile and run this program. What is the output that you obtain? | |
|
|
| 35. You compile and run this program. What is the output that you obtain? | |
|
|
| 36. What is the result of attempting to compile and run the following code? | |
|
|
| 37. What is the result of attempting to compile and run the following code? | |
|
|
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?
| |
|
|
| 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)