Java Garbage Collector.

CSCI A201/A597 and I210

Final Examination

Spring 2001


Directions:

Good luck and do well!


For the following 5 (five) exercises consider the following method definitions:

public static int f(int x) {
  return h(h(x)); 
}
public static int g(int x) { return k(k(x)); }
public static int h(int x) { return 1 + k(x); }
public static int k(int x) { return x - 1; }
1. What does g(2) evaluate to?
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

Note: To save space I wrote other where I would have put "None of the above."


2. What does g(h(2)) evaluate to?
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

3. What does k(g(2) + h(2)) evaluate to?
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

4. What does f(0) + f(1) + f(2) evaluate to?
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

5. What does f(2) evaluate to?
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

6. "0" is an example of what kind of constant?
  1.   int
  2.   char
  3.   boolean
  4.   String
  5.   other

7. Assume that x is integer variable. Simplify the following expression:
!(((x - 1) >= 4) && ((x - 1) <= 4))
  1.   true
  2.   false
  3.   x != 5
  4.   x == 5
  5.   other

Warning: Don't overlook the logical negation (bang!) at the front.


8. Consider the following two program fragments. Assume that x is an int variable.
       // fragment 1         |         // fragment 2
       if (x == 5)           |         if (x == 5)
          x = x + 1;         |            x = x + 1;
       else                  |         if (x != 5)
          x = 8;             |            x = 8;
Which of the following statements is false?

  1. The two fragments are not logically equivalent.
  2. x always has the value of 8 after executing fragment 2.
  3. x is always divisible by 2 (even) after executing fragment 1.
  4. x has either a value of 5 or a value of 8 after executing fragment 2.
  5. The first fragment could be simplified as: x = (x != 5) ? 8 : 6;

Reminder: Last option above uses the selection operator (page 187 in your text).


9. You compile and run this program. What is the output (or outcome)?
class Wan {
    static int value; 
    Wan() { value += 1; }
    public static void main(String[] args) {
	Wan a = new Wan(); 
	a = new Wan(); 
	a = new Wan(); 
	System.out.println(Wan.value); 
    } 
} 
  1.    0
  2.    1
  3.    2
  4.    3
  5.   other

10. Assume that x and y are integer variables and 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 a value of 3 after executing the above program fragment, then what do you know about x?
  1. x != 6
  2. x == 6
  3. x == 4
  4. x != 4
  5. (x > 3) && (x <= 5)
  1.    1
  2.    2
  3.    3
  4.    4
  5.    5

11. Which of the following expressions does not print a 4 (four)?
  1. System.out.println('M' - 'J' + 1);
  2. System.out.println((4 * 5 + 2) / 7 + 1);
  3. System.out.println("012".length() + 1);
  4. System.out.println(8 - 3 - 2 + 1);
  5. System.out.println('3' + 1);
  1.    1
  2.    2
  3.    3
  4.    4
  5.    5
Note: There are no typos!

12. After the following statement is executed,
int x = (int)(Math.random() * (30 - 10) + 20);
  1.   22
  2.   37
  3.   27
  4.   42
  5.   32
which of these numbers could NOT possibly be contained in x?

13. Given the following array declaration what does a[a[3][2]][1] evaluate to?
int[][] a = { {2, 4, 3}, 
              {1, 3, 2}, 
              {1, 2, 3}, 
              {3, 4, 1}
            }; 
  1.    1
  2.    2
  3.    3
  4.    4
  5.    other

14. If fun is defined as below:
public static int[] fun(int size, int valu) {
  int[] a = new int[size];
  for (int i = 0; i < size; ++i) a[i] = valu - i;
  return a;
}
What will fun(3, 3)[2] evaluate to? (Yes, it compiles and runs fine).
  1.    0
  2.    1
  3.    2
  4.    3
  5.    other

15. What is printed if you compile and run the following program?
class Nine { 
  public static void main(String[] args) {
    int[] x = {1, 2, 3};
    triple(x);
    System.out.println(x[0] + x[1] + x[2]);
  }
  public static void triple(int[] y) {
    y[2] = 3 * y[2];
  }
} 
  1.   6
  2.   8
  3.   10
  4.   12
  5.   other

16. What is printed if you compile and run the following program?
class Nine { 
  public static void main(String[] args) {
    int[] x = {1, 2, 3};
    triple(x[2]);
    System.out.println(x[0] + x[1] + x[2]);
  }
  public static void triple(int m) {
    m = 3 * m;
  }
} 
  1.   6
  2.   8
  3.   10
  4.   12
  5.   other

17. You compile and run this program. What is the output?

class A {
    public static void main(String[] args) {
        A a = new A(); 
        B b = new B(); 
        A c = new B(); 
        System.out.println(a.fun() + 
                           b.fun() + 
                           c.fun()); 
    } 
    int fun() { return 0; }
} 
class B extends A { int fun() { return 1; } } 
  1.   0
  2.   1
  3.   2
  4.   3
  5.   other

18. What letter should go in both blank spaces for the following program to compile?
class A extends D { } 
class D extends B { } 
class C extends D { 
  public static void main(String[] args) {
    ____ seq[] = new ____[10]; 
    seq[6] = new A(); 
    seq[3] = new B(); 
  } 
} 
class B { } 
class E extends Object { }
  1.   A
  2.   B
  3.   C
  4.   D
  5.   E

19. Assume the program above is in a file E.java and you want to compile and run it.
C:\> javac E.java
C:\> java ____
  1.   A
  2.   B
  3.   C
  4.   D
  5.   E
Fill in the blank with the name of the class that you need to run.

20. Assume you compile the following program:
public class A {
  static int index; 
  public static void main(String[] args) {
    int[] x = {6, 7, 8, 9, 10};
    index = Integer.parseInt(args[0]); 
    System.out.print(fun(x) + " "); 
    System.out.print(x[index] + " "); 
  } 
  public static int fun(int[] x) {
    x[index] = x[index] - 1; 
    return x[index]; 
  } 
}
  1.   7 7
  2.   7 8
  3.   8 7
  4.   8 8
  5.   other
What gets printed when you run it as follows:
java A 3

21. Assume that
  • expr1 evaluates to true,
  • expr2 evaluates to false, and
  • expr3 evaluates to true.
Which of the following expressions evaluates to false?
  1.    expr2 || expr1 && expr3
  2.    expr2 || (expr1 && expr3)
  3.   (expr2 || expr1) && expr3
  4.   !expr2 && expr1 && expr3
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other

22. 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(5, nuf(fun(4, 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; 
  }
}
  1.   -3
  2.   -2
  3.   -1
  4.   0
  5.   other
There are no typos, so please don't rush!

23. You compile and run this contrived program. What is the output?
class Account {
  public static void main(String[] args) {
    Account e = new Account(); 
    Account f = new Account(10); 
    System.out.println(e.getBalance() + f.getBalance()); 
  } 
  Account() { this(20); } 
  Account (int n) { balance = n; } 
  int balance; 
  int getBalance() { return balance + 30; }
}
  1.   10
  2.   30
  3.   60
  4.   90
  5.   other

24. Consider the following code fragment:
class One {
  public static void main(String[] args) {
    int t = Integer.parseInt(args[1]); 
    if (t >= 2)
      System.out.print("IN");
    if (t <= 3)
      System.out.print("DIA");
    if (t >= 1)
      System.out.print("NA");
  }
} 
  1.   INNA
  2.   INDIA
  3.   INDIANA
  4.   DIANA
  5.   other
What gets printed when you compile and run it as follows:
java One 1 2 3
Work carefully: there are no typos.

25. Exactly how many question marks will appear on the screen when the following program fragment is executed?
for (int i = 0; i < 10; i += 3) 
  for (int j = 0; j < 10; j += 3) 
    System.out.print("?"); 
System.out.println(); 
  1.   4
  2.   16
  3.   25
  4.   36
  5.   other

26. What does the following program's output most closely resemble?
public class Nine {
  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
           for (int j = 0; j < 10; j++) {
               if (i == 9 || j == 0 || 9 == j) {
                 System.out.print(" " + j);
               } else {
                 System.out.print("  ");
               }
           }
           System.out.println();
         }
     }
}
  1.   C
  2.   Z
  3.   U
  4.   N
  5.   other

27. What does System.out.println(mix("one", "two")) print if mix is defined as below?


public static String mix(String one, String two) {
  String ans = ""; 
  int olen = one.length(), tlen = two.length();
  for (int i = 0; i < olen && i < tlen; i++) {
    ans = one.charAt(i) + ans + two.charAt(i); 
  }  
  return ans; 
} 
  1.   ontowe
  2.   ewotno
  3.   owtone
  4.   enotwo
  5.   other


28. What does System.out.println(mix("one", "two")) print if mix is defined as below?


public static String mix(String one, String two) {
  String ans = ""; 
  int olen = one.length(), tlen = two.length();
  for (int i = 0; i < olen && i < tlen; i++) {
    ans = two.charAt(i) + ans + one.charAt(i); 
  }  
  return ans; 
} 
  1.   ontowe
  2.   ewotno
  3.   owtone
  4.   enotwo
  5.   other


29. What does System.out.println(mix("one", "two")) print if mix is defined as below?


public static String mix(String one, String two) {
  String ans = ""; 
  int olen = one.length(), tlen = two.length();
  for (int i = 0; i < olen && i < tlen; i++) {
    if (i % 2 == 1) { 
      ans = one.charAt(i) + ans + two.charAt(i); 
    } else {
      ans = two.charAt(i) + ans + one.charAt(i); 
    } 
  }  
  return ans; 
} 
  1.   ontowe
  2.   ewotno
  3.   owtone
  4.   enotwo
  5.   other
There are no typos so, please, think carefully.


30. What does System.out.println(mix("one", "two")) print if mix is defined as below?


public static String mix(String one, String two) {
  String ans = ""; 
  int olen = one.length(), tlen = two.length();
  for (int i = 0; i < olen && i < tlen; i++) {
    if (i % 2 == 0) { 
      ans = one.charAt(i) + ans + two.charAt(i); 
    } else {
      ans = two.charAt(i) + ans + one.charAt(i); 
    } 
  }  
  return ans; 
} 
  1.   ontowe
  2.   ewotno
  3.   owtone
  4.   enotwo
  5.   other


31. Assume the following declaration:
int[][] a = { { 1, 2 }, { 3, 4, 5}, { 6 } };
What does the following code print?
int sum = 0;
for (int i = 0; i < a.length; i++)
  sum += a[i].length; 
System.out.println(sum); 
  1.   3
  2.   6
  3.   90
  4.   180
  5.   other

32. Assume the following declaration:
int[][] a = { { 1, 2 }, { 3, 4, 1 }, { 2, 3, 4 }, { 1 } };
What does the following code print?
System.out.println(a[2][2]); 
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other

33. You compile and run this program. What is the output that it produces?
class One {
  int a = 8;
  int b = 1;
  void fun() {
    b *= 2;
    a /= 2;
  }
  void report() {     
    System.out.print(a + " " + b + " "); 
  }
  public static void main(String[] args) {
    One alpha = new One();
    alpha.fun();
    alpha.fun();
    alpha.fun();
    alpha.report(); 
  }
}
  1.   1 8
  2.   4 2
  3.   8 1
  4.   4 8
  5.   other

34. You compile and run this program. What is the output that you obtain?
class One {
  int a = 8;
  int b = 1;
  void fun() {
    b *= 2;
    a /= 2;
  }
  void report() {     
    System.out.print(a + " " + b + " "); 
  }
  public static void main(String[] args) {
    One alpha = new One();
    One beta = new One();
    alpha.fun();
    beta.fun();
    alpha.fun();
    beta.report(); 
  }
}
  1.   1 8
  2.   4 2
  3.   8 1
  4.   4 8
  5.   other

35. You compile and run this program. What is the output that you obtain?
class One {
  int a = 8;
  static int b = 1;
  void fun() {
    b *= 2;
    a /= 2;
  }
  void report() {     
    System.out.print(a + " " + b + " "); 
  }
  public static void main(String[] args) {
    One alpha = new One();
    One beta = new One();
    alpha.fun();
    beta.fun();
    alpha.fun();
    beta.report(); 
  }
}
  1.   1 8
  2.   4 2
  3.   8 1
  4.   4 8
  5.   other

36. What is the result of attempting to compile and run the following code?
class Blow {
    public static void main(String[] args) {
	int[] x = { 9, 1, 3, 7, 5 }; 
	fun(x); 
	for (int i = 0; i < x.length; i++) 
	    System.out.print(x[i]); 
    } 
    public static void fun(int[] a) {
	for (int i = 0; i < a.length - 1; i++) 
	    if (a[i] < a[i + 1]) {
		int temp = a[i + 1]; 
		a[i + 1] = a[i]; 
		a[i] = temp; 
	    } 
    } 
} 
  1.   13759
  2.   93751
  3.   13579
  4.   97531
  5.   other

37. 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(1)); 
  } 
  int fun() { return 1 + fun(1); } 
  int fun(int n) { return 1 + n; } 
} 
  1.   1
  2.   3
  3.   5
  4.   7
  5.   other

38. Assuming that the following program fragment is syntactically correct...
int[] a = new int[10];
   :
   :
int n = f38(a, a[0]);
   :
   :
...select the correct header for the function f38.
  1. public static int f38(int v, int[] a)
  2. public static int f38(int[] a, int v)
  3. public static int[] f38(int v, int[] a)
  4. public static int[] f38(int[] a, int v)
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other

39. Assuming that the following program fragment is syntactically correct...
int[] a = new int[10];
   :
   :
int[] n = f39(a, a[0]);
   :
   :
...select the correct header for the function f39.
  1. public static int f39(int v, int[] a)
  2. public static int f39(int[] a, int v)
  3. public static int[] f39(int v, int[] a)
  4. public static int[] f39(int[] a, int v)
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other

40. Assuming that the following program fragment is syntactically correct...
int[][] a = new int[10];
   :
   :
int n = f40(0, a[0]);
   :
   :
...select the correct header for the function f40.
  1. public static int f40(int v, int[] a)
  2. public static int f40(int[] a, int v)
  3. public static int[] f40(int v, int[] a)
  4. public static int[] f40(int[] v, int a)
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other

41. Assume the following declaration:
int[][] a = { { 0, 1, 2 }, { 3, 4, 5}, { 6 } };
What does the following code print?
int count = 6; 
for (int i = 0; i < a.length; i++) 
  for (int j = 0; j < a[i].length; j++) 
    if (a[i][j] % 2 == 0) 
      count = 1 - count; 
System.out.println(count); 
  1.   7
  2.   6
  3.   5
  4.   4
  5.   other

42. Which of the following numbers is closest to the output of this line if embedded in a complete program, that is then compiled and run?
int a, b;
a = 10; b = 4;  
System.out.println(b * a / b + a % b);
  1.   10
  2.   20
  3.   30
  4.   40
  5.   50

43. What is the output produced by the following code when embedded in a complete program?
if (false) System.out.print(0); 
else System.out.print(1); 
boolean x = (1 < 2) && (! (4 < 3)); 
if (x) System.out.print(2);   
else System.out.print(3); 
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other

44. What is the output produced by the following code when embedded in a complete program?
boolean x; 
if (x = (false && (false || true))) System.out.print(0); 
else System.out.print(1); 
if (! x) System.out.print(2);   
else System.out.print(3);  
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other
(Think carefully. Yes, it compiles).
45. What is the output produced by the following code when embedded in a complete program?
boolean x; 
if (x = (false && false || true)) System.out.print(0); 
else System.out.print(1); 
if (! x) System.out.print(2);   
else System.out.print(3);  
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other

Short Note on Java Garbage Collectors: The character depicted on the front page is Dilbert's 'sanitation engineer'. He is a mysterious character that has inexplicable knowledge of all subjects from science to philosophy. In Dilbert comic strips he shows up occasionally to solve impossibly complex problems for Dilbert or Dogbert. I hope he will bring you luck on this final exam!
Tue May 1 17:00:00 EST 2001 (A201/A597/I210, Rawles Hall 100)