CSCI A201/A597

Final Exam

Right answers in blue.

Spring 2000


Directions:

Good luck and do well!

Write your name or username here:______________________


1. 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 0
1 1
0 1
0 0
none of the above
2. 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[2] + " "); 
  } 
  public static int fun(int[] x) {
    x[2] = x[2] - 1; 
    return x[2]; 
  } 
}
3 3
3 2
2 3
2 2
none of the above
3. 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?

x == 13
(x < 14) || (x > 12)
true
false
none of the above
4. 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?

x != 13
(x < 14) && (x > 12)
true
false
none of the above
5. After the following statement is executed,
int x = (int)(Math.random() * (20 - 6) + 14);
which of the following numbers could NOT possibly be contained in x?
12
16
18
20
22
6. 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;
  } 
}
5
6
5 5
5 6
none of the above
7. What is printed by the following program fragment?
int x = 1, y = 1;
y += x; 
x += y;
System.out.println(x + " " + y);
1 2
2 1
2 3
3 2
none of the above
8. Assume that x is an integer variable. Simplify the following boolean expression:
!(((x > 10) || (x == 10)) && (x > 0))
Note: do not overlook the logical negation at the front!
true
false
x >= 10
x < 10
none of the above
9. 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")
evalue to?
12345678
142536
415263
123456
none of the above
10. 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;  
  } 
}
true
false
this code will not compile
this code compiles but produces a run-time error
none of the above
11. 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;
  } 
} 
5
12
15
17
25
12. Is this an infinite loop?
for (int i = 0; i < 10; i--) 
  System.out.println(i);
yes
no
13. Is this an infinite loop?
for (int i = 1; i != 10; i = i + 3) 
  System.out.println(i);
yes
no
14. Is this an infinite loop?
int x = 0, y = -1;
while (x + y < 0) {
  int temp = x; 
  x = y; 
  y = temp; 
  System.out.println("Hello!");
}
yes
no
15. What does the following program fragment print?
boolean done = false;
int counter = 4;
while (! done) {
  counter = counter - 1;
  done = (counter == 0); 
  System.out.print(counter); 
}
4321
3210
432
321
none of the above
16. The header for a function named ahoy looks like this:
public static String ahoy(String s, char c)
Which of the following statements does NOT contain a valid call to the function ahoy?
ahoy("aho", 'y');
ahoy(ahoy("aho", 'y'), 'y');
String ahoy = aho("aho", 'y');
int len = ahoy("aho", 'y').length();
ahoy("aho", "y");

There was a typo in the third alternative (a missing "y"). For this reason either one of the two marked answers will be considered correct. Originally I had intended the last alternative to be the one that did not contain a "valid" call to the function, because a string of length one is not a char. Sorry for the typo, it was accidental.

17. Given the following declaration, what does a[a[a[a[0]]]] evaluate to?
int[] a = {1, 2, 3, 4, 5}; 
1
2
3
4
5
18. Given the following declaration, what does a[1 + a[1 + a[0]]] evaluate to?
int[] a = {1, 2, 3, 4, 5}; 
1
2
3
4
5
19. 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?
One
Two
Three
Four
none of the above
20. 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?
One
Two
Three
Four
none of the above
21. 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("?"); 
  } 
} 
4
6
9
16
none of the above
22. What does the following program fragment print?
int[] a = new int[5]; 
a[0] = 10;
for (int i = 1; i < a.length; i++) 
  a[i] = a[i-1]-1;
int value = a[0];
for (int i = 0; i < a.length; i++) 
  if (value > a[i]) 
    value = a[i]; 
System.out.println(value); 
3
0
4
1
none of the above

The value printed is 6, the smallest in the array (initialized at 10, 9, 8, 7, 6).

23. 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); 
6
12
20
24
none of the above
24. 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); 
6
12
20
24
none of the above
25. 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); 
6
12
20
24
none of the above

Tue May 2 17:00:00 EST 2000 (A201, Rawles Hall 100)