 You can do it! CSCI A201/A597/I210 Final Examination Summer II 2003 (Good luck and do well!)

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 75 minutes to complete the test. The test is open-book, open-notes (no computers).

Good luck and do well! (The answers are marked in yellow).

 1. Consider the following condition. `(x % 4 == 0 || (x % 100 == 0 && x % 400 == 0))` Which of the following represents a simplification of it? `((x % 100) == 0)`   `((x % 400) == 0)`   `((x % 100) != 0)`   `((x % 400) != 0)`   none of the above

The real simplification would be `x % 4 == 0`.

Here's the idea of a test you could make:

```frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
int[] count = new int;
for (int x = 0; x < 1000; x++) {
boolean cond = (x % 4 == 0 || (x % 100 == 0 && x % 400 == 0));
boolean a = ((x % 100) == 0),
b = ((x % 400) == 0),
c = ((x % 100) != 0),
d = ((x % 400) != 0),
ans = ((x % 4 == 0));
if (cond != a) count += 1;
if (cond != b) count += 1;
if (cond != c) count += 1;
if (cond != d) count += 1;
if (cond != ans) count += 1;
}
for (int i = 0; i < count.length; i++)
System.out.println(i + ": " + count[i] / 1000.0 + "%");
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
0: 0.24%
1: 0.247%
2: 0.76%
3: 0.753%
4: 0.0%
frilled.cs.indiana.edu%```
What's printed here is the degree of wrongness.

(There was a question yesterday in lab about testing the right answer).

 2. Consider the following condition. `! (a == ! false)` Which of the following represents a simplification of it. `a`   `! a`   `a && ! a`   `a || ! a`   none of the above

Go ahead and test it, like above.

But we've discussed this in class and lecture notes.

 3. Consider the following program fragment. `int x = 12, y = 0; if (x < 10) if (x > 5) y = 1; else y = 2;` What value does y have at the end? `0`   `1`   `2`   cannot be determined with the information given   this program fragment would not compile

For this and the next problem here's the proof:

```frilled.cs.indiana.edu%cat ThreeandFour.java
class ThreeandFour {
public static void main(String[] args) {
{
int x = 12, y = 0; if (x < 10) if (x > 5) y = 1; else y = 2;
System.out.println(y);
}
{
int x = 8, y = 0; if (x < 6) { if (x > 5) y = 1; } else y = 2;
System.out.println(y);
}
}
}
frilled.cs.indiana.edu%javac ThreeandFour.java
frilled.cs.indiana.edu%java ThreeandFour
0
2
frilled.cs.indiana.edu%```

 4. Consider the following program fragment. `int x = 8, y = 0; if (x < 6) { if (x > 5) y = 1; } else y = 2;` What value does y have at the end? `0`   `1`   `2`   cannot be determined with the information given   this program fragment would not compile

 5. Consider the following program fragment. `int x = 36, y = 8; if (x < 10) { if (x > 5) y = 1; else y = 2; }` What value does y have at the end? `0`   `1`   `2`   cannot be determined with the information given (not quite true, but see below)   this program fragment would not compile

If you left this question unanswered you will also get a point.

 6. Consider the following program fragment. `int x = 8, y = 0; if (x < 10) ; if (x > 5) y = 1; else y = 2; ` What value does y have at the end? `0`   `1`   `2`   cannot be determined with the information given   this program fragment would not compile

 7. How many question marks will be printed by the following code fragment? ```for (int i = -10; i <= 10; i = i + 3) ; System.out.print("?"); ``` `1`   `2`   `3`   `6`   other

Only one, because of the `for` loop having an empty body.

 8. How many question marks will be printed by the following code fragment? ```for (int i = 7; i >= 0; i--) System.out.print("?"); ``` `7`   `8`   `9`   `10`   other

This one (the `for`) was normal.

 9. Consider the following fragment. What does it print? ```int x = 3, y = 11; x = x + y; x = x - y; y = x - y; System.out.println("(" + x + ", " + y + ")"); ``` `(3, 11)`   `(11, 3)`   `(14, 14)`   `(7, 7)`   other

For this and the next one here's the test:

```frilled.cs.indiana.edu%cat NineandTen.java
class NineandTen {
public static void main(String[] args) {
{

{
int x = 3, y = 11;
x = x + y;
x = x - y;
y = x - y;
System.out.println("(" + x + ", " + y + ")");

}

{
int x = 11, y = 3;
y = x - y;
x = y + x;
y = y - x;
System.out.println("(" + x + ", " + y + ")");
}

}
}
}
frilled.cs.indiana.edu%javac NineandTen.java
frilled.cs.indiana.edu%java NineandTen
(3, -8)
(19, -11)
frilled.cs.indiana.edu%```

 10. Consider the following fragment. What does it print? ```int x = 11, y = 3; y = x - y; x = y + x; y = y - x; System.out.println("(" + x + ", " + y + ")"); ``` `(3, 11)`   `(11, 3)`   `(14, 14)`   `(7, 7)`   other

 11. Consider the following fragment. What does it print? ```int[] v = new int; for (int i = 0; i < 10; i++) v[i] = 2 * i; System.out.print(v + v)``` `6`   `8`   `10`   `31`   other

A semicolon (`;`) was missing for the program to compile.

Otherwise it would give `8`.

 12. Consider the following fragment. What does it print? ```int[] v = { 2, 4, 6, 8, 10, 12}, m = { 1, 3, 5, 7, 9, 11}; for (int i = 0, sum = 0; i < 10; i++) sum -= (m[i] - v[i]); System.out.print(sum)``` `6`   `36`   `-6`   `-84`   other

First off, the `sum` variable is local to the `for` for this to compile.

If that is taken care of, a `java.lang.ArrayIndexOutOfBoundsException` will be thrown (why?)

However, should that be taken care of the result would be `6`.

(You constantly subtract negative numbers.)

 13. Consider the following program. What does it print? ```int[] a = { 0, -1, 1, -2, 2, -3, 3}; int fun = a[a.length - 1]; for (int i = a.length - 1; i >= 0; i--) if (a[i] <= fun) fun = a[i]; System.out.println(fun); ``` `0`   `3`   `-3`   `-1`   other

 14. Consider the following program fragment. ```public 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 ( fun == 0 || fun == size - 1 || __________________ ) System.out.print(" *"); else System.out.print(" "); System.out.println(); } } }``` What's need for the output to resemble an `N` ? `fun == size / 2`   `nuf == size / 2`   `fun + nuf == size - 1`   `fun - nuf == size - 1`   other

(As seen from the inside of the computer...)

 15. Consider the following command line invocation: `java One Two Three Four Five` Assume your program contains this line inside `System.out.println(args[args.length - 1]);` What does this line print? `One`   `Two`   `Three`   `Five`   other

The name of the program is not negotiable here:

```frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
System.out.println(args[args.length - 1]);
}
}
frilled.cs.indiana.edu%java One Two Three Four Five
Five
frilled.cs.indiana.edu%```

 16. Consider the following code fragment, what does it print? ```String plum = "nectarine"; System.out.println(plum.substring("plum".length(), "peach".length()); ``` `t`   `a`   `ta`   `ar`   other

There was a close paren missing for this to compile.

If it did it would print `a`.

 17. Consider the following code fragment, what does it print? ```String plum = "nectarine"; System.out.println((char)('A' + plum.length())); ``` `J`   `O`   `K`   `E`   other

One only needs to know the alphabet here.

 18. Consider the following code fragment, what does it print? ```String plum = "nectarine"; System.out.println((char)('A' + "plum".length())); ``` `O`   `T`   `H`   `E`   `R`

 19. Does this source code compile? ```class Momerath { } class Tove extends Momerath { } class Kroger { public static void main(String[] args) { Tove a = new Momerath(); } }``` `yes`   `no`

 20. Does this source code compile? ```class Tove { } class Momerath extends Tove { } class Kroger { public static void main(String[] args) { Tove a = new Momerath(); } }``` `yes`   `no`

 21. Does this source code compile? ```class Wabe { } class Tove extends Wabe { } class Momerath extends Tove { } class Kroger { public static void main(String[] args) { Wabe a = new Momerath(); } }``` `yes`   `no`

 22. Does this source code compile? ```class Wabe { } class Tove extends Wabe { } class Momerath extends Tove { } class Kroger { public static void main(String[] args) { Tove a = new Wabe(); } }``` `yes`   `no`

 23. You compile and run this code, what is the output? ```class One { int red; One() { this(10); } One (int blue) { red += blue; } int report() { System.out.println(red); } public static void main(String[] args) { One x = new One(20); x = new One(30); x = new One(); x.report(); } } ``` `10`   `20`   `30`   `60`   other

No `return` when a return type is specified?

If it were to compile the answer would be `10`.

 24. 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 - 1]); } } ``` `2`   `4`   `6`   `8`   other

Easy question.

 25. 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); // focus here!! System.out.println(-x + x + x); } public static void fun(int value) { value = value - 1; } } ``` `0`   `2`   `4`   `6`   other

Pass by value (see Lab Two).

 26. 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, 0); // focus here!! System.out.println(-x + x + x); } public static void fun(int[] value, int index) { value[index] = value[index] - 1; } } ``` `0`   `2`   `4`   `6`   other

 27. What gets printed when you compile and run the following program? ```class One { public static void main(String[] args) { System.out.println(one(one(two(one(two(1, 2), 1), 2), two(1, 2)), 1)); } public static int one(int b, int a) { return a + b; } public static int two(int b, int a) { return a - b; } } ``` `0`   `2`   `4`   `6`   `other`

I hope that's what you got too.

 28. Assuming that the following program fragment is syntactically correct... ```int[][] a = new int; ... a = fun(a, a); ``` ... select the correct header for method `fun`.   `int[][] fun(int[] a, int b) `   `int fun(int[] a, int[][] b)`   `int[] fun(int a, int[] b)`   `int fun(int[][] a, int b)`   none of the above

 29. Assuming that the following program fragment is syntactically correct... ```int[][] a = new int; ... a = fun(a, a); ``` ... select the correct header for method `fun`.   `int[][] fun(int[] a, int b)`   `int fun(int[] a, int[][] b)`   `int[] fun(int a, int[] b)`   `int fun(int[][] a, int b)`   none of the above

 30. Assuming that the following program fragment is syntactically correct... ```int[][] a = new int; ... a = fun(a, a); ``` ... select the correct header for method `fun`.   `int[][] fun(int a, int[][] b)`   `int fun(int[] a, int[][] b)`   `int[] fun(int a, int[] b)`   `int fun(int[][] a, int b)`   none of the above

 31. What is the result of attempting to compile and run this code? ```class Alpha { public static void main(String[] args) { System.out.println("... will not run"); } public static void main() { System.out.println("... won't compile"); } } ``` The program compiles and runs and prints `... won't compile`   The program compiles but when you try to run it complains about `main`.   The program does not compile because `main` is not defined correctly.   The program compiles and runs and prints `... will not run`   none of the above

 32. What is the result of attempting to compile and run this code? ```abstract class Alpha { int value; Alpha (int value) { this.value = value; System.out.println(value); } public static void main(String[] args) { Beta f = new Beta(2003); } } class Beta extends Alpha { Beta (int value) { super(value); } }``` The code will compile but nothing will appear in the standard output.   Code won't compile because there's no default no-args constructor for Alpha.   The code will not compile because Alpha is abstract.   The code will compile and run.   none of the above

 33. What is the result of attempting to compile and run this code? ```class Alpha { public static void main(String[] args) { Alpha f = new Beta(); Alpha g = new Gamma(); System.out.println(f.test(g.test(3))); } int test(int i) { return (i + 2); } } class Beta extends Alpha { } class Gamma extends Beta { int test(int i) { return (i + 1); } } ``` The code compiles and runs succesfully, and outputs 3.   The code compiles and runs succesfully, and outputs 4.   The code compiles and runs succesfully, and outputs 5.   The code does not compile.   none of the above

 34. What is the result of attempting to compile and run this code? ```class Alpha { public void main(String[] args) { Beta f = new Beta(3); } Alpha (int i) { } } class Beta extends Alpha { Beta(int i) { } }``` The code does not compile because Beta's constructor is empty.   The code does not compile because Alpha doesn't define a no-args constructor.   The code compiles and runs succesfully.   The code does not compile because Beta does not define a no-args constructor.   none of the above

 35. What is the result of attempting to compile and run this code? ```class Alpha { public static void main(String[] args) { Beta f = new Beta(); } } class Beta extends Alpha { }``` The code does not compile because Beta does not define a no-args constructor.   The code does not compile because Beta does not define any constructors whatsoever.   The code compiles and runs succesfully, with no output.   The code does not compile because Alpha doesn't define a no-args constructor.   none of the above

 36. What is the result of attempting to compile and run this code? ```abstract class Alpha { abstract void complain(String s); } 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."); } }``` The program will compile and run.   The code will not compile because Beta has no default no-arg constructor.   The code does not compile because Beta is abstract.   The code does not compile because Tester does not inherit from Alpha.   none of the above

`A201/A597/I210 TTFN Exam Thu Aug 14 2003 in LH102`