# CSCI A201/A597 Lecture Notes Sixteen

Second Summer 2000

Let's practice some more.
 What do we do today? I'd like to work some exercises.

 What kind? The toughest, most tedious kind, if possible.

 What `for`? I just want to be in very good shape.

 Then let's pretend we are taking an exam. Great. Let's do it.

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 questions carry the same weight, they're each worth exactly one point. 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 closed-book.

Good luck and do well!

 1. What is the output produced by the following three lines (when embedded in a complete program)? ```int x, y; x = 2; y = 3; System.out.println("1" + x + y); ``` `123`   `33`   `13`   `15`   `6`
 2. What is the output produced by the following three lines (when embedded in a complete program)? ```int x, y; x = 2; y = 3; System.out.println("1" + (x + y)); ``` `123`   `33`   `13`   `15`   `6`
 3. What is the output produced by the following two lines (when embedded in a complete program)? ```int a; a = 'E' - 'B'; System.out.println(a); ``` `E`   `B`   `C`   `3`   `0`

 Wait, wait, wait. Isn't this new?

 Somewhat. Have you seen anything like it before? I don't really think so...

 How about the table on page 732. I used that in lexicographic ordering. So characters have codes that act as whole numbers sometimes.

 Yes, and we need to revisit this later. But meanwhile it's a good intermezzo. Let's move on...

 Yes, this was an intermezzo.

 4. What is the output produced by the following program fragment (when embedded in a complete program)? Note that the indentation may or may not be right so please be careful. ```if ('C' < 'A') System.out.print("1" + 3 / 2); System.out.print(2);``` `1`   `11`   `12`   `21`   `2`

 This is tricky, but I can make an educated guess now.

 That was the whole point. I see you have one more and then perhaps we can get into the real deal.

 5. What is the type of this expression: `"1" + 'a'` `int`   `char`   `boolean`   `double`   `String`
 6. What is the output produced by the following three lines (when embedded in a complete program)? ```int a, b; a = 12; b = 5; System.out.println(b * (a / b) + a % b);``` `5`   `12`   `14`   `60`   `300`
 7. What is the output produced by the following three lines (when embedded in a complete program)? ```int a, b; a = 12; b = 5; System.out.println(b * a / b + a % b);``` `5`   `12`   `14`   `60`   `300`
 8. 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 = 3; if (2 > x) System.out.print(1); else System.out.print(2); if (x < 2) System.out.println(3); System.out.print(4);``` `23`   `24`   `234`   `134`   `1234`
 9. 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 = 3; if (x > 5) { if (x < 10) System.out.print(1); } else System.out.print(2); System.out.print(3);``` `1`   `2`   `13`   `12`   `23`
 10. 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 = 14; while (x < 15) x = x + 1; System.out.print(x);``` `14`   `15`   `1415`   `1516`   `141516`
 11. 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`
 12. 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`   `145 `
 13. 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`   `0123`
 14. 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`   `0123`
 15. What is the output produced by the following code when embedded in a complete program? ```int x = 3; if (x > 0) System.out.print(x + 1); else if (x > 1) System.out.print(x); else if (x > 2) System.out.print(x - 1); else if (x > 3) System.out.print(2 * x); else System.out.print(x * x);``` `2`   `3`   `4`   `6`   `9`
 16. What is the output produced by the following code when embedded in a complete program? (The code is identical to the one in problem 15 above, except two `else`'s have been removed). As always, please be careful about indentation in the resulting program. ```int x = 3; if (x > 0) System.out.print(x + 1); else if (x > 1) System.out.print(x); else if (x > 2) System.out.print(x - 1); else if (x > 3) System.out.print(2 * x); else System.out.print(x * x);``` `4`   `43`   `46`   `369`   `439`
 17. 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);``` `-7`   `-3`   `-2`   `-1`   ` 4`
 18. 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);``` `-7`   `-3`   `-2`   `-1`   ` 4`
 19. 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);``` `-7`   `-3`   `-2`   `-1`   `4`
 20. 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); }``` `true`   `false`

 I've seen this one before.

 And the next one. You have to be patient with these...

 21. 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); }``` `true`   `false`
 22. Is this an example of an infinite loop?
 Oh, this kind of questions I like best!

 Very good. Is it an infinite loop or not?

 ```int count = 0; while (!(count != count)) { System.out.println(count); count = count + 1; } ``` `Yes.`   `No.`
 23. Is this an example of an infinite loop? ```int x = 0; while (x == 0) { x = x + 1; System.out.println(x); x = x % x; } ``` `Yes.`   `No.`
 24. Is this an example of an infinite loop? ```int x = -1; while (x != 10) { System.out.println(x); x = x + 2; } ``` `Yes.`   `No.`
 25. Is this an example of an infinite loop? ```int x = -2; if (x != 10) { System.out.println(x); x = x + 3; } ``` `Yes.`   `No.`
 What do you think?

 This last one's a bit cruel...
 26. Is this an example of an infinite loop? ```int i; for (i = 0; i < 10; i = i + 1) { System.out.println(i); i = i - 3; }``` `Yes.`   `No.`

 Ready for some more? Infinite loops exercises are nifty.

 Easy perhaps. What's next?

 Some heavy-duty `for` loops. Looking at the answers I feel loopy already.

 Ignore the answers. They are designed to give you as little help as possible. I was going to say...

 27. 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`   none of the above
 28. 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`   none of the above
 29. 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`   none of the above
 30. 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); else System.out.print(x % 3); ``` `4`   `41`   `02402`   `024102`   none of the above

 I can see you're in a state of amazement so here's two of your favorite kind...

 31. Is this an example of an infinite loop? ```int count = -1; for (count = count + 1; count + 1 > 0; count = count + 1) { System.out.println(count); } ``` `Yes.`   `No.`
 32. Is this an example of an infinite loop? ```int count = -1; for (count = count + 1; count + 1 > 0; count = count + 1) { System.out.println(count); count = count - 2; }``` `Yes.`   `No.`

 Don't worry, we'll take care of that...

 33. 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(); } } }``` `A`   `Z`   `N`   `Y`   none of the above
 34. 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(); } } }``` `A`   `Z`   `N`   `Y`   none of the above

 They're both in a shape of Y for me...

 If you think carefully about them they're EZ... The first one might be a Z but the second one?

 N-EZ-problem as well. Easy for you to say that.

 U bet. Hang in there!

 35. Assume the code fragment below. Does anything change in the way `z` gets updated if we remove the `else` keyword? ```if (x > y) z = z + 1; else if (x <= y) z = z + 2;``` `Yes.`   `No.`
 36. Assume the code fragment below. Does anything change in the way `z` gets updated if we remove the `else` keyword? ```if (! (x > y)) z = z + 2; else z = z + 1;``` `Yes.`   `No.`

 These last ones were a bit better. Can we take a break now and get updated?

 Sounds reasonable.

Last updated: July 17, 2000 by Adrian German for A201