| CSCI A201/A597Lecture Notes Sixteen Second Summer 2000 |
| 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. |
Good luck and do well!
Write your name or username here:______________________
| 1. What is the output produced by the following three lines (when embedded in a complete program)? | |
|
|
| 2. What is the output produced by the following three lines (when embedded in a complete program)? | |
|
|
| 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); |
|
| 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. | |
|
|
| 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' |
|
| 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); |
|
| 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); |
|
| 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); |
|
| 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.) | |
|
|
| 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); |
|
| 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.) | |
|
|
| 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.) | |
|
|
| 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); |
|
| 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); |
|
| 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); |
|
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); |
|
| 17. What is the output produced by the following code when embedded in a complete program? | |
|
|
| 18. What is the output produced by the following code when embedded in a complete program? | |
|
|
| 19. What is the output produced by the following code when embedded in a complete program? | |
|
|
| 20. What is the output produced by the following code when embedded in a complete program? | |
|
|
| 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? | |
|
|
| 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? |
|
|
| 23. Is this an example of an infinite loop? | |
|
|
| 24. Is this an example of an infinite loop? | |
|
|
| 25. Is this an example of an infinite loop? | |
|
|
| What do you think? |
| This last one's a bit cruel... |
| 26. Is this an example of an infinite loop? | |
|
|
| 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.) | |
|
|
| 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.) | |
|
|
| 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.) | |
|
|
| 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.) | |
|
|
| 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?
|
|
32. Is this an example of an infinite loop?
|
|
| OK, I feel refreshed already! |
| 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? | |
|
|
| 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? | |
|
|
| 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; |
|
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; |
|
| 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