![]() Good luck and do well. |
CSCI A201/A597/I210 Second Midterm Examination Spring Semester 2002 |
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); } } |
|
2. Consider the following condition.
Which of the following represents a simplification of it (obtained perhaps using DeMorgan).! (x % 3 != 0 || x % 2 != 0) | |
| |
3. Assume a is of type boolean and consider the following condition.
Which of the following represents a simplification of it.(a && !a) == false | |
| |
4. Consider the following program fragment.
What value does y have at the end?int x = 1, y = 1; if (x < 2) if (x > 1) y = 2; else y = 3; else y = 4; | |
| |
5. Consider the following program fragment.
What value does y have at the end?int x = 1, y = 1; if (x > 2) y = 2; y = y + 1; else y = y + 1; | |
| |
6. Consider the following program fragment.
What value does y have at the end?int x = 1, y = 1; if (x > 2) { y = 2; y = y + 1; } else y = y + 1; | |
| |
7. Consider the following program fragment.
What value does y have at the end?int x = 1, y = 1; if (x < 2) y = 2; else y = y + 1; y = y - 1; | |
| |
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("?"); | |
| |
9. How many question marks will be printed by the following code fragment?
for (int i = 7; i >= -3; i -= 2) System.out.print("?"); | |
| |
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("?"); } | |
| |
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 + ")"); | |
| |
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 + ")"); | |
| |
13. Consider the following fragment. What does it print?
int[] v = new int[10]; for (int i = 0; i < 10; i++) v[i] = i % 3; System.out.print(v[3] + v[5]); | |
| |
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); | |
| |
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); | |
| |
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(); | |
| |
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(); | |
| |
18. Consider the following program fragment.
What's needed for the output to resemble anpublic 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(); } } } Z ?
| |
| |
19. Consider the following command line invocation:
Assume your program (java One appear One.java ) contains this line inside
What does this line print?
| |
| |
20. Consider the following code fragment, what does it print?
String seven = "eleven"; System.out.println(seven.substring(0, 3)); | |
| |
21. Consider the following code fragment, what does it print?
String one = "BC"; System.out.println((char)('A' + one.length())); | |
| |
22. Consider the following code fragment, what does it print?
String one = "3"; System.out.println((char)('3' + one.length() + 3)); | |
| |
23. Does this source code compile?
class One { } class Two extends One { } class Three { public static void main(String[] args) { Two a = new One(); } } | |
| |
24. Does this source code compile?
class Three { } class Two extends Three { } class One { public static void main(String[] args) { Three a = new Two(); } } | |
| |
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(); } } | |
| |
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(); } } | |
| |
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(); } } | |
| |
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(); } } | |
| |
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[0][1] - 1][0]); } } | |
| |
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[1]); System.out.println(x[0] - x[1] + x[2]); } public static void fun(int value) { value = 1 - value; } } | |
| |
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[0] - x[1] + x[2]); } public static void fun(int[] value) { value[1] = 1 - value[1]; } } | |
| |
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; } } | |
| |
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; } } | |
| |
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); | |
| |
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; } | |
| |
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; } | |
| |
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; } | |
| |
38. Assuming that the following program fragment is syntactically correct...
int[] a = new int[10]; ... a[0] = fun(a[2], a); | |
... select the correct header for method fun .
| |
39. Assuming that the following program fragment is syntactically correct...
int[] a = new int[10]; ... a = fun(a[1], a); | |
... select the correct header for method fun .
| |
40. Assuming that the following program fragment is syntactically correct...
int[] a = new int[10]; ... a[0] = fun(a, a); | |
... select the correct header for method fun .
| |
41. You compile this program ...
... then run it as follows:class One { public static void main(String[] args) { System.out.println(args[args.length - args[0].length()]); } } java One abc defghi jk lmnopq | |
What does the program print (if anything)?
| |
42. You compile this program ...
... then run it as follows:class One { public static void main(String[] args) { System.out.println(args[3]); } } java One Two Three One Two Three | |
What does the program print (if anything)?
| |
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; } | |
|
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; } | |
|
A201/A597/I210 Second Midterm Exam Wed Apr 3 2002 in RH100