| CSCI A201/A597 and I210 Midterm Exam One
Spring Semester 2002
|
Directions:
- 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.
-
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.
- Don't forget to write your name and student ID on the scannable answer
sheet provided. You have two hours (120 minutes) to complete the test. The
test is open-book.
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)? |
int x, y;
x = 2; y = 3;
System.out.println(1 + "x" + y);
|
-
123
-
33
-
13
-
15
-
 other
|
| Note: To save space I wrote
other where I would have put
"None of the above." |
The program prints 1x3
so the correct answer is other.
|
2. What is the output produced by the following three
lines (when embedded in a complete program)? |
int x, y;
x = 3; y = 2;
System.out.println("1" + (x + y));
|
-
132
-
33
-
13
-
 15
-
6
|
|
3. What is the output produced by the following
two lines (when embedded in a complete
program)? |
char a; a = 3 + 'B';
System.out.println(a);
|
-
 E
-
B
-
C
-
3
-
other
|
|
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' + 3)
System.out.print(1 + 3 / 2);
System.out.print(2);
|
-
2
-
1.52
-
12
-
 22
-
other
|
|
5. What is the type of this expression: |
"1" + 'a'
|
-
int
-
char
-
boolean
-
double
-
 other
|
The type is String
so the answer is other.
|
6. What is the type of this expression: |
1 == 3
|
-
int
-
char
-
 boolean
-
double
-
other
|
|
7. What is the type of this expression: |
1 / 2
|
-
 int
-
char
-
boolean
-
double
-
other
|
|
8. What is the type of this expression: |
"abc".length()
|
-
 int
-
char
-
boolean
-
double
-
other
|
|
9. What is the type of this expression: |
"abc".length() == 3
|
-
int
-
char
-
 boolean
-
double
-
other
|
|
10. What is the type of this expression: |
"appearance".substring(2, 6)
|
-
int
-
char
-
boolean
-
double
-
 other
|
The type is String so
the correct answer is other.
|
11. What is the type of this expression: |
"abc".toUpperCase()
|
-
int
-
char
-
boolean
-
double
-
 other
|
The type is String
so the correct answer is other.
|
12. What is the output produced by the following three
lines (when embedded in a complete program)?
|
int a, b;
a = 3; b = 5;
System.out.println(b * (a / b) + a % b);
|
-
1
-
2
-
 3
-
6
-
other
|
|
13. What is the output produced by the following three
lines (when embedded in a complete program)?
|
int a, b;
a = 3; b = 5;
System.out.println(b * a / b + a % b);
|
-
1
-
2
-
3
-
 6
-
other
|
|
14. 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("*");
else
System.out.print("*");
if (x < 2)
System.out.println("*");
System.out.print("*");
|
-
*
-
 **
-
***
-
****
-
other
|
|
15. 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("**"); }
else
System.out.print("*");
System.out.print("*");
|
-
*
-
 **
-
***
-
****
-
other
|
|
16. 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 || x > 10)
System.out.print("*");
if (x < 5 && x > 10)
System.out.print("*");
if (x < 10 && x > 5)
System.out.print("*");
if (x < 10 && x > 5)
System.out.print("*");
System.out.print("*");
|
-
*
-
 **
-
***
-
****
-
other
|
|
17. 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 || x > 10)
System.out.print("*");
else if (x < 5 && x > 10)
System.out.print("*");
else if (x < 10 && x > 5)
System.out.print("*");
else if (x < 10 && x > 5)
System.out.print("*");
else System.out.print("*");
|
-
 *
-
**
-
***
-
****
-
other
|
|
18. 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 && 0 != 1)
System.out.print("*");
else
System.out.print("*");
System.out.print("*");
if (2 > 3 || 0 == 1)
System.out.print("*");
else
System.out.print("*");
System.out.print("*");
|
-
*
-
**
-
***
-
 ****
-
other
|
|
19. What is the output produced by the following code when embedded in a
complete program?
|
boolean x = false;
if (true)
System.out.print(10);
else
System.out.print(1);
x = x || !x;
if (x)
System.out.print(0);
else
System.out.print(00);
|
-
10
-
 100
-
1000
-
10000
-
other
|
|
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
|
|
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. 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
|
|
23. 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
|
|
24. 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
|
|
25. 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
|
|
26. 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
|
|
27. What is the output produced by the
following code when embedded in a complete program?
|
if (!true) {
System.out.print(false);
} else {
System.out.print(true);
}
|
-
 true
-
false
|
|
28. What is the output produced by the
following code when embedded in a complete program?
|
if (false) {
System.out.print(false);
} else {
System.out.print(true);
}
|
-
 true
-
false
|
|
29. What is the output produced by the
following code when embedded in a complete program?
|
if ((2 > 3) == false) {
System.out.print(false);
} else {
System.out.print(true);
}
|
-
true
-
 false
|
|
30. What is the output produced by the
following code when embedded in a complete program?
|
if (! (2 > 3)) {
System.out.print(false);
} else {
System.out.print(true);
}
|
-
true
-
 false
|
|
31. What is the output produced by the
following code when embedded in a complete program?
|
if (2 <= 3) {
System.out.print(false);
} else {
System.out.print(true);
}
|
-
true
-
 false
|
32. Are these two code fragments equivalent (do they
always change x in the exact same way)?
|
|
| Fragment 1 |
Fragment 2 |
if (x == 8)
x = x + 3;
else
x = 5;
|
if (x == 8)
x = x + 3;
if (x != 8)
x = 5;
|
|
-
Yes.
-
 No.
|
They are almost equivalent. The
only value of x for which they don't behave the
same is 8 (eight).
For
the following 5 (five) exercises consider
the following method definitions:
public static int f(int x) {
return x + 1;
}
public static int g(int x) { return x + 2; }
public static int h(int x) { return x - 2; }
public static int k(int x) { return x - 1; }
Here's how you find the answer to 33-37:
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
System.out.println( g(2) );
System.out.println( g(h(2)) );
System.out.println( k(g(2) + h(2)) );
System.out.println( f(0) + f(1) + f(2) );
System.out.println( f(2) );
}
public static int f(int x) {
return x + 1;
}
public static int g(int x) { return x + 2; }
public static int h(int x) { return x - 2; }
public static int k(int x) { return x - 1; }
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
4
2
3
6
3
frilled.cs.indiana.edu%
33. What does g(2) evaluate to?
|
|
|
- 0
- 1
- 2
- 3
-
 other
|
34. What does g(h(2)) evaluate to?
|
|
|
- 0
- 1
-
2
- 3
-
other
|
|
|
35. What does k(g(2) + h(2)) evaluate to?
|
|
|
- 0
- 1
- 2
-
3
-
other
|
|
|
36. What does f(0) + f(1) + f(2) evaluate to?
|
|
|
- 0
- 1
- 2
- 3
-
 other
|
|
|
37. What does f(2) evaluate to?
|
|
|
- 0
- 1
- 2
-
3
-
other
|
|
|
38. "true" is an example of what kind of constant?
|
|
|
-
int
-
char
-
boolean
-
 String
-
other
|
|
|
39. true is an example of what kind of constant?
|
|
|
-
int
-
char
-
 boolean
-
String
-
other
|
|
|
40. Assume that x is an integer variable. Simplify
the following expression: |
!((x >= 4) && (x <= 4))
|
-
true
-
false
-
 x != 4
-
x == 4
-
other
|
|
|
Warning: Don't overlook the logical negation (bang!) at the front.
|
41. How many instance methods do you see defined below?
|
class One {
int balance;
int f() { return 1; }
static int g() { return 2; }
One(int x) { this.balance = x; }
public static void main(String[] args) { }
}
|
- 0
-
1
- 2
- 3
-
other
|
|
|
|
42. How many static methods do you see defined below?
|
class One {
int balance;
int f() { return 1; }
static int g() { return 2; }
One(int x) { this.balance = x; }
public static void main(String[] args) { }
}
|
- 0
- 1
-
2
- 3
-
other
|
|
|
|
43. How many instance variables do you see defined below?
|
class One {
int n;
int m;
int fun(int p) {
int q = p + 1;
return q;
}
}
|
- 0
- 1
-
2
- 3
-
other
|
|
|
|
44. How many instance variables do you see defined below?
|
class One {
static int n;
int m;
int fun(int p) {
int q = p + 1;
return q;
}
}
|
- 0
-
1
- 2
- 3
-
other
|
|
|
|
45. How many instance variables do you see defined below?
|
class One {
static int n;
int fun(int p) {
int m;
int q = p + 1;
return q;
}
}
|
-
0
- 1
- 2
- 3
-
other
|
|
|
|
46. How many local variables do you see defined below?
|
class One {
static int n;
int fun(int p) {
int m;
int q = p + 1;
return q;
}
}
|
- 0
- 1
-
2
- 3
-
other
|
|
|
|
47. You compile and run this program. What is the output (or outcome)?
|
class Wan {
int value;
int addOne() { value += 1; return 0; }
void report() { System.out.println(value); }
public static void main(String[] args) {
Wan a = new Wan();
Wan b = new Wan();
a.addOne();
b.addOne();
b.addOne();
a.addOne();
a.report();
}
}
|
- 0
- 1
-
2
- 3
-
other
|
|
|
The focus of this exercise and the next was the difference
between static and non-static variables.
|
48. You compile and run this program. What is the output (or outcome)?
|
class Wan {
static int value;
int addOne() { value += 1; return 0; }
void report() { System.out.println(value); }
public static void main(String[] args) {
Wan a = new Wan();
Wan b = new Wan();
a.addOne();
b.addOne();
b.addOne();
a.addOne();
a.report();
}
}
|
- 0
- 1
- 2
- 3
-
 other
|
|
|
This one prints a 4 (four)
so the answer is other.
49. Which of the following expressions does NOT print a 4 (four)?
|
-
System.out.println('P' - 'K');
-
System.out.println(3 % 5 + 1);
-
System.out.println("123".length() + 1);
-
System.out.println((char)('3' + 1));
-
System.out.println(16 / (1 + 1) / 2);
|
|
|
Note: There are no typos!
|
|
50. What's the output produced by the following lines (when embedded in a complete program)?
|
String a = "blue";
String b = "berry";
System.out.println(
"blueberry".substring(a.length(), b.length()));
|
-
 b
-
l
-
u
-
e
-
other
|
|
|
|
51. What gets printed when you compile and run the following program?
|
public class A {
public static void main(String[] args) {
System.out.println(nuf(fun(3, 2), 1));
}
public static int fun(int a, int b) {
return a - b;
}
public static int nuf(int b, int a) {
return a - b;
}
}
|
-
 0
-
1
-
2
-
3
-
other
|
|
There are no typos, so please don't rush!
|
|
52. What is the result of attempting to compile and run the following code?
|
class Example {
public static void main(String[] args) {
Example e = new Example();
System.out.println(e.fun(e.fun()));
}
int fun() { return 1; }
int fun(int n) { return 1 + n; }
}
|
-
0
-
1
-
 2
-
3
-
other
|
|
|
A201/A597/I210 Midterm Exam Wed Feb 13 2002 in RH100