![]() PLEASE, DON'T! | CSCI A201/A597/I210 Final Exam Spring Semester 2003 |
Exam is closed-book, closed-notes, and individual work. | |
The test is closed-book and closed-notes.
Good luck and do well!
1. Consider the following program:
abstract class Alpha { abstract void complain(); } class Beta extends Alpha { void complain(String s) { System.out.println(s); } } class Tester { public static void main(String[] args) { Beta f = new Beta(); f.complain("There's a tomato in every automaton."); } } |
A. The program will compile and run.
B. The code will not compile because Beta has no default no-arg constructor.
C. The code does not compile because Beta is abstract.
D. The code does not compile because Tester does not inherit from Alpha.
E. None of the above.
2. Which of the following statements about arrays is true?
A. All of the elements in an array must be of the same type. |
For the next two questions please consider the following method:
Please answer the following two questions:int fun(int m) { int n = 0; if (m == 0) { n = 1; } else { n = m + fun(m - 2); } return n; } 3. What does fun(4) evaluate to? A. 4 |
4. What does fun(5) evaluate to?
A. 4 |
5. After the following statement is executed,
which of these numbers could NOT possibly be contained in x?int x = (int)(Math.random() * (12 - 5) + 6); A. 6 |
6. Assuming that the following program fragment is syntactically correct...
... select the correct header for method fun.int[] a = new int[10]; ... a[0] = fun(a, a); A. |
7. What does mix("abc", "def") return if mix is defined as below?
Please choose one of the options listed 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; } A. |
8. What gets printed when you compile and run the following program?
Choose one of the following options: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; } } A. 1 |
9. If you compile and run this code, what is the output?
Choose one of the following: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; } } A. |
10. If you compile and run this code, what is the output?
Choose one of the following: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]; } } A. |
11. Consider the following fragment. What does it print?
Choose among the following:int x = 2, y = 1; y = x + y; x = y - x; y = y - x; System.out.println("(" + x + ", " + y + ")"); A. |
12. You compile this program ...
... then run it as follows:class One { public static void main(String[] args) { System.out.println(args[args.length - args[3].length()]); } } What does the program print (if anything)?java One this is my song for the asking A. |
13. If you compile and run this code, what is the output?
class One { public static void main(String[] args) { int[][] a = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {6, 2, 7} }; System.out.println(a[a[0][1] - 1][0]); } } | |
A. | |
14. 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]); // focus here!! System.out.println(x[0] + x[1] + x[2]); } public static void fun(int value) { value = value - 3 * value;; } } | |
A. | |
15. Does this source code compile?
class Vegetable { } class Tomato extends Vegetable { } class Kroger { public static void main(String[] args) { Vegetable a = new Tomato(); } } | |
A. | |
16. Does this source code compile?
class Vegetable { } class Cabbage extends Vegetable { } class Kohlrabi extends Cabbage { } class Kroger { public static void main(String[] args) { Cabbage a = new Kohlrabi(); } } | |
A. | |
17. Does this source code compile?
class Vegetable { } class Cabbage extends Vegetable { } class Kohlrabi extends Cabbage { } class Kroger { public static void main(String[] args) { Kohlrabi a = new Vegetable(); } } | |
A. | |
18. 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(); } } } | |
Choose from the following:
A. | |
Choose from one of the options below:class Alpha { public static void main(String[] args) { Beta f = new Beta(); } Alpha(int i) { } } class Beta extends Alpha { }
A. The code does not compile becauseBeta
does not define a no-args constructor.
B. The code does not compile becauseBeta
does not define any constructors whatsoever.
C. The code compiles and runs succesfully, with no output.
D. The code does not compile becauseAlpha
doesn't define a no-args constructor.
E. None of the above.
Choose one of the options below:class Alpha { public static void main(String[] args) { Beta f = new Beta(3); } Alpha (int i) { } } class Beta extends Alpha { Beta(int i) { } }
A. The code does not compile becauseBeta
's constructor is empty.
B. The code does not compile becauseAlpha
doesn't define a no-args constructor.
C. The code compiles and runs succesfully.
D. The code does not compile because Beta does not define a no-args constructor.
E. None of the above.
Please choose one of the options listed below:class Alpha { public static void main(String[] args) { Beta f = new Beta(3); } Alpha() { System.out.println(0); } Alpha(int i) { System.out.println(i); } } class Beta extends Alpha { Beta() { } Beta(int i) { } }
A.The code does not compile becauseBeta
's constructors are empty.
B. The code compiles and runs succesfully, but there is no output.
C. The code compiles and runs succesfully, and outputs0
.
D. The code compiles and runs succesfully, and outputs3
.
E. None of the above.
Please choose one of the options listed below:class Alpha { public static void main(String[] args) { Beta f = new Beta(3); } Alpha() { System.out.println(0); } Alpha(int i) { System.out.println(i); } } class Beta extends Alpha { Beta() { super(6); } Beta(int i) { this(); } }
A. The code compiles and runs succesfully, and outputs0
.
B. The code compiles and runs succesfully, and outputs3
.
C. The code compiles and runs succesfully, and outputs6
.
D. The code does not compile because at least one ofBeta
's constructors is not defined legally.
E. None of the above.
23. What is the result of attempting to compile and run this code?
Please choose one of the options listed below:class Alpha { public static void main(String[] args) { Beta f = new Beta(3); } Alpha() { System.out.println(0); } Alpha(int i) { System.out.println(i); } } class Beta extends Alpha { Beta() { super(6); } Beta(int i) { super(3); this(); } } A. The code compiles and runs succesfully, and outputs |
24. How many instance methods do you see defined below? | |||
class One { int balance; int f() { return 1; } static int g() { return 2; } One(int x) { this.balance = x; } public static void main(String[] args) { } } |
A. 0 |
25. How many static methods do you see defined below? | |
class One { int balance; int f() { return 1; } static int g() { return 2; } One(int x) { this.balance = x; } public static void main(String[] args) { } } | |
Choose from the options listed below:
A. 0 | |
26. How many instance variables do you see defined below? | |
class One { int n; int m; int fun(int p) { int q = p + 1; return q; } } | A. 0 |
27. How many instance variables do you see defined below? | |
class One { static int n; int m; int fun(int p) { int q = p + 1; return q; } } | A. 0 |
28. How many instance variables do you see defined below? | |
class One { static int n; int fun(int p) { int m; int q = p + 1; return q; } } |
B. 1 C. 2 D. 3 E. none of the above |
29. How many local variables do you see defined below? | |
class One { static int n; int fun(int p) { int m; int q = p + 1; return q; } } |
A. 0 |
|
31. 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; } |
Please choose from the options listed below:
A. |
32. What is the output produced by the following code when embedded in a complete program? | |
if (!false && false || true) { System.out.print(false); } else { System.out.print(true); } | A. |
33. What is the output produced by the following code when embedded in a complete program? | |
if (false && (!false || true)) { System.out.print(false); } else { System.out.print(true); } | A. |
34. What is the output produced by the following code when embedded in a complete program? | |
if (false && (false || !true)) { System.out.print(false); } else { System.out.print(true); } | A. |
35. What is the output produced by the following code when embedded in a complete program? | |
if (false && !(false || true)) { System.out.print(false); } else { System.out.print(true); } | A. |
36. What is the output produced by the following code when embedded in a complete program? | |
if (!false && (!false || !true)) { System.out.print(false); } else { System.out.print(true); } | A. |
37. What is the output produced by the following code when embedded in a complete program? | |
if (!false && !false || !true) { System.out.print(false); } else { System.out.print(true); } |
A. true B. false
|
38. What is the output produced by the following code when embedded in a complete program? | |
if (!(false && false || true)) { System.out.print(false); } else { System.out.print(true); } | A. |
39. What is the output produced by the following code when embedded in a complete program? | |
if (!true) { System.out.print(false); } else { System.out.print(true); } | A. |
40. What is the output produced by the following code when embedded in a complete program? | |
if (false) { System.out.print(false); } else { System.out.print(true); } | A. |
Assume that i and sum have been declared already.i = 0; sum = 0; while (i < 100) { sum += i; i += 2; }
A.for (i = sum = 0; i < 100; sum += i, i += 2) ;
B.for (i = 0, sum = 0; i < 100; i += 2) sum += i;
C.for (i = sum = 0; (i += 2) < 100 ; ) sum += i;
D.for (i = sum = 0; i < 100; sum += i) i += 2;
E.for (i = sum = 0; i < 100; i += 2) sum += i;
42. What does the following program's output look like?
Please choose from the options below:public class Eleven { public static void main(String[] args) { int i = 0; int j = 0; for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { if ((i == j) && (i + j == 10)) { System.out.print(" " + j); } else { System.out.print(" "); } } System.out.println(); } } } A. a long line of numbers |
Warning: Don't overlook the logical negation (bang!) at the front.!(((x > 0) || (x == 0)) && (x < 10))
A.true
B.(x != 0) || (x >= 10)
C.false
D.(x < 0) || (x >= 10)
E.(x < 0) && (x >= 10)
44. Assume that x is an integer variable. Simplify the following boolean expression.
Warning: Don't overlook the logical negation (bang!) at the front.!(((x > 0) || (x == 0)) || (x < 10)) A. |
45. 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); } } | A. |
46. Consider the following condition.
Which of the following represents a simplification of it (obtained perhaps using DeMorgan).! (x % 3 != 0 || x % 2 != 0) | |
A. | |
47. Assume a is of type boolean and consider the following condition.
Which of the following represents a simplification of it.(! (a && !a)) == false | |
A. | |
48. 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; | |
A. | |
49. 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; | |
A. | |
50. 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; | |
A. | |
51. 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; | |
A. | |
52. How many question marks will be printed by the following code fragment?
for (int i = -10; i <= 10; i = i + 4) System.out.print("?"); | |
A. |
A201/A597/I210 Final Exam Tue May 6 5-7pm 2003 in RH100