Good luck and do well! |
CSCI A201/A597 Final Examination Summer II 2001 |
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!
| 1. You compile and run the following program. What does it print? | |
|
|
One can compile and run this code to obtain the
correct answer, which is: true
2. Consider the following condition.
Which of the following represents a simplification of it (obtained perhaps using DeMorgan).!(x % 4 != 0 || !(x % 100 == 0 && x % 400 == 0)) | |
| |
This question appears in the warmups for chapter 5.
3. Consider the following condition.
Which of the following represents a simplification of it.a == false | |
| |
This is a problem we have discussed in class.
4. 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; | |
| |
class One {
public static void main(String[] args) {
{ int x = 8, y = 0; if (x < 10) if (x > 5) y = 1; else y = 2;
System.out.println(y);
}
{ int x = 8, y = 0; if (x < 10) { if (x > 5) y = 1; } else y = 2;
System.out.println(y);
}
{ int x = 8, y = 0; if (x < 10) { if (x > 5) y = 1; else y = 2; }
System.out.println(y);
}
}
}
5. Consider the following program fragment.
What value does y have at the end?
| |
| |
6. Consider the following program fragment.
What value does y have at the end?
| |
| |
7. Consider the following program fragment.
What value does y have at the end?
| |
| |
frilled.cs.indiana.edu%
frilled.cs.indiana.edu%javac One.java
One.java:3: 'else' without 'if'.
int x = 8, y = 0; { if (x < 10) if (x > 5) y = 1; } else y = 2;
^
1 error
frilled.cs.indiana.edu%
8. How many question marks will be printed by the following code fragment?
| |
| |
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
for (int i = -10; i <= 10; i = i + 3)
System.out.print("?");
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
???????frilled.cs.indiana.edu%
9. How many question marks will be printed by the following code fragment?
| |
| |
Arguably the index of the loop had to be declared
explicitly for this to remove all doubts, so we have two correct
answers here, let's say.
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
for (int i = 7; i >= 0; i--)
System.out.print("?");
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
????????frilled.cs.indiana.edu%
10. How many question marks will be printed by the following code fragment?
| |
| |
other (the semicolon was intentional).
11. Consider the following fragment. What does it print?
| |
| |
This is known as swapping two numbers without a third variable.
Correct answers for this and the next problem are obtained as follows:
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
{
int x = 3, y = 11;
x = x + y;
y = x - y;
x = 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 One.java
frilled.cs.indiana.edu%java One
(11, 3)
(3, 11)
frilled.cs.indiana.edu%
12. Consider the following fragment. What does it print?
| |
| |
13. Consider the following fragment. What does it print?
int[] v = new int[10]; for (int i = 0; i < 10; i++) v[i] = i * i; System.out.print(v[3] + v[5]) | |
| |
other.
14. Consider the following fragment. What does it print?
| |
| |
other. The first answer will
also be considered correct, but please try to make this
code run and see how many reasons you collect in support
of the last of the options.
15. Consider the following program. What does it print?
| |
| |
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
int[] a = { 0, -1, 1, -2, 2, -3, 3};
int fun = a[a.length - 1];
for (int i = a.length - 1; i >= 0; i--)
if (a[i] > fun)
fun = a[i];
System.out.println(fun);
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
3
frilled.cs.indiana.edu%
16. Consider the following program fragment. What would it print?
| |
| |
17. Consider the following program fragment. What would it print?
| |
| |
18. Consider the following program fragment.
What's need for the output to resemble an H?
| |
| |
This is a problem like in lab six.
19. Consider the following command line invocation:
Assume your program contains this line insidejava One Two Three Four Five
What does this line print?
| |
| |
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
System.out.println(args[1]);
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One Two Three Four Five
Three
frilled.cs.indiana.edu%
20. Consider the following code fragment, what does it print?
String plum = "nectarine"; System.out.println(plum.substring(0, "plum".length()); | |
| |
Yes, I forgot to type a right parenthesis.
21. Consider the following code fragment, what does it print?
| |
| |
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
{
String plum = "nectarine";
System.out.println((char)('Q' - plum.length()));
}
{
String plum = "nectarine";
System.out.println((char)('Q' - "plum".length()));
}
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
H
M
frilled.cs.indiana.edu%
22. Consider the following code fragment, what does it print?
| |
| |
23. Does this source code compile?
| |
| |
Not all vegetables are tomatoes.
frilled.cs.indiana.edu%javac One.java
One.java:5: Incompatible type for declaration.
Explicit cast needed to convert Vegetable to Tomato.
Tomato a = new Vegetable();
^
1 error
frilled.cs.indiana.edu%
24. Does this source code compile?
| |
| |
Yes, all unicorns are horses (as defined in class).
25. Does this source code compile?
| |
| |
Not that it really matters, but to remove all doubts:
This exercise courtesy of
frilled.cs.indiana.edu%webster kohlrabi
kohl-ra-bi \ko^-l-'rab-e^-, -'ra^:b-\ n, pl -bies
[G, fr. It cavolo rapa, fr. cavolo cabbage (fr. L caulis stalk)
+ rapa turnip, fr. L -- more at HOLE, RAPE]
(1807)
:any of a race of cabbages having a greatly enlarged, fleshy, turnip-shaped
edible stem
frilled.cs.indiana.edu%
.
Maybe.
26. Does this source code compile?
| |
| |
Many things are not kohlrabi, and this
Java program knows that.
frilled.cs.indiana.edu%javac One.java
One.java:6: Incompatible type for declaration.
Explicit cast needed to convert Vegetable to Kohlrabi.
Kohlrabi a = new Vegetable();
^
1 error
[2] - Done emacs One.java
frilled.cs.indiana.edu%
27. You compile and run this code, what is the output?
| |
| |
For both this and the next exercise the report method should
have been declared as void to compile. So other
will be considered correct. Assume the compilation error is fixed then the
two questions both have a second correct answer, as indicated.
28. You compile and run this code, what is the output?
| |
| |
29. If you compile and run this code, what is the output?
| |
| |
30. If you compile and run this code, what is the output?
| |
| |
31. If you compile and run this code, what is the output?
| |
| |
32. What gets printed when you compile and run the following program?
| |
| |
Here's to running all our programs at least once:
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
System.out.println(kanga(kanga(kanga(kanga(ru(1, 2),
1),
2),
ru(1, 2)),
1));
}
public static int kanga(int a, int b) {
return a + b;
}
public static int ru(int a, int b) {
return a - b;
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
2
frilled.cs.indiana.edu%
33. What gets printed when you compile and run the following program?
| |
| |
'nuf said.
34. What does the next fragment print if embedded in a complete program?
String tsew = "stue".charAt(0) + "tsue".charAt(0) + "ew"; System.out.println(tsew); | |
| |
Just as discussed in class the plus between characters
happens first, which makes them participate with their
codes, thus producing an integer. The next plus is between
the resulting integer and a String and that
produces a String. Keep this in mind as you
enter the next three questions.
35. What does mix("bbb", "aaa") return if mix
is defined as below?
| |
| |
This program does not compile. And neither do the next two.
But does it matter?
Let's see... Suppose I fix it to compile:
For the same reasoning used in the exercise before this one.
So now we know the answers for the next two questions.
As you can see, it doesn't matter. frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
System.out.println(mix("bbb", "aaa"));
}
public static String mix(String one, String two) {
String answer = "";
int lengthOne = one.length(), lengthTwo = two.length();
for (int i = 0; i < lengthOne && i < lengthTwo; i++) {
answer = one.charAt(i) + two.charAt(i) + answer;
}
return answer;
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
195195195
frilled.cs.indiana.edu%
36. What does mix("bd", "pq") return if mix
is defined as below?
| |
| |
No characters can come out of this code on
the left of the original value of answer, as the
characters codes will be summed up into integers. Here's a version
of this program that compiles:
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
System.out.println(
mix("bd", "pq")
);
}
public static String mix(String one, String two) {
String answer = "";
int lengthOne = one.length(), lengthTwo = two.length();
for (int i = 0; i < lengthOne && i < lengthTwo; i++) {
answer = one.charAt(i) + two.charAt(i) + answer + two.charAt(i);
}
return answer;
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
213210pq
frilled.cs.indiana.edu%
37. What does mix("bd", "pq") return if mix
is defined as below?
| |
| |
I did get distracted when I was putting these together on Tuesday and I didn't get the time to review them. Hence the typos. But the outcome of the exercises would have still been the same, and I told you about it at the beginning of the exam today.
38. Assuming that the following program fragment is syntactically correct...
int[][] a = new int[10][10]; ... a[0][1] = fun(a[2], a); | |
... select the correct header for method fun.
| |
For this and the next three questions
I left out some commas and the names of the formal params. So for
this reason I will count other as a good answer. But
I will also indicate the answers that are closest (syntactically
speaking) to the right answers.
39. Assuming that the following program fragment is syntactically correct...
int[][] a = new int[10][10]; ... a[0] = fun(a[1][2], a[3]); | |
... select the correct header for method fun.
| |
40. Assuming that the following program fragment is syntactically correct...
int[][] a = new int[10][10]; ... a = fun(a[1], a[2][3]); | |
... select the correct header for method fun.
| |
41. You compile this program ...
... then run it as follows:
java One this is my song for the asking | |
What does the program print (if anything)?
| |
42. You compile this program ...
... then run it as follows:
java One ask me and I'll play so sweetly I'll make you smile | |
What does the program print (if anything)?
| |
| 43. You compile and run this program. What's the output? | |
| |
| |
| 44. You compile and run this program. What's the output? | |
| |
| |
I left behind the name of the class
from the previous exercise. So there are three cases: you either
have that class around, in which case this compiles and prints 2.
Or you don't, in which case this does not compile. Or you make
the change (from Four to Five) and
then this prints 4. So the third, the fifth, amd the fifth
again were the right answers, and all the others impossible.
A201/A597 TTFN Exam Fri Aug 10 2001 in SW119