 # CSCI A201/A597 Final Exam Answers

Second Summer 2000

Directions:

• Each of the questions below is followed by a number of suggested answers. Please read the questions carefully and select the one answer that is best, then mark the corresponding bubble on the answer sheet for each question.

• Work as rapidly as you can without being careless. This includes checking frequently to make sure that you are marking your answers in the appropriate rows on your answer sheet.

• All questions carry the same weight, they're each worth exactly one point. Don't forget to write your name and student ID on the scannable answer sheet provided. You have 120 minutes to complete the test. This test is closed-book.

Good luck and do well!

Question # : 1.

The price of some item is stored in a `double` variable named `cost`. The sales tax on this item is 5 percent. Which of the following statements will increase the value in `cost` by the sales tax?

1. `cost * .05;`
2. `cost + cost * .05;`
3. `cost = cost * .05;`
4. `cost = cost + cost * .05;`
5. `cost = Integer.parseInt(cost * .05);`

Question # : 2. Consider the following program fragment:
`System.out.println(2 - 4 / 3); `
What is it printing?
1. an error message
2. 1
3. 0.7
4. 1.3
5. 0.66

Question # : 3. The expression `"age = " + 20` evaluates to:
1. `"age = + 20"`
2. `"age=20"`
3. `"age = 20"`
4. `20`
5. this expression contains a syntax error

Question # : 4.

The user runs a program containing the following fragment and then types `29` at the prompt.

```int w;
System.out.print("Enter w: ");
int a, b;
a = w / 16;
b = w % 16;
System.out.println(a + b);
```
What is printed by the last output statement?
1. `113`
2. `14`
3. `"1 13"`
4. `"13 1"`
5. an error message

Question # : 5. Consider the following program fragment:
```int x, y;
x = 5;
y = 3;
x = x + 1;
y = x;
x = x - 1;
System.out.println("x = " + x + ", y = " + y);```
What is printed by the last statement in the program?
1. `x = 3, y = 5`
2. `x = 3, y = 3`
3. `x = 4, y = 4`
4. `x = 3, y = 4`
5. none of the above

Question # : 6. Given
`String quote = "There's a tomato in every automaton!";`
What does `quote.substring(29, 35)` evaluate to?
1. `"utomat"`
2. `"utomato"`
3. `"tomato"`
4. `"tomaton"`
5. none of the above

Question # : 7.

The user runs a program containing the following fragment and then types `Alice` at the prompt.

```ConsoleReader c = new ConsoleReader(System.in);
String name;
name = name.toUpperCase();
System.out.println("Hello " + name + "!");
```
What is printed by the last output statement?
1. `Hello Alice!`
2. `Hello ALICE!`
3. `HELLO Alice!`
4. `HELLO ALICE!`
5. none of the above

Question # : 8. Given
`String vehicle = "snowmobile";`
Which of the following expressions is NOT of type `String`.
1. `vehicle`
2. `"vehicle"`
3. `vehicle.substring(0, 5)`
4. `vehicle.length()`
5. `vehicle + vehicle.length()`

Question # : 9.

At a certain university, if a student drops a class on or before September 20 (that is, `9/20`), then no record of the course appears on the student's transcript. A grade of `W` appears if the withdrawal takes place on or before October 15 (that is, `10/15`), and a grade of `F` or `I` is recorded otherwise. Assume that the month and the day that a student withdraws are stored in two separate `int` variables called `month` and `day`. Select the condition that should be inserted into the following `if` statement

```if ( /* What goes here? */ )
System.out.println("Assign a grade of W."); ```
so that the message will be printed if the student is to receive a grade of `W`.
1. `(month == 9 || month == 10) && (15 < day && day <= 20)`
2. `(month >= 9) && (day > 20 || day <= 15)`
3. `(month == 9 && day > 20) || (month == 10 && day <= 15)`
4. `(month == 9 && day > 20) && (month == 10 && day <= 15)`
5. `(month >= 9 && month <= 10) && (day <= 15 || day > 20)`

Question # : 10.

Which of the following expressions is NOT logically equivalent to `(x <= y)`?

1. `(y >= x)`
2. `(x < y) && (x == y)`
3. `(x == y) || (x < y)`
4. `(x - y) <= 0`
5. `(x <= y) && (x == x)`
Question # : 11. Consider the following code fragment:
```int t = 5;
if (t >= 5)
System.out.print("Wicked ");
else if (t >= 3)
System.out.print("Witch ");
if (t <= 7)
System.out.print("of the West");
```
What gets printed when you run it?
1. `Wicked`
2. `Wicked Witch`
3. `Wicked of the West`
4. `Witch of the Wicked West`
5. `Witch`

Question # : 12. Are these two code fragments equivalent?
 ```z = z + 1; if (x <= y) z = z + 1; ``` ```if (x > y) z = z + 1; else if (x <= y) z = z + 2;```
1. yes
2. no

Question # : 13. What does the following program fragment print? Read carefully.
```String s = "dogfood", t = "dogfood";
if (!s.equals(t))
System.out.print("You could've fooled me! ");
else
System.out.print("Figures. ");
System.out.print("I'm outta here!");
```
1. `You could've fooled me! Figures.`
2. `Figures. I'm outta here!`
3. `You could've fooled me!`
4. `I'm outta here!`
5. `You could've fooled me! I'm outta here!`

Question # : 14. Assume that `x` is an integer variable. Simplify the following boolean expression:
`(x > 3) || (x < 5)`
1. `x == 4`
2. `true`
3. `x > 3`
4. `x < 5`
5. none of the above

Question # : 15. Assume that `x` and `y` are integer variables. Consider the following nested `if` statement.
```if (x > 3)
{
if (x <= 5)
y = 1;
else if (x != 6)
y = 2;
else
y = 3;
} else
y = 4;```
If `y` has the value 2 after executing the above program fragment, then what do you know about `x`?
1. `(x == 4) || (x == 6)`
2. `x >= 7`
3. `(x != 6) && (x < 5)`
4. `(x <= 3)`
5. none of the above

Question # : 16. Assume that `x` is an integer variable. Simplify the following boolean expression:
`(x > 3) && (x < 5)`
1. `x == 4`
2. `true`
3. `x > 3`
4. `x < 5`
5. none of the above

Question # : 17. Assume that `x` is an integer variable. Simplify the following boolean expression.
`!(((x > 0) || (x == 0)) && (x < 10))`
Warning: Don't overlook the logical negation (bang!) at the front.
1. `(x < 0) || (x >= 10)`
2. `(x < 0) && (x >= 10)`
3. `x >= 10`
4. `(x != 0) || (x >= 10)`
5. none of the above

Question # : 18. Given the following declaration:
`String word = "drow";`
What does `word.charAt(3)` evaluate to?
1. `"r"`
2. `'o'`
3. `'r'`
4. `"o"`
5. none of the above

Question # : 19. Which of the following expressions does not evaluate to `5`?
1. `'5' - '0'`
2. `(4 * 5 + 3) / 4`
3. `("01" + "0" + "12").length()`
4. `8 - 1 - 2`
5. `'5'`

Question # : 20. Consider the following class definition. How many instance members does it define for objects of class `One`?
```class One {
public static void main(String[] args) {
int a = 0, b = 1;
System.out.println(" a + b = " + (a + b));
}
}```
1. One cannot create objects of this class, because there is no constructor defined.
2. No instance members, the objects will be empty.
3. One instance member.
4. Two variables and a method.

Question # : 21. Is this an example of an infinite loop?
```int x = 10;
while (x == 0) {
x = x + 1;
System.out.println(x);
x = x % x;
} ```
Don't rush, think carefully.
1. Yes
2. No
Question # : 22. Is this an example of an infinite loop?
```int x = 10;
do {
x = x + 1;
System.out.println(x);
x = x % x;
} while (x == 0); ```
Don't rush, think carefully.
1. Yes
2. No

Question # : 23. What is wrong with this class definition?
```class Avg {
public static void main(String[] args) {
double a = 1;
double b = a + 1;
double c = b + 1;
System.out.println(findAvg(a, b, c));
}
double findAvg(double a, double b, double c) {
return (a + b + c) / 3.0;
}
}```

How can you fix it so that it works when you invoke `main()`?

1. turn `findavg` into a `public` method
2. turn `findavg` into a `static` method
3. turn `findavg` into a `void` method
4. none of the above

Question # : 24. Consider the following code:
```class One {
static int var1 = 1;
static int var2;
public static void main(String[] args) {
int var3 = var1 + var2;
System.out.println(var1 + var2 + var3);
}
}```
What is the result of trying to compile and run it?
1. `0`
2. `1`
3. The code does not compile because `var3` is not initialized correctly.
4. The code does not compile because `var2` is not initialized at all.
5. none of the above

Question # : 25.

Can you initialize an array of three `boolean` values to all `true` the way presented below?

```boolean[] b;
b = { true, true, true };```
1. Yes.
2. No.

Question # : 26. By default integer values are of type `int`, and floating point values are of type `double`. However, you can force an integer value to be a `long` by placing an `L` after it. You can also force a floating-point value to be a `float` by placing an `F` after it.

Knowing this, consider the following program:

```class Example {
public static void main(String[] args) {
Example e = new Example();
e.test(5);
}
int test(int i) {
System.out.println("long");
return 1;
}
void test(long i) {
System.out.println("int");
}
}```
What is the result of attempting to compile and run it?
1. The program does not compile because the compiler cannot distinguish between the two `test()` methods provided.
2. The program compiles and runs and "`long`" appears in the standard output.
3. The program compiles and runs and "`int`" appears in the standard output.
4. The program compiles and runs and "`long`" appears in the standard output followed by a `1`.

Question # : 27. Which of these defines a valid `main()` method (that will run when you invoke `java` on the class that contains the method, from the command line)?
1. `public static void main(String args) { }`
2. `public static void main(String[]) { }`
3. `public static void main(string[] args) { }`
4. `public static void main(args) { }`
5. none of the above

Question # : 28. How can you access the word `"jini"` from the following command-line invocation:
`java java jini java jini java`
1. `args`
2. `args`
3. `args`
4. `args`
5. `args`

Question # : 29. What will appear in the standard output when you run the `Tester` class below?
```class Tester {

int var;

Tester (double var) { this.var = (int)var;            }
Tester (int var)    { this("hello");                  }
Tester (String s)   { this(); System.out.println(s);  }
Tester ()           { System.out.println("good-bye"); }

public static void main(String[] args) {
Tester t = new Tester(Math.sqrt(2));
}

}```
1. nothing at all
2. `hello`
3. `1.4142135623730951`
4. `hello` followed by `good-bye`
5. `good-bye` followed by `hello`

Question # : 30. What is printed by the following program?
```public class A {
public static void main(String[] args) {
int x = 1;
System.out.print(fun(x) + " ");
System.out.print(x + " ");
}
public static int fun(int x) {
x = x - 1;
return x;
}
}```
1. `1 0`
2. `1 1`
3. `0 1`
4. `0 0`
5. none of the above

Question # : 31. What is printed by the following program?
```public class A {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5};
System.out.print(fun(x) + " ");
System.out.print(x + " ");
}
public static int fun(int[] x) {
x = x - 1;
return x;
}
}```
1. `3 3 `
2. `3 2 `
3. `2 3 `
4. `2 2 `
5. none of the above

Question # : 32. Assume that `x` is an integer variable that appears in the following `boolean` expression:
`(x >= 14) && (x <= 12)`
Which of the following is a simplification of the expression?

1. `x == 13`
2. `(x < 14) || (x > 12)`
3. `true`
4. `false`
5. none of the above

Question # : 33. Assume that `x` is an integer variable that appears in the following expression:
`(x >= 12) || (x <= 14)`
Which of the following is a simplification of the expression?

1. `x != 13`
2. `(x < 14) && (x > 12)`
3. `true`
4. `false`
5. none of the above

Question # : 34. What is printed by the following program?
```public class A {
public static void main(String[] args) {
System.out.print(fun(5) + " ");
}
public static int fun(int x) {
System.out.print(x + " ");
return x + 1;
}
}```
1. `5 `
2. `6 `
3. `5 5 `
4. `5 6 `
5. none of the above

Question # : 35. What is printed by the following program fragment?
```int x = 1, y = 1;
y += x;
x += y;
System.out.println(x + " " + y);```
1. `1 2`
2. `2 1`
3. `2 3`
4. `3 2`
5. none of the above

Question # : 36. Given the following definition
```public static String mix(String s, String t) {
String ans = "";
int slen = s.length(), tlen = t.length();
for (int i = 0; i < slen && i < tlen; i++) {
ans = ans + t.charAt(i) + s.charAt(i);
}
return ans;
}```
what does
`mix ("123", "45678")`
evaluate to?
1. `12345678`
2. `142536`
3. `415263`
4. `123456`
5. none of the above

Question # : 37. What is printed by the following program:
```public class A {
public static void main(String[] args) {
int yo = 12;
System.out.println(yo(yo));
}
public static boolean yo (int yo) {
if (yo == 12)
return true;
return false;
}
}```
1. `true`
2. `false`
3. this code will not compile
4. this code compiles but produces a run-time error
5. none of the above

Question # : 38. What is printed by the following program:
```public class A {
public static void main(String[] args) {
System.out.println(fun(1, fun(fun(2, fun(3, 4)), 5)));
}
public static int fun(int a, int b) {
return a + b;
}
} ```
1. `5`
2. `12`
3. `15`
4. `17`
5. `25`

Question # : 39. Is this an infinite loop?
```for (int i = 0; i < 10; i--)
System.out.println(i);```
1. yes
2. no

Question # : 40. Is this an infinite loop?
```for (int i = 1; i != 10; i = i + 3)
System.out.println(i);
```
1. yes
2. no

Question # : 41. Given the following declaration, what does `a[a[a[a]]]` evaluate to?
`int[] a = {1, 2, 3, 4, 5}; `
1. `1`
2. `2`
3. `3`
4. `4`
5. `5`

Question # : 42. Given the following declaration, what does `a[1 + a[1 + a]]` evaluate to?
`int[] a = {1, 2, 3, 4, 5}; `
1. `1`
2. `2`
3. `3`
4. `4`
5. `5`

Question # : 43. Assume the following two arrays:
```String[] name = {"One", "Two", "Three", "Four", "Five"};
boolean[] mark = {false, true, true, false, true}; ```
And a method `fun` with the following definition:
```public static String fun(String[] a, boolean[] b) {
for (int i = 0; i < b.length; i++) {
if (b[i]) {
return a[i];
}
}
return "One";
}```
What does `fun(name, mark)` evaluate to?
1. `One`
2. `Two`
3. `Three`
4. `Four`
5. none of the above

Question # : 44. Now assume that the arrays are like this:
```String[] name = {"One", "Two", "Three", "Four", "Five"};
boolean[] mark = {false, false, false, false, false}; ```
and the method still has the same definition:
```public static String fun(String[] a, boolean[] b) {
for (int i = 0; i < b.length; i++) {
if (b[i]) {
return a[i];
}
}
return "One";
}```
What does `fun(name, mark)` evaluate to?
1. `One`
2. `Two`
3. `Three`
4. `Four`
5. none of the above

Question # :45. Exactly how many question marks will appear on the screen when the following program fragment is executed?
```for (int x = 0; x < 3; x++) {
for (int y = -3; y < 0; y++) {
System.out.print("?");
}
} ```
1. `4`
2. `6`
3. `9`
4. `16`
5. none of the above

Question # : 46. What does the following program fragment print?
```int[] a = new int;
a = 10;
for (int i = 1; i < a.length; i++)
a[i] = a[i-1]-1;
int value = a;
for (int i = 0; i < a.length; i++)
if (value > a[i])
value = a[i];
System.out.println(value);
```
1. `3`
2. `0`
3. `4`
4. `1`
5. none of the above

Question # : 47. What does the following program print?
```int[][] a = { {1, 2, 3, 4},
{2, 3, 4, 1},
{3, 4, 1, 2},
{4, 1, 2, 3}};
int sum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] % 2 == 0)
sum = sum + a[i][j];
}
}
System.out.println(sum);
```
1. `6`
2. `12`
3. `20`
4. `24`
5. none of the above

Question # : 48. What does the following program print?
```int[][] a = { {1, 2, 3, 4},
{2, 3, 4},
{3, 4},
{4}};
int sum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] % 2 == 0)
sum = sum + a[i][j];
}
}
System.out.println(sum);
```
1. `6`
2. `12`
3. `20`
4. `24`
5. none of the above

Question # : 49. What does the following program print?
```int[][] a = { {1, 2, 3, 4},
{2, 3, 4},
{3, 4},
{4}};
int sum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] % 2 == 0)
sum = sum + 1;
}
}
System.out.println(sum);
```
1. `6`
2. `12`
3. `20`
4. `24`
5. none of the above

3. August 11, 2000 in Lindley Hall 102 SWAIN EAST 140 (1:30pm-2:30pm)