|
Spring Semester 2003 |
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. Each question is worth 1.8 points (for a total of 99
points).
Good luck and do well!
Write your name or username here:______________________
For the following 5 (five) exercises consider the following method definitions:
public static int f(int x) {
return h(h(x));
}
public static int g(int x) { return k(k(x)); }
public static int h(int x) { return 1 + k(x); }
public static int k(int x) { return x - 1; }
Question 1. What does g(2) evaluate to?
| |
| |
| Note: To save space I wrote
| |
Question 2. What does g(h(2)) evaluate to?
| |
| |
Question 3. What does k(g(2) + h(2)) evaluate to?
| |
| |
Question 4. What does f(0) + f(1) + f(2) evaluate to?
| |
| |
Question 5. What does f(2) evaluate to?
| |
| |
Question 6. "0" is an example of what kind of constant?
| |
| |
Question 7. Assume that x is an integer variable. Simplify
the following expression: | |
!(((x - 1) >= 4) && ((x - 1) <= 4)) |
|
Warning: Don't overlook the logical negation (bang!) at the front.
Question 8. Consider the following two program fragments. Assume that x is an int variable.
Which of the following statements is false?
|
|
|
Reminder: Last option above uses the selection operator (page 189 in your text).
Question 9. You compile and run this program. What is the output (or outcome)? | |
|
|
Question 10. Assume that x and
y are
integer variables and the following nested if statement.
If y has a value of 3 after executing the
above program fragment, then what do you know about x?
| |
|
|
Question 11. Which of the following expressions does not print a 4 (four)?
| |
|
|
| Note: There are no typos! | |
Question 12. After the following statement is executed, | |
int x = (int)(Math.random() * (30 - 10) + 20); |
|
which of these numbers could NOT possibly be contained in x?
| |
Question 13. What is the output produced by the following three lines (when embedded in a complete program)? | |
|
|
Question 14. What is the output produced by the following three lines (when embedded in a complete program)? | |
|
|
Question 15. What is the output produced by the following three lines (when embedded in a complete program)? | |
|
|
Question 16. 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); |
|
Question 17. What is the output produced by the following program fragment (when embedded in a complete program)? | |
|
|
Question 18. What is the type of this expression: | |
"1" + 'a' |
|
Question 19. 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); |
|
Question 20. What's the output produced by the following lines (when embedded in a complete program)? | |
|
|
Question
21. What's the output produced by the following lines (when embedded in a complete program)? | |
|
|
Question 22. Given String a = "tomato"; which of the following expressions is NOT of type String?
| |
|
|
Question 23. 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()); |
|
Question 24. Which of the following expressions is NOT logically equivalent to (a >= b)?
| |
|
|
Question 25. What's the output produced by the following lines (when embedded in a complete program)? | |
|
|
Question 26. Consider the following expression in which m is an int.
Which of the following is a valid Java simplification of the expression above?(m < 10) && (m > 12) | |
|
|
Question 27. 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; |
|
Question 28. 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); |
|
Question 29. 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.) | |
|
|
Question 30. 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.) | |
|
|
Question 31. 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); |
|
Question
32. 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); |
|
Question 33. 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.) | |
|
|
Question 34. 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); |
|
Question 35. 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); |
|
Question 36. 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); |
|
Question 37. 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); |
|
Question 38. What is the output produced by the following code when embedded in a complete program? | |
|
|
Question
39. What is the output produced by the following code when embedded in a complete program? | |
|
|
Question 40. 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.) | |
|
|
Question 41. 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? | |
|
|
Question 42. 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? | |
|
|
Question 43. Assume that
false?
| |
|
|
Question 44. What gets printed when you compile and run the following program? | |
|
|
| There are no typos, so please don't rush! | |
Question 45. You compile and run this contrived program. What is the output? | |
|
|
Question 46. Exactly how many question marks will appear on the screen when the following program fragment is executed? | |
|
|
Question 47. What does the following program's output most closely resemble? | |
|
|
Question 48. You compile and run this program. What is the output that it produces? | |
|
|
Question 49. You compile and run this program. What is the output that you obtain? | |
|
|
Question 50. You compile and run this program. What is the output that you obtain? | |
|
|
Question 51. What is the result of attempting to compile and run the following code? | |
|
|
Question 52. Which of the following numbers is closest to the output of this line if embedded in a complete program, that is then compiled and run? | |
int a, b; a = 10; b = 4; System.out.println(b * a / b + a % b); |
|
Question 53. What is the output produced by the following code when embedded in a complete program? | |
if (false) System.out.print(0); else System.out.print(1); boolean x = (1 < 2) && (! (4 < 3)); if (x) System.out.print(2); else System.out.print(3); |
|
Question 54. What is the output produced by the following code when embedded in a complete program? | |
boolean x; if (x = (false && (false || true))) System.out.print(0); else System.out.print(1); if (! x) System.out.print(2); else System.out.print(3); |
|
Question 55. What is the output produced by the following code when embedded in a complete program? | |
boolean x; if (x = (false && false || true)) System.out.print(0); else System.out.print(1); if (! x) System.out.print(2); else System.out.print(3); |
|