You can do it! | CSCI A201/A597/I210 Final Examination Summer II 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.
Good luck and do well! (The answers are marked in yellow).
1. Consider the following condition.
Which of the following represents a simplification of it?(x % 4 == 0 || (x % 100 == 0 && x % 400 == 0))
| |
| |
The real simplification would be x % 4 == 0.
Here's the idea of a test you could make:
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
int[] count = new int[5];
for (int x = 0; x < 1000; x++) {
boolean cond = (x % 4 == 0 || (x % 100 == 0 && x % 400 == 0));
boolean a = ((x % 100) == 0),
b = ((x % 400) == 0),
c = ((x % 100) != 0),
d = ((x % 400) != 0),
ans = ((x % 4 == 0));
if (cond != a) count[0] += 1;
if (cond != b) count[1] += 1;
if (cond != c) count[2] += 1;
if (cond != d) count[3] += 1;
if (cond != ans) count[4] += 1;
}
for (int i = 0; i < count.length; i++)
System.out.println(i + ": " + count[i] / 1000.0 + "%");
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
0: 0.24%
1: 0.247%
2: 0.76%
3: 0.753%
4: 0.0%
frilled.cs.indiana.edu%
What's printed here is the degree of wrongness. (There was a question yesterday in lab about testing the right answer).
2. Consider the following condition.
Which of the following represents a simplification of it.! (a == ! false) | |
| |
Go ahead and test it, like above.
But we've discussed this in class and lecture notes.
3. Consider the following program fragment.
What value does y have at the end?int x = 12, y = 0; if (x < 10) if (x > 5) y = 1; else y = 2; | |
| |
For this and the next problem here's the proof:
frilled.cs.indiana.edu%cat ThreeandFour.java
class ThreeandFour {
public static void main(String[] args) {
{
int x = 12, y = 0; if (x < 10) if (x > 5) y = 1; else y = 2;
System.out.println(y);
}
{
int x = 8, y = 0; if (x < 6) { if (x > 5) y = 1; } else y = 2;
System.out.println(y);
}
}
}
frilled.cs.indiana.edu%javac ThreeandFour.java
frilled.cs.indiana.edu%java ThreeandFour
0
2
frilled.cs.indiana.edu%
4. Consider the following program fragment.
What value does y have at the end?
| |
| |
5. Consider the following program fragment.
What value does y have at the end?
| |
| |
If you left this question unanswered you will also get a point.
6. Consider the following program fragment.
What value does y have at the end?int x = 8, y = 0; if (x < 10) ; if (x > 5) y = 1; else y = 2; | |
| |
7. How many question marks will be printed by the following code fragment?
| |
| |
Only one, because of the for loop having an empty body.
8. How many question marks will be printed by the following code fragment?
| |
| |
This one (the for) was normal.
9. Consider the following fragment. What does it print?
| |
| |
For this and the next one here's the test:
frilled.cs.indiana.edu%cat NineandTen.java
class NineandTen {
public static void main(String[] args) {
{
{
int x = 3, y = 11;
x = x + y;
x = x - y;
y = 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 NineandTen.java
frilled.cs.indiana.edu%java NineandTen
(3, -8)
(19, -11)
frilled.cs.indiana.edu%
10. Consider the following fragment. What does it print?
| |
| |
11. Consider the following fragment. What does it print?
int[] v = new int[10]; for (int i = 0; i < 10; i++) v[i] = 2 * i; System.out.print(v[3] + v[1]) | |
| |
A semicolon (;) was missing for the program to compile.
Otherwise it would give 8.
12. Consider the following fragment. What does it print?
| |
| |
First off, the sum variable is local to the for for this to compile.
If that is taken care of, a java.lang.ArrayIndexOutOfBoundsException will be thrown (why?)
However, should that be taken care of the result would be 6.
(You constantly subtract negative numbers.)
13. Consider the following program. What does it print?
| |
| |
14. Consider the following program fragment.
What's need for the output to resemble an N ?
| |
| |
(As seen from the inside of the computer...)
15. Consider the following command line invocation:
Assume your program contains this line insidejava One Two Three Four Five What does this line print?
| |
| |
The name of the program is not negotiable here:
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
System.out.println(args[args.length - 1]);
}
}
frilled.cs.indiana.edu%java One Two Three Four Five
Five
frilled.cs.indiana.edu%
16. Consider the following code fragment, what does it print?
| |
| |
There was a close paren missing for this to compile.
If it did it would print a.
17. Consider
the following code fragment, what does it print?
| |
| |
One only needs to know the alphabet here.
18. Consider the following code fragment, what does it print?
| |
| |
19. Does this source code compile?
| |
| |
20. Does this source code compile?
| |
| |
21. Does this source code compile?
| |
| |
22. Does this source code compile?
| |
| |
23. You compile and run this code, what is the output?
| |
| |
No return when a return type is specified?
If it were to compile the answer would be 10.
24. If you
compile and run this code, what is the output?
| |
| |
Easy question.
25. If you compile and run this code, what is the output?
| |
| |
Pass by value (see Lab Two).
26. If you compile and run this code, what is the output?
| |
| |
27. What gets printed when you compile and run the following program?
| |
| |
I hope that's what you got too.
28. Assuming that the following program fragment is syntactically correct...
int[][] a = new int[10][10]; ... a[0][1] = fun(a[2], a[0][0]); | |
... select the correct header for method fun.
| |
29. Assuming
that the following program fragment is syntactically correct...
int[][] a = new int[10][10]; ... a[0] = fun(a[1][2], a); | |
... select the correct header for method fun.
| |
30. Assuming that the following program fragment is syntactically correct...
int[][] a = new int[10][10]; ... a = fun(a[1], a); | |
... select the correct header for method fun.
| |
31.
What is the result of attempting to compile and run this code?
| |
| |
32.
What is the result of attempting to compile and run this code?
| |
| |
33.
What is the result of attempting to compile and run this code?
| |
| |
34.
What is the result of attempting to compile and run this code?
| |
| |
35. What is the result of attempting to compile and run this code?
| |
| |
36.
What is the result of attempting to compile and run this code?
| |
| |
A201/A597/I210 TTFN Exam Thu Aug 14 2003 in LH102