You can do it!

CSCI A201/A597/I210

Final Examination

Summer II 2003
(Good luck and do well!)


Directions:

Good luck and do well!


1. Consider the following condition.
(x % 4 == 0 || (x % 100 == 0 && x % 400 == 0))
Which of the following represents a simplification of it?

  1.   ((x % 100) == 0)
  2.   ((x % 400) == 0)
  3.   ((x % 100) != 0)
  4.   ((x % 400) != 0)
  5.   none of the above


2. Consider the following condition.
! (a == ! false)
Which of the following represents a simplification of it.
  1.   a
  2.   !a
  3.   a && ! a
  4.   a || ! a
  5.   none of the above


3. Consider the following program fragment.
int x = 12, y = 0; if (x < 10) if (x > 5) y = 1; else y = 2;
What value does y have at the end?
  1.   0
  2.   1
  3.   2
  4.   cannot be determined with the information given
  5.   this program fragment would not compile

4. Consider the following program fragment.
int x = 8, y = 0; if (x < 6) { if (x > 5) y = 1; } else y = 2;
What value does y have at the end?
  1.   0
  2.   1
  3.   2
  4.   cannot be determined with the information given
  5.   this program fragment would not compile

5. Consider the following program fragment.
int x = 36, y = 8; if (x < 10) { if (x > 5) y = 1; else y = 2; }
What value does y have at the end?
  1.   0
  2.   1
  3.   2
  4.   cannot be determined with the information given
  5.   this program fragment would not compile

6. Consider the following program fragment.
int x = 8, y = 0; if (x < 10) ; if (x > 5) y = 1; else y = 2; 
What value does y have at the end?
  1.   0
  2.   1
  3.   2
  4.   cannot be determined with the information given
  5.   this program fragment would not compile

7. How many question marks will be printed by the following code fragment?
for (int i = -10; i <= 10; i = i + 3) ; 
  System.out.print("?"); 
  1.   1
  2.   2
  3.   3
  4.   6
  5.   other


8. How many question marks will be printed by the following code fragment?
for (int i =  7; i >=  0; i--) 
  System.out.print("?"); 
  1.   7
  2.   8
  3.   9
  4.   10
  5.   other


9. Consider the following fragment. What does it print?
int x = 3, y = 11; 
x = x + y;
x = x - y; 
y = x - y; 
System.out.println("(" + x + ", " + y + ")"); 
  1.   (3, 11)
  2.   (11, 3)
  3.   (14, 14)
  4.   (7, 7)
  5.   other


10. Consider the following fragment. What does it print?
int x = 11, y = 3; 
y = x - y;
x = y + x; 
y = y - x; 
System.out.println("(" + x + ", " + y + ")"); 
  1.   (3, 11)
  2.   (11, 3)
  3.   (14, 14)
  4.   (7, 7)
  5.   other

11. Consider the following fragment. What does it print?
int[] v = new int[10];
for (int i = 0; i < 10; i++) v[i] = 2 * i;
System.out.print(v[3] + v[1])
  1.   6
  2.   8
  3.   10
  4.   31
  5.   other

12. Consider the following fragment. What does it print?
int[] v = { 2, 4, 6, 8, 10, 12},
      m = { 1, 3, 5, 7,  9, 11};      
for (int i = 0, sum = 0; i < 10; i++) 
  sum -= (m[i] - v[i]); 
System.out.print(sum)
  1.   6
  2.   36
  3.   -6
  4.   -84
  5.   other

13. Consider the following program. What does it print?
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); 
  1.   0
  2.   3
  3.   -3
  4.   -1
  5.   other

14. Consider the following program fragment.
public class One {
  public static void main(String[] args) {
    int size = 10, nuf, fun;
    for (nuf = 0; nuf < size; nuf++) { 
      for (fun = 0; fun < size; fun++) 
        if ( fun == 0 || 
             fun == size - 1 || 
             __________________ )
          System.out.print(" *"); 
        else 
          System.out.print("  "); 
      System.out.println(); 
    }
  } 
}
What's need for the output to resemble an N ?






  1.   fun == size / 2
  2.   nuf == size / 2
  3.   fun + nuf == size - 1
  4.   fun - nuf == size - 1
  5.   other


15. Consider the following command line invocation:
java One Two Three Four Five
Assume your program contains this line inside
System.out.println(args[args.length - 1]);
What does this line print?
  1.   One
  2.   Two
  3.   Three
  4.   Five
  5.   other


16. Consider the following code fragment, what does it print?
String plum = "nectarine"; 
System.out.println(plum.substring("plum".length(), "peach".length()); 
  1.   t
  2.   a
  3.   ta
  4.   ar
  5.   other


17. Consider the following code fragment, what does it print?
String plum = "nectarine";  
System.out.println((char)('A' + plum.length())); 
  1.   J
  2.   O
  3.   K
  4.   E
  5.   other

18. Consider the following code fragment, what does it print?
String plum = "nectarine";  
System.out.println((char)('A' + "plum".length())); 
  1.   O
  2.   T
  3.   H
  4.   E
  5.   R

19. Does this source code compile?
class Momerath { } 
class Tove extends Momerath { }
class Kroger {
  public static void main(String[] args) {
    Tove a = new Momerath(); 
  }
}
  1.   yes
  2.   no
(Please think CAREFULLY).


20. Does this source code compile?
class Tove { } 
class Momerath extends Tove { }
class Kroger {
  public static void main(String[] args) {
    Tove a = new Momerath(); 
  }
}
  1.   yes
  2.   no
(Please think CAREFULLY.)


21. Does this source code compile?
class Wabe { } 
class Tove extends Wabe { }
class Momerath extends Tove { } 
class Kroger {
  public static void main(String[] args) {
    Wabe a = new Momerath(); 
  }
}
  1.   yes
  2.   no
(Please think CAREFULLY).


22. Does this source code compile?
class Wabe { } 
class Tove extends Wabe { }
class Momerath extends Tove { } 
class Kroger {
  public static void main(String[] args) {
    Tove a = new Wabe(); 
  }
}
  1.   yes
  2.   no
(Please think CAREFULLY).


23. You compile and run this code, what is the output?
class One {
  int red; 
  One() { this(10); }
  One (int blue) { red += blue; }
  int report() { System.out.println(red); }
  public static void main(String[] args) {
    One x = new One(20); 
    x = new One(30); 
    x = new One(); 
    x.report(); 
  }
} 
  1.   10
  2.   20
  3.   30
  4.   60
  5.   other


24. If you compile and run this code, what is the output?
class One {
  public static void main(String[] args) {
    int[][] a = { {1, 2, 3}, 
                  {4, 5, 6}, 
                  {7, 8, 9}, 
                  {6, 2, 7}
                }; 
    System.out.println(a[a[1][0] - 1][0]); 
  }
} 
  1.   2
  2.   4
  3.   6
  4.   8
  5.   other

25. If you compile and run this code, what is the output?
class One {
  public static void main(String[] args) {
    int[] x = {1, 2, 3};
    fun(x[0]); // focus here!!
    System.out.println(-x[0] + x[1] + x[2]);
  }
  public static void fun(int value) {
    value = value - 1;
  }
} 
  1.   0
  2.   2
  3.   4
  4.   6
  5.   other

26. If you compile and run this code, what is the output?
class One {
  public static void main(String[] args) {
    int[] x = {1, 2, 3};
    fun(x, 0); // focus here!!
    System.out.println(-x[0] + x[1] + x[2]);
  }
  public static void fun(int[] value, int index) {
    value[index] = value[index] - 1;
  }
} 
  1.   0
  2.   2
  3.   4
  4.   6
  5.   other

27. What gets printed when you compile and run the following program?
class One {
  public static void main(String[] args) {
    System.out.println(one(one(two(one(two(1, 2), 1), 2), two(1, 2)), 1)); 
  } 
  public static int one(int b, int a) {
    return a + b;
  } 
  public static int two(int b, int a) {
    return a - b; 
  }
} 
  1.   0
  2.   2
  3.   4
  4.   6
  5.   other
(Please think CAREFULLY).
28. Assuming that the following program fragment is syntactically correct...
int[][] a = new int[10][10];
  ...
a[0][1] = fun(a[2], a[0][0]); 
... select the correct header for method fun.
  1.   int[][] fun(int[] a, int b)
  2.   int fun(int[] a, int[][] b)
  3.   int[] fun(int a, int[] b)
  4.   int fun(int[][] a, int b)
  5.   none of the above


29. Assuming that the following program fragment is syntactically correct...
int[][] a = new int[10][10];
  ...
a[0] = fun(a[1][2], a); 
... select the correct header for method fun.
  1.   int[][] fun(int[] a, int b)
  2.   int fun(int[] a, int[][] b)
  3.   int[] fun(int a, int[] b)
  4.   int fun(int[][] a, int b)
  5.   none of the above

30. Assuming that the following program fragment is syntactically correct...
int[][] a = new int[10][10];
  ...
a = fun(a[1], a); 
... select the correct header for method fun.
  1.   int[][] fun(int a, int[][] b)
  2.   int fun(int[] a, int[][] b)
  3.   int[] fun(int a, int[] b)
  4.   int fun(int[][] a, int b)
  5.   none of the above

31. What is the result of attempting to compile and run this code?
class Alpha { 
  public static void main(String[] args) { 
    System.out.println("... will not run"); 
  } 
  public static void main() { 
    System.out.println("... won't compile"); 
  } 
} 
  1.   The program compiles and runs and prints ... won't compile
  2.   The program compiles but when you try to run it complains about main.
  3.   The program does not compile because main is not defined correctly.
  4.   The program compiles and runs and prints ... will not run
  5.   none of the above

32. What is the result of attempting to compile and run this code?
abstract class Alpha { 
  int value; 
  Alpha (int value) { 
    this.value = value; 
    System.out.println(value); 
  } 
  public static void main(String[] args) { 
    Beta f = new Beta(2003); 
  } 
} 

class Beta extends Alpha { 
  Beta (int value) { 
    super(value); 
  } 
}
  1.   The code will compile but nothing will appear in the standard output.
  2.   Code won't compile because there's no default no-args constructor for Alpha.
  3.   The code will not compile because Alpha is abstract.
  4.   The code will compile and run.
  5.   none of the above

33. What is the result of attempting to compile and run this code?
class Alpha { 
  public static void main(String[] args) { 
    Alpha f = new Beta();
    Alpha g = new Gamma();  
    System.out.println(f.test(g.test(3))); 
  } 
  int test(int i) { 
    return (i + 2); 
  } 
} 
class Beta extends Alpha {
  
}
class Gamma extends Beta { 
  int test(int i) { 
    return (i + 1); 
  } 
}

  1.   The code compiles and runs succesfully, and outputs 3.
  2.   The code compiles and runs succesfully, and outputs 4.
  3.   The code compiles and runs succesfully, and outputs 5.
  4.   The code does not compile.
  5.   none of the above

34. What is the result of attempting to compile and run this code?
class Alpha {
  public void main(String[] args) {
    Beta f = new Beta(3);
  }
  Alpha (int i) {  }
}
class Beta extends Alpha {
  Beta(int i) {
  }
}
  1.   The code does not compile because Beta's constructor is empty.
  2.   The code does not compile because Alpha doesn't define a no-args constructor.
  3.   The code compiles and runs succesfully.
  4.   The code does not compile because Beta does not define a no-args constructor.
  5.   none of the above

35. What is the result of attempting to compile and run this code?
class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta();
  }
}
class Beta extends Alpha { }
  1.   The code does not compile because Beta does not define a no-args constructor.
  2.   The code does not compile because Beta does not define any constructors whatsoever.
  3.   The code compiles and runs succesfully, with no output.
  4.   The code does not compile because Alpha doesn't define a no-args constructor.
  5.   none of the above

36. What is the result of attempting to compile and run this code?
abstract class Alpha {
  abstract void complain(String s);
}
class Beta extends Alpha {
  void complain(String s) {
    System.out.println(s);
  }
}
class Tester {
  public static void main(String[] args) {
    Beta f = new Beta();
    f.complain("There's a tomato in every automaton.");
  }
}
  1.   The program will compile and run.
  2.   The code will not compile because Beta has no default no-arg constructor.
  3.   The code does not compile because Beta is abstract.
  4.   The code does not compile because Tester does not inherit from Alpha.
  5.   none of the above

A201/A597 TTFN Exam Thu Aug 14 2003 in LH201