 Good luck and do well! CSCI A201/A597 Final Examination Summer II 2001

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 two hours (120 minutes) to complete the test. The test is open-book.

• The correct answers are marked in blue.

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))); } } ``` `true`   `false`   `other`

One can compile and run this code to obtain the correct answer, which is: ` true `

 2. Consider the following condition. `!(x % 4 != 0 || !(x % 100 == 0 && x % 400 == 0))` Which of the following represents a simplification of it (obtained perhaps using DeMorgan). `((x % 100) == 0)` `((x % 400) == 0)`   `((x % 100) != 0)`   `((x % 400) != 0)`   `none of the above`

This question appears in the warmups for chapter 5.

 3. 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`

This is a problem we have discussed in class.

 4. 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`
Here's how you get the right answer to this and the next two questions.

```class One {
public static void main(String[] args) {

{ int x = 8, y = 0; if (x < 10) if (x > 5) y = 1; else y = 2;
System.out.println(y);
}
{ int x = 8, y = 0; if (x < 10) { if (x > 5) y = 1; } else y = 2;
System.out.println(y);
}
{ int x = 8, y = 0; if (x < 10) { if (x > 5) y = 1; else y = 2; }
System.out.println(y);
}

}
} ```

 5. 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`

 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. 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`
```frilled.cs.indiana.edu%
frilled.cs.indiana.edu%javac One.java
One.java:3: 'else' without 'if'.
int x = 8, y = 0; { if (x < 10) if (x > 5) y = 1; } else y = 2;
^
1 error
frilled.cs.indiana.edu%```

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

```frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
for (int i = -10; i <= 10; i = i + 3)
System.out.print("?");
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
???????frilled.cs.indiana.edu%```

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

```frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
for (int i =  7; i >=  0; i--)
System.out.print("?");
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
????????frilled.cs.indiana.edu%```

Arguably the index of the loop had to be declared explicitly for this to remove all doubts, so we have two correct answers here, let's say.

 10. How many question marks will be printed by the following code fragment? ```for (i = 7; i >= 0; i--) ; System.out.print("?"); ``` `7`   `8`   `9`   `10` `other`
No matter how you look at this question the answer is `other` (the semicolon was intentional).
 11. Consider the following fragment. What does it print? ```int x = 3, y = 11; x = x + y; y = x - y; x = x - y; System.out.println("(" + x + ", " + y + ")"); ``` `(3, 11)` `(11, 3)`   `(14, 14)`   `(7, 7)`   `other`

This is known as swapping two numbers without a third variable.

Correct answers for this and the next problem are obtained as follows:

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

{
int x = 3, y = 11;
x = x + y;
y = x - y;
x = 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 One.java
frilled.cs.indiana.edu%java One
(11, 3)
(3, 11)
frilled.cs.indiana.edu%```

 12. 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`

 13. Consider the following fragment. What does it print? ```int[] v = new int; for (int i = 0; i < 10; i++) v[i] = i * i; System.out.print(v + v)``` `6`   `8`   `10`   `31` `other`
No matter how you look at it the answer is `other`.
 14. Consider the following fragment. What does it print? ```int[] v = { 1, 2, 3, 4, 5, 6}, m = { 6, 5, 4, 3, 2, 1}; for (int i = 0, sum = 0; i < 10; i++) sum += (v[i] - m[i]); System.out.print(sum)``` `0`   `21`   `42`   `84` `other`
Again, no matter how you look at it the answer is `other`. The first answer will also be considered correct, but please try to make this code run and see how many reasons you collect in support of the last of the options.
 15. 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); ``` `-3`   `0` `3`   `7`   `other`
```frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {

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);

}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
3
frilled.cs.indiana.edu%```

 16. Consider the following program fragment. What would it print? ```int[] a = { 4, 3, 5, 2, 6, 1, 7}; boolean done = false; while (! done) { done = true; for (int i = 0; i < a.length - 1; i++) if (a[i] > a[i + 1]) { // focus here! 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(); ``` `1234567`   `7654321`   `3425167`   `4536217`   `other`

 17. Consider the following program fragment. What would it print? ```int[] a = { 4, 3, 5, 2, 6, 1, 7}; boolean done = false; while (! done) { done = true; for (int i = 0; i < a.length - 1; i++) if (a[i] < a[i + 1]) { // focus here! 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(); ``` `1234567` `7654321`   `3425167`   `4536217`   `other`

 18. 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 `H`? `fun == size / 2` `nuf == size / 2`   `fun + nuf == size - 1`   `fun - nuf == size - 1`   `other`

This is a problem like in lab six.

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

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

 20. Consider the following code fragment, what does it print? ```String plum = "nectarine"; System.out.println(plum.substring(0, "plum".length()); ``` `nec` `nect`   `nectarin`   `nectarine` `other`

Yes, I forgot to type a right parenthesis.

 21. Consider the following code fragment, what does it print? ```String plum = "nectarine"; System.out.println((char)('Q' - plum.length())); ``` `H`   `O`   `M`   `E`   `other`
Here's how you get the key for this and the next problem.

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

{
String plum = "nectarine";
System.out.println((char)('Q' - plum.length()));
}

{
String plum = "nectarine";
System.out.println((char)('Q' - "plum".length()));
}

}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
H
M
frilled.cs.indiana.edu%```

 22. Consider the following code fragment, what does it print? ```String plum = "nectarine"; System.out.println((char)('Q' - "plum".length())); ``` `H`   `O` `M`   `E`   `other`

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

Not all vegetables are tomatoes.

```frilled.cs.indiana.edu%javac One.java
One.java:5: Incompatible type for declaration.
Explicit cast needed to convert Vegetable to Tomato.

Tomato a = new Vegetable();
^
1 error
frilled.cs.indiana.edu%```

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

Yes, all unicorns are horses (as defined in class).

 25. 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(); } }``` `yes`   `no`

Not that it really matters, but to remove all doubts:

```frilled.cs.indiana.edu%webster kohlrabi
kohl-ra-bi \ko^-l-'rab-e^-, -'ra^:b-\ n,  pl -bies
[G, fr. It cavolo rapa, fr. cavolo cabbage (fr. L caulis stalk)
+ rapa turnip, fr. L -- more at HOLE, RAPE]
(1807)
:any of a race of cabbages having a greatly enlarged, fleshy, turnip-shaped
edible stem

frilled.cs.indiana.edu%```

This exercise courtesy of . Maybe.

 26. 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(); } }``` `yes` `no`

Many things are not kohlrabi, and this Java program knows that.

```frilled.cs.indiana.edu%javac One.java
One.java:6: Incompatible type for declaration.
Explicit cast needed to convert Vegetable to Kohlrabi.

Kohlrabi a = new Vegetable();
^
1 error
  - Done                 emacs One.java
frilled.cs.indiana.edu%```

 27. 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`

For both this and the next exercise the `report` method should have been declared as `void` to compile. So `other` will be considered correct. Assume the compilation error is fixed then the two questions both have a second correct answer, as indicated.

 28. You compile and run this code, what is the output? ```class One { static 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`

 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}, {4, 5, 6}, {7, 8, 9}, {6, 2, 7} }; System.out.println(a[a - 1]); } } ``` `2` `4`   `6`   `8`   `other`

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

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

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

Here's to running all our programs at least once:

```frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
System.out.println(kanga(kanga(kanga(kanga(ru(1, 2),
1),
2),
ru(1, 2)),
1));
}
public static int kanga(int a, int b) {
return a + b;
}
public static int ru(int a, int b) {
return a - b;
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
2
frilled.cs.indiana.edu%```

 33. 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(fun(6))))); } static int fun(int value) { return value - 1; } } ``` `0` `2`   `4`   `6`   `other`

'nuf said.

 34. What does the next fragment print if embedded in a complete program? ```String tsew = "stue".charAt(0) + "tsue".charAt(0) + "ew"; System.out.println(tsew); ``` `stew`   `stue`   `tsew` `231ew`   `other`

Just as discussed in class the plus between characters happens first, which makes them participate with their codes, thus producing an integer. The next plus is between the resulting integer and a `String` and that produces a `String`. Keep this in mind as you enter the next three questions.

 35. What does `mix("bbb", "aaa")` 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 < olen && i < tlen; i++) { ans = one.charAt(i) + two.charAt(i) + answer; } return ans; } ``` `bbbaaa`   `aaabbb`   `ababab`   `bababa` `other`
Note: please recall what you did in the previous exercise.

This program does not compile. And neither do the next two. But does it matter?

Let's see... Suppose I fix it to compile:

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

System.out.println(mix("bbb", "aaa"));

}

public static String mix(String one, String two) {
int lengthOne = one.length(), lengthTwo = two.length();
for (int i = 0; i < lengthOne && i < lengthTwo; i++) {
}
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
195195195
frilled.cs.indiana.edu%```
As you can see, it doesn't matter.

For the same reasoning used in the exercise before this one.

So now we know the answers for the next two questions.

 36. What does `mix("bd", "pq")` 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 < olen && i < tlen; i++) { ans = one.charAt(i) + two.charAt(i) + answer + two.charAt(i); } return ans; } ``` `dqbppq`   `qdpqqp`   `bdqppq`   `pbpqqp` `other`

No characters can come out of this code on the left of the original value of `answer`, as the characters codes will be summed up into integers. Here's a version of this program that compiles:

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

System.out.println(
mix("bd", "pq")
);

}

public static String mix(String one, String two) {
int lengthOne = one.length(), lengthTwo = two.length();
for (int i = 0; i < lengthOne && i < lengthTwo; i++) {
}
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
213210pq
frilled.cs.indiana.edu%```

 37. What does `mix("bd", "pq")` 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 < olen && i < tlen; i++) { ans = one.charAt(i) + two.charAt(i) + answer + two.charAt(i); } return ans; } ``` `dqbppq`   `qdpqqp`   `bdqppq`   `pbpqqp` `other`

I did get distracted when I was putting these together on Tuesday and I didn't get the time to review them. Hence the typos. But the outcome of the exercises would have still been the same, and I told you about it at the beginning of the exam today.

 38. 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[] int)`   ` int fun(int[], int[][])`   `int[] fun(int, int[])`   `int fun(int[][], int)`   ` none of the above`

For this and the next three questions I left out some commas and the names of the formal params. So for this reason I will count `other` as a good answer. But I will also indicate the answers that are closest (syntactically speaking) to the right answers.

 39. 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[] int)`   `int fun(int[], int[][])` `int[] fun(int, int[])`   `int fun(int[][], int)` `none of the above`

 40. 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[] int)`   `int fun(int[], int[][])`   `int[] fun(int, int[])`   `int fun(int[][], int)` `none of the above`

 41. You compile this program ... ```class One { public static void main(String[] args) { System.out.println(args[args.length - args.length()]); } } ``` ... then run it as follows: `java One this is my song for the asking` What does the program print (if anything)?   `this` `song`   `ing`   `the program compiles but does not run`   `other`

 42. You compile this program ... ```class One { public static void main(String[] args) { System.out.println(args); } } ``` ... then run it as follows: `java One ask me and I'll play so sweetly I'll make you smile ` What does the program print (if anything)?   `ask`   `me`   `and`   `the program compiles but does not run`   ` other`

 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(); a.nuf(3); a.fun(); a.nuf(5); System.out.println(Four.m + a.n); } void nuf(int p) { this.n = this.n + p; Four.m = Four.m - p; } void fun() { int temp = this.n; this.n = Four.m; Four.m = temp; } int n; static int m; } ``` `0`   `1`   `2`   `3`   `other`

 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(); b.nuf(3); b.fun(); a.nuf(5); System.out.println(Four.m + a.n); } void nuf(int p) { this.n = this.n + p; Four.m = Four.m - p; } void fun() { int temp = this.n; this.n = Four.m; Four.m = temp; } int n; static int m; } ``` `0`   `1` `2`   `3` `other`

I left behind the name of the class from the previous exercise. So there are three cases: you either have that class around, in which case this compiles and prints 2. Or you don't, in which case this does not compile. Or you make the change (from `Four` to `Five`) and then this prints 4. So the third, the fifth, amd the fifth again were the right answers, and all the others impossible.

`A201/A597 TTFN Exam Fri Aug 10 2001 in SW119`