CSCI A201/A597 and I210 Midterm Exam Two Spring 2001

Directions:
• Each of the questions below is followed by a number of suggested answers. 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.

• Work as rapidly as you can without being careless. This includes checking frequently to make sure that you are marking your answers in the appropriate rows on your answer sheet.

• All correct answers carry the same weight, they're each worth each exactly one point. Leaving a question unanswered will count as a zero. All incorrect answers count as -0.25 (with a minus). You can only mark 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.

Good luck and do well!

 The right answers are marked in underlined `blue`. Where I need to make a comment (about typos and such) I will be writing it in italic blue.

 1. What is the output produced by the following three lines (when embedded in a complete program)? ```int x, y; x = 7; y = 5; System.out.println("1" + x + y); ``` `13`   `17`   `75`   `175`   `other` Note: To save space I wrote `other` where I would have put "None of the above."

 2. What is the output produced by the following three lines (when embedded in a complete program)? ```int x, y; x = 9; y = 10; System.out.println("1" + x * y); ``` `0`   `19`   `91`   `1910`   `other` The code would print `190` so the answer is `other`.

 3. What is the output produced by the following three lines (when embedded in a complete program)? ```int x, y; x = 9; y = 10; System.out.println("1" * x + y); ``` `0`   `19`   `91`   `1910`   `other` The code does not compile because of the asterisk (star) not being a valid operators for `String`s, so the answer is `other`.

 `4`. What is the output produced by the following two lines (when embedded in a complete program)? ```int a; a = 'Q' - 'M'; System.out.println(a); ``` `E`   `B`   `C`   `3`   `0` The answer would be a value of `4` which is not listed at all where it should be. So regardless of what you marked (and whether you marked anything or not) you will get two points. (I marked in blue as being the right answer the closest `4` that I could find on this page... So close it was, yet so far).

 5. What is the output produced by the following program fragment (when embedded in a complete program)? ```if ('Z' > 'B') System.out.print("1" + 3 / 2); System.out.print(2);``` `12`   `11`   `"1"1.5`   `2.5`   `other` The output is a `112` so the right answer is `other`.

 6. What is the type of this expression: `"1" + 'a'` `int`   `char`   `boolean`   `double`   `other` The type of the expression is `String` so the right answer was `other`.

 7. What is the output produced by the following three lines (when embedded in a complete program)? ```int a, b; a = 3; b = 2; System.out.println(b * (a / b) + a % b);``` `1`   `2`   `3`   `4`   `other` Both the quotient and the remainder have a value of 1.

 8. What's the output produced by the following lines (when embedded in a complete program)? ```String quote = "There's a tomato" + "in every automaton!"; System.out.println(quote.substring(29, 35)); ``` `utomat`   `tamotu`   `otomat`   `tomato`   `other` The program prints `maton!` so the right answer was `other`.

 9. What's the output produced by the following lines (when embedded in a complete program)? ```String a = "sun"; String b = "dried"; System.out.println( "tomato".substring(b.length() - a.length(), a.length()));``` `a`   `m`   `o`   `t`   `other` A paren was missing so we will count `other` as a valid answer. My intention though was for the code to be correct, so there is a second correct answer, which is what the code would print if the paren had been in its place from the beginning.

 10. Given `String a = "tomato";` which of the following expressions is NOT of type `String`? `a` `"a"` `'a'` `a + a.length` `a.substring(0, 1) + 'a'` `1`   `2`   `3`   `4`   `5` The middle one is a character (type is `char` ) while in each of the other expressions there's a `String` somewhere that sets the tone.

 11. What's the output produced by the following lines (when embedded in a complete program)? ```String a = "a"; a.toUpperCase(); System.out.println(a + "a" + a.toUpperCase());``` `aaa`   `aaA`   `AaA`   `AAA`   `other` Remember that `String`s are immutable...

 12. Which of the following expressions is NOT logically equivalent to `(a >= b)`? `(b <= a)` `(b < a) || (b == a)` `(b == a) && (b < a)` `(b - a) <= 0 ` `(b <= a) && (a >= b)` `1`   `2`   `3`   `4`   `5` The third boolean is always `false` the others are all equivalent to the original formula.

 13. What's the output produced by the following lines (when embedded in a complete program)? ```int a = 100; if (a >= 100) System.out.print("a "); else if (a >= 10) System.out.print("b "); // if (t <= 1000) if (a <= 1000) System.out.print("c ");``` `a `   `a b `   `a c `   `a b c `   `other` I don't know how the `t` found its way there. It should have been an `a`. As such, the code does not compile. Both the 3rd and the 5th answers are considered correct.

 14. Consider the following expression in which `m` is an `int`. `(m < 10) && (m > 12) ` Which of the following is a valid Java simplification of the expression above? `(m == 11)`   `true`   `(m != 11)`   `false`   `(10 > m > 12) ` `1`   `2`   `3`   `4`   `5` For any value of `m` you can think of, the condition is `false`.

 15. Assume `a` and `b` are integer variables. If `a` has a value of `2` after executing the following program fragment what was the value of `b` at the beginning of the fragment code? ```if (b > 2 && b < 6) a = 1; else a = 3; if (b % 2 == 0) a = a + 1; ``` `2`   `3`   `4`   `5`   `other` Careful investigation reveals that a can either be 2 or 4 at the end of the code. There's no other possibility. a ends up being 2 that means b was even, integer and between 3 and 5.

 16. Which of the following expressions does NOT evaluate to `3`? `'d' - 'a'`   `'3'`   `(3 * 3 + 2) / 3`   `1 + 2`   `"abc".length()` `1`   `2`   `3`   `4`   `5` `'3'` is of type `char`acter, while all of the other expressions evaluate to the actual number `3`.

 17. How can you access the word `One` from the following command line invocation? `java One one two three four One` `args[0]`   `args[1]`   `args[4]`   `args[5]`   `args[6]` `1`   `2`   `3`   `4`   `5` `One` is also the name of the `.class` file.

 18. How can you access the word `java` from the following command line invocation? ```java one two three one two three java 0 1 2 3 4 5``` `args[0]`   `args[1]`   `args[2]`   `args[5]`   `args[7]` `1`   `2`   `3`   `4`   `5` Same as above. Starting all class names with a capital letter is not mandatory, just a (good) convention. I think you should follow that convention, but once in a while one might not, as in the case of this exercise, for the technicality of it.

 19. What will appear in the standard output when you compile and run the following class? ```public class A { public static void main(String[] args) { int x = 0; System.out.print(fun(x) + " "); System.out.print(x + " "); } public static int fun(int x) { x = x + 1; return x; } }``` `1 0 `   `0 0 `   `0 1`   `1 1`   `other` fun is called with an argument of 0, which is made a 1, and returned, to be printed. The change is not seen in the main method. So when we finally print the x that was passed to fun it comes out as what it was when we did pass it to fun, that is, a 0 (zero).

 20. What will be printed by the following program? ```public class A { public static void main(String[] args) { int[] x = {5, 4, 3, 2, 1}; System.out.print(fun(x) + " "); System.out.print(x[2] + " "); } public static int fun(int[] x) { x[2] = x[2] - 1; return x[2]; } }``` `2 2`   `2 3`   `3 2`   `3 3`   `other` Passing the array to a method means that we pass a pointer to it to the method, so the array is shared by the two methods. Thus changes to `x[2]` (in fun) will be seen in main.

 21. Consider the following expression in which `m` is an `int`. `(m > 10) || (m < 12) ` Which of the following is a valid Java simplification of the expression above? `(m == 11)`   `true`   `(m != 11)`   `false`   `(12 > m > 10) ` `1`   `2`   `3`   `4`   `5` Any integer is either bigger than 10 or smaller than 12. So the condition is always true.

 22. What gets printed when you compile and run the following program? ```public class A { public static void main(String[] args) { System.out.println(fun(1, fun(fun(2, fun(3, 4)), 5))); } public static int fun(int a, int b) { return a - b; } }``` `1`   `2`   `3`   `4`   `other` This boils down to 1 - ((2 - (3 - 4)) - 5)

 23. Given the following declaration what does `a[a[a[a[a[4]]]]]` evaluate to? ```int[] a = { 2, 3, 1, 5, 0, 4 }; 0 1 2 3 4 5``` `1`   `2`   `3`   `4`   `other` The derivation goes like this ```a[a[a[a[a[4]]]]] a[a[a[a[ 0]]]] a[a[a[ 2]]] a[a[ 1]] a[ 3] 5 ``` so the answer is `5` (`other`).

 24. Given the following declaration what does `a[a[1] - 1]` evaluate to? ```int[] a = { 1, 2, 3 }; 0 1 2``` `1`   `2`   `3`   `4`   `other` We just calculate `a[1]` twice.

 25. Exactly how many question marks will appear on the screen when the following program fragment is executed? ```for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) System.out.print("?"); System.out.println(); ``` `0`   `1`   `2`   `4`   `other` Two times two equals four.

 26. What does the following code print when compiled and run? ```public class A { public static void main(String[] args) { int yo = 12; System.out.println(yo(yo)); } public static boolean yo (int yo) { if (yo == 12) return true; return false; } } ``` `12`   `1212`   `true`   `false`   `other` Variable names and method names belong to different name spaces so all works as expected.

The next four questions have all been affected by the same typo (corrected below). Also, the method `mix` doesn't really print anything. It returns a `String` which one could print, and that was my original intent, as illustrated below:

```class A {
public static void main(String[] args) {
System.out.println(mix("abc", "ABC"));
}
public static String mix(String one, String two) {
// [...]
}
} ```
Under the circumstances both the intented correct answer as well as `other` will be accepted.

 27. What does `mix("abc", "ABC")` print if `mix` is defined as below? ```public static String mix(String one, String two) { String ans = ""; // int slen = s.length; int slen = one.length(); // int tlen = t.length; int tlen = two.length(); for (int i = 0; i < slen && i < tlen; i++) { ans = ans + one.charAt(i) + two.charAt(i); } return ans; } ``` `cCbBaA`   `CcBbAa`   `aAbBcC`   `AaBbCc`   `other` Letters from the first preced in the output letters from the second.

 28. What does `mix("abc", "ABC")` print if `mix` is defined as below? ```public static String mix(String one, String two) { String ans = ""; // int slen = s.length; int slen = one.length(); // int tlen = t.length; int tlen = two.length(); for (int i = 0; i < slen && i < tlen; i++) { ans = one.charAt(i) + two.charAt(i) + ans; } return ans; } ``` `cCbBaA`   `CcBbAa`   `aAbBcC`   `AaBbCc`   `other` We changed the order in which we place the pairs in the resulting `String`, so the first answer would be OK, and is accepted. But do run it and tell me why it does not produce that output? What happens? Does this relate to question 6 and question 2 above? (Especially with question 2).

 29. What does `mix("abc", "ABC")` print if `mix` is defined as below? ```public static String mix(String one, String two) { String ans = ""; // int slen = s.length; int slen = one.length(); // int tlen = t.length; int tlen = two.length(); for (int i = 0; i < slen && i < tlen; i++) { ans = two.charAt(i) + one.charAt(i) + ans; } return ans; } ``` `cCbBaA`   `CcBbAa`   `aAbBcC`   `AaBbCc`   `other` Same as above, the program would print: 166164162. As far as the alternate key goes, second option was selected, since that's how the program works (but that's not what the program prints). I am sure you know by now why that happens.

 30. What does `mix("abc", "ABC")` print if `mix` is defined as below? ```public static String mix(String one, String two) { String ans = ""; // int slen = s.length; int slen = one.length(); // int tlen = t.length; int tlen = two.length(); for (int i = 0; i < slen && i < tlen; i++) { ans = ans + two.charAt(i) + one.charAt(i); } return ans; } ``` `cCbBaA`   `CcBbAa`   `aAbBcC`   `AaBbCc`   `other` Due to associativity we're back into building `String`s, and the result is obvious.

 31. Assume the following declaration: `int[][] a = { { 1, 2 }, { 3, 4, 5}, { 6 } }; ` What does the following code print? ```int sum = 0; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) sum = sum + a[i][j]; System.out.println(sum); ``` `12`   `20`   `21`   `180`   `other` 1 + 2 + 3 + 4 + 5 + 6

 32. Assume the following declaration: `int[][] a = { { 1, 2 }, { 3, 4, 5}, { 6 } }; ` What does the following code print? `System.out.println(a[1][0]); ` `1`   `2`   `3`   `5`   `other` First element in the second array.

 33. You compile and run this program. What is the output that it produces? ```class One { int a = 6; int b = 3; void fun() { b += 1; a -= 1; } void report() { System.out.print(a + " " + b + " "); } public static void main(String[] args) { One alpha = new One(); alpha.fun(); alpha.fun(); alpha.fun(); alpha.report(); } }``` `3 3 `   `3 6 `   `9 6 `   `3 0 `   `other` One instance, alpha, whose instance variables change as indicated.

 34. You compile and run this program. What is the output that you obtain? ```class One { int a = 6; int b = 3; void fun() { b += 1; a -= 1; } void report() { System.out.print(a + " " + b + " "); } public static void main(String[] args) { One alpha = new One(); One beta = new One(); alpha.fun(); beta.fun(); alpha.fun(); beta.report(); } }``` `5 4 `   `4 5 `   `3 6 `   `3 0`   `other` Same as above, except that what alpha does is not important here.

 35. You compile and run this program. What is the output that you obtain? ```class One { int a = 6; static int b = 3; void fun() { b += 1; a -= 1; // System.out.print(a + " " + b + " "); } void report() { System.out.print(a + " " + b + " "); } public static void main(String[] args) { One alpha = new One(); One beta = new One(); alpha.fun(); beta.fun(); alpha.fun(); beta.report(); } }``` `5 4 `   `5 6 `   `6 5 `   `4 5 `   `other` Please notice the typo (one line should have been removed). For this reason, again, two answers will be accepted. Notice that b is static so alpha and beta are sharing it. As far as a goes, alpha and beta have each their own.

 36. What is the result of attempting to compile and run the following code? ```class One { int value; One () { value = 1; } One (int v) { value = 0; } public static void main(String[] args) { One alpha = new One(1); One beta = new One(); System.out.println(alpha.value + " " + beta.value); } }``` `0 0`   `0 1`   `1 0`   `1 1`   `other` The constructors are really working backwards, but that was intentional.

 37. What is the result of attempting to compile and run the following code? ```class One { static int value; One (int v) { value = v; } public static void main(String[] args) { One alpha = new One(0); One beta = new One(3); System.out.println(alpha.value + " " + beta.value); } }``` `0 0 `   `3 0 `   `0 3 `   `3 3 `   `other` alpha and beta communicate through value

The next four problems have been affected by the same typo, corrected below. For this reason both the intended answer as well as the `other` alternative are accepted as correct.

 38. Assume the following declaration: `int[][] a = { { 1, 2 }, { 3, 4, 5}, { 6 } }; ` What does the following code print? ```// int value = 10; int count = 10; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) if (a[i][j] % 2 == 0) count = count + 1; System.out.println(count); ``` `3`   `7`   `10`   `13`   `other` There are three even numbers so count gets incremented by one for each one of them.

 39. Assume the following declaration: `int[][] a = { { 1, 2 }, { 3, 4, 5}, { 6 } }; ` What does the following code print? ```// int value = 10; int count = 10; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) if (a[i][j] % 2 == 0) count = count - 1; System.out.println(count); ``` `3`   `7`   `10`   `13`   `other` count decreases by one for each even number

 40. Assume the following declaration: `int[][] a = { { 0, 1, 2 }, { 3, 4, 5}, { 6 } }; ` What does the following code print? ```// int value = 10; int count = 10; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) if (a[i][j] % 2 == 0) count = count - 1; System.out.println(count); ``` `3`   `7`   `10`   `13`   `other` Same as above, only the array has changed.

 41. Assume the following declaration: `int[][] a = { { 0, 1, 2 }, { 3, 4, 5}, { 6 } }; ` What does the following code print? ```// int value = 10; int count = 10; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) if (a[i][j] % 2 == 0) count = 1 - count; System.out.println(count); ``` `3`   `7`   `10`   `13`   `other` 10, -9, 10, -9, 10 and the last one gets printed.

 42. What is the output produced by the following three lines (when embedded in a complete program)? ```int a, b; a = 8; b = 3; System.out.println(b * a / b + a % b);``` `3`   `5`   `8`   `24`   `other` The printed answer is 10.

 43. What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.) ```int x = 1; if (2 > x) System.out.print(1); else System.out.print(11); if (x < 5) { // System.out.println(1); System.out.print(1); System.out.print(1); } ``` `1`   `11`   `111`   `1111`   `other` I meant to have print statements everywhere, without any newlines.

 44. What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.) ```int x = 1; if (x > 2) { if (x < 5) System.out.print(1); } else System.out.print(1); System.out.print(11);``` `1`   `11`   `111`   `1111`   `other` No typos, what can I say. Easy problem.

 45. What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.) ```int x = 2; while (x > 1) x = x - 1; System.out.print(x);``` `1`   `2`   `21`   `0`   `other` One iteration and we're done.

 46. What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.) ```int i; for (i = 0; i < 3; i = i + 1) System.out.print(1); System.out.print(0); ``` `0`   `1110`   `11110`   `101010`   `10101010` No curly braces, proper indentation.

 47. What is the output produced by the following code when embedded in a complete program? (Note that the program may or may not be indented correctly so think about it carefully.) ```if (2 <= 3) if (0 != 1) System.out.print(0); else System.out.print(1); System.out.print(2); if (2 > 3) if (0 == 1) System.out.print(3); else System.out.print(4); System.out.print(5); ``` `023`   `024`   `025`   `125`   `other` Another basic, easy question about if statements, branches and such.

 48. What is the output produced by the following code when embedded in a complete program? ```boolean x; if (true) System.out.print(0); else System.out.print(1); x = (1 < 2) && (4 < 3); if (x) System.out.print(2); else System.out.print(3); ``` `02`   `12`   `03`   `13`   `other`

 49. What is the output produced by the following code when embedded in a complete program? ```boolean x = false; if (true) System.out.print(0); else System.out.print(1); x = x || !x; if (x) System.out.print(2); else System.out.print(3); ``` `02`   `12`   `03`   `13`   `other`

 50. What is the output produced by the following code when embedded in a complete program? ```boolean x = false; if (true) System.out.print(0); else System.out.print(1); x = (false && false || true); if (x) System.out.print(2); else System.out.print(3); ``` `02`   `12`   `03`   `13`   `other` && should be done first.

 51. What is the output produced by the following code when embedded in a complete program? ```boolean x = false; if (true) System.out.print(0); else System.out.print(1); x = (false && (false || true)); if (x) System.out.print(2); else System.out.print(3); ``` `02`   `12`   `03`   `13`   `other` Now the order of evaluation is dictated by the parens.

 52. What is the output produced by the following code when embedded in a complete program? ```int x = 10, y = 3; while (x > 0 && y > 0) { x = x - y; } System.out.print(x);``` `0`   `-1`   `-2`   `-3`   `other` Here's what happens with the two variables: y never changes, while x becomes 7, 4, 1, -2.

 53. What is the output produced by the following code when embedded in a complete program? ```int x = 10, y = 3; while (x > 0 && y > 0) { x = x - y; y = y + 1; } System.out.print(x);``` `0`   `-1`   `-2`   `-3`   `other` (x, y) goes through the following stages: (10, 3), (7, 4), (3, 5), and (-2, 6).

 54. What is the output produced by the following code when embedded in a complete program? (Note that the fragment may or may not be indented correctly so please think about it carefully.) ```int x; for (x = 0; x < 10; x = x + 2) if (x % 3 == 1) System.out.print(x); System.out.print(x % 3); ``` `4`   `41`   `02402`   `024102`   `other` Indentation is incorrect, so printing of the remainder happens only once. But because the only number whose remainder with 3 is 1 is 4 that really doesn't matter.

 55. Consider the following program, it prints out a pattern. Which of the following letter patterns is closest in shape to the pattern that the program produces? ```public class One { public static void main(String[] args) { int size = 10, x, y; for (x = 0; x < size; x++) { for (y = 0; y < size; y++) if ( y == 0 || y == size - 1 || (x - y == 0)) System.out.print(" *"); else System.out.print(" "); System.out.println(); } } }``` `Z`   `A`   `N`   `Y`   `other` Careful investigation reveals that the variable y counts columns, and that variable x counts lines. So the program prints two vertical bars and a diagonal (the one from WA to FL)

 56. Consider the following program, it prints out a pattern. Which of the following letter patterns is closest in shape to the pattern that the program produces? ```public class One { public static void main(String[] args) { int size = 10, x, y; for (y = 0; y < size; y++) { for (x = 0; x < size; x++) if ( y == 0 || y == size - 1 || (x + y == size - 1)) System.out.print(" *"); else System.out.print(" "); System.out.println(); } } }``` `Z`   `A`   `N`   `Y`   `other` Careful investigation reveals y as being the line counter, while x takes care of the columns this time. The test is almost the same, so we get two horizontal bars, and the other diagonal (from LA to NY).

The last two questions summarize the exam, don't you think?

`Wed Apr 4 19:00:00 EST 2001 (A201/A597/I210, Rawles Hall 100) `