PLEASE, DON'T!

CSCI A201/A597/I210

Final Exam

Spring Semester 2003

Exam is closed-book, closed-notes, and individual work.


Directions:

Good luck and do well!


1. Consider the following program:
abstract class Alpha {
  abstract void complain();
}
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.");
  }
}
What will happen if you try to compile and execute the Tester class?

A. The program will compile and run.
B. The code will not compile because Beta has no default no-arg constructor.
C. The code does not compile because Beta is abstract.
D. The code does not compile because Tester does not inherit from Alpha.
E. None of the above.

2. Which of the following statements about arrays is true?
A. All of the elements in an array must be of the same type.
B. You may not dimension an array to be larger than 100.
C. Arrays automatically stretch to fit the number of inputs given.
D. Two arrays can be joined together with the + operator.
E. All of the above are true.

For the next two questions please consider the following method:
int fun(int m) {
  int n = 0;
  if (m == 0) {
    n = 1;
  } else {
    n = m + fun(m - 2);
  }
  return n;
}
Please answer the following two questions:

3. What does fun(4) evaluate to?

A. 4
B. 5
C. 7
D. it never ends
E. none of the above
4. What does fun(5) evaluate to?
A. 4
B. 5
C. 7
D. it never ends
E. none of the above

5. After the following statement is executed,
int x = (int)(Math.random() * (12 - 5) + 6);
which of these numbers could NOT possibly be contained in x?
A. 6
B. 12
C. 5
D. 9
E. 8

6. Assuming that the following program fragment is syntactically correct...
int[] a = new int[10];
  ...
a[0] = fun(a, a); 
... select the correct header for method fun.
A. int[] fun(int[] x, int y)
B. int[] fun(int x, int[] y)
C. int fun(int x, int[] y)
D. int fun(int[] x, int[] y)
E. none of the above

7. What does mix("abc", "def") return if mix is defined as below?
public static String mix(String one, String two) {

  String answer = ""; 

  int lengthOne = one.length(), lengthTwo = two.length();

  for (int i = 0; i < lengthOne && i < lengthTwo; i++) {
    answer = one.charAt(i) + (two.charAt(i) + answer); 
  }  

  return answer; 

} 
Please choose one of the options listed below:
A. cbadef
B. abcfed
C. cfbead
D. fedabc
E. none of the above

8. What gets printed when you compile and run the following program?

class One {

  public static void main(String[] args) {
    System.out.println(nuf(fun(nuf(fun(1))))); 
  }

  static int fun(int value) {
    return value - 1; 
  } 

  static int nuf(int value) {
    return value + 1;  
  } 

}
Choose one of the following options:
A.   1
B.   2
C.   3
D.   4
E.   other

9. 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[1]); 

    System.out.println(x[0] - x[1] + x[2]);
  }

  public static void fun(int value) {
    value = 1 - value;
  }

} 
Choose one of the following:
A.  6
B.  5
C.  4
D.  3
E.  other

10. 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); 

    System.out.println(x[0] - x[1] + x[2]);

  }
  public static void fun(int[] value) {

    value[1] = 1 - value[1]; 

  }
} 
Choose one of the following:
A.  6
B.  5
C.  4
D.  3
E.  other

11. Consider the following fragment. What does it print?
int x = 2, y = 1; 
y = x + y;
x = y - x; 
y = y - x; 
System.out.println("(" + x + ", " + y + ")"); 
Choose among the following:
A.  (1, 1)
B.  (1, 2)
C.  (2, 1)
D.  (2, 2)
E.  other

12. You compile this program ...
class One {
  public static void main(String[] args) {
    System.out.println(args[args.length - args[3].length()]);     
  }
}
... then run it as follows:
java One this is my song for the asking
What does the program print (if anything)?
A. this
B. song
C. ing
D. the program compiles but does not run
E. other

13. 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[0][1] - 1][0]); 
  }
} 
A.  2
B.  4
C.  6
D.  8
E.  something else will happen

14. 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[1]); // focus here!!
    System.out.println(x[0] + x[1] + x[2]);
  }
  public static void fun(int value) {
    value = value - 3 * value;;
  }
} 
A.  0
B.  2
C.  4
D.  6
E.  something else will happen

15. Does this source code compile?
class Vegetable { } 
class Tomato extends Vegetable { }
class Kroger {
  public static void main(String[] args) {
    Vegetable a = new Tomato(); 
  }
}
A.  yes
B.  no


16. Does this source code compile?
class Vegetable { } 
class Cabbage extends Vegetable { }
class Kohlrabi extends Cabbage { } 
class Kroger {
  public static void main(String[] args) {
    Cabbage a = new Kohlrabi(); 
  }
}
A.  yes
B.  no


17. Does this source code compile?
class Vegetable { } 
class Cabbage extends Vegetable { }
class Kohlrabi extends Cabbage { } 
class Kroger {
  public static void main(String[] args) {
    Kohlrabi a = new Vegetable(); 
  }
}
A.  yes
B. no


18. 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();
         }
     }
}
Choose from the following:
A.  C
B.  Z
C.  U
D.  N
E.  none of the above

19. What is the result of attempting to compile and run this code?
class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta();
  }
  Alpha(int i) {  }
}
class Beta extends Alpha { }
Choose from one of the options below:
A. The code does not compile because Beta does not define a no-args constructor.
B. The code does not compile because Beta does not define any constructors whatsoever.
C. The code compiles and runs succesfully, with no output.
D. The code does not compile because Alpha doesn't define a no-args constructor.
E. None of the above.


20. What is the result of attempting to compile and run this code?
class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(3);
  }
  Alpha (int i) {  }
}
class Beta extends Alpha {
  Beta(int i) {
  }
}
Choose one of the options below:
A. The code does not compile because Beta's constructor is empty.
B. The code does not compile because Alpha doesn't define a no-args constructor.
C. The code compiles and runs succesfully.
D. The code does not compile because Beta does not define a no-args constructor.
E. None of the above.


21. What is the result of attempting to compile and run this code?
class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(3);
  }
  Alpha() { System.out.println(0); }
  Alpha(int i) {
    System.out.println(i);
  }
}
class Beta extends Alpha {
  Beta()      { }
  Beta(int i) { }
}
Please choose one of the options listed below:
A.The code does not compile because Beta's constructors are empty.
B. The code compiles and runs succesfully, but there is no output.
C. The code compiles and runs succesfully, and outputs 0.
D. The code compiles and runs succesfully, and outputs 3.
E. None of the above.

22. What is the result of attempting to compile and run this code?
class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(3);
  }
  Alpha() {
    System.out.println(0);
  }
  Alpha(int i) {
    System.out.println(i);
  }
}
class Beta extends Alpha {
  Beta() {
    super(6);
  }
  Beta(int i) {
    this();
  }
}
Please choose one of the options listed below:
A. The code compiles and runs succesfully, and outputs 0.
B. The code compiles and runs succesfully, and outputs 3.
C. The code compiles and runs succesfully, and outputs 6.
D. The code does not compile because at least one of Beta's constructors is not defined legally.
E. None of the above.

23. What is the result of attempting to compile and run this code?
class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(3);
  }
  Alpha() {
    System.out.println(0);
  }
  Alpha(int i) {
    System.out.println(i);
  }
}
class Beta extends Alpha {
  Beta() {
    super(6);
  }
  Beta(int i) {
    super(3);
    this();
  }
}
Please choose one of the options listed below:
A. The code compiles and runs succesfully, and outputs 0.
B. The code compiles and runs succesfully, and outputs 3.
C. The code compiles and runs succesfully, and outputs 6.
D. The code does not compile because at least one of Beta's constructors is not defined legally.
E. None of the above.

24. 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) { }  
} 
A.    0
B.    1
C.    2
D.    3
E.    other

25. 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) { }  
} 
Choose from the options listed below:
A.    0
B.    1
C.    2
D.    3
E.    none of the above

26. 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; 
  }
} 
A.    0
B.    1
C.    2
D.    3
E.   none of the above

27. 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; 
  }
} 
A.    0
B.    1
C.    2
D.    3
E.    none of the above

28. 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; 
  }
} 
    A.    0
    B.    1
    C.    2
    D.    3
    E.    none of the above

29. 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; 
  }
} 
A.    0
B.    1
C.    2
D.    3
E.    none of the above

30. You compile and run this program. What's the output?
public class Four {
    public static void main(String[] args) {
        Four a = new Four(); 
        a.nuf(1); 
        a.fun(1); 
        a.nuf(2); 
        a.fun(2); 
        a.nuf(3); 
        System.out.println(Four.m + a.n); 
    } 
    void nuf(int p) {
        this.n = this.n + p; 
        Four.m = Four.m - p; 
    } 
    void fun(int p) {
        int temp = p; 
        this.n = Four.m; 
        Four.m = temp; 
    } 
    int n; 
    static int m; 
} 
Please choose from the options listed below:
A.   0
B.   1
C.   2
D.   3
E.   none of the above

31. You compile and run this program. What's the output?
public class Five {
    public static void main(String[] args) {
	Five a = new Five(); 
	Five b = new Five(); 
	a.nuf(1); 
	a.fun(1); 
	b.nuf(2); 
	b.fun(2); 
	a.nuf(3); 
	System.out.println(Five.m + a.n); 
    } 
    void nuf(int p) {
	this.n = this.n + p; 
	Five.m = Five.m - p; 
    } 
    void fun(int p) {
	int temp = p; 
	this.n = Five.m; 
	Five.m = temp; 
    } 
    int n; 
    static int m; 
} 
Please choose from the options listed below:
A.   0
B.   1
C.   2
D.   3
E.   none of the above


32. 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); 
}
A.   true
B.   false

33. 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); 
}
A.   true
B.   false

34. 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); 
}
A.   true
B.   false

35. 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); 
}
A.   true
B.   false

36. 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); 
}
A.  true
B.  false

37. 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); 
}
A.   true
B.   false

38. 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); 
}
A.   true
B.   false

39. 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); 
}
A.   true
B.   false

40. 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); 
}
A.   true
B.   false

41. Which one of the for loops presented below is NOT equivalent to this while loop:
i = 0;
sum = 0;
while (i < 100) {
  sum += i;
  i += 2;
}
Assume that i and sum have been declared already.
A. for (i = sum = 0; i < 100; sum += i, i += 2) ;
B. for (i = 0, sum = 0; i < 100; i += 2) sum += i;
C. for (i = sum = 0; (i += 2) < 100 ; ) sum += i;
D. for (i = sum = 0; i < 100; sum += i) i += 2;
E. for (i = sum = 0; i < 100; i += 2) sum += i;

42. What does the following program's output look like?
public class Eleven {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        for (i = 0; i < 10; i++) {
          for (j = 0; j < 10; j++) {
              if ((i == j) && (i + j == 10)) {
                System.out.print(" " + j);
              } else {
                System.out.print("  ");
              }
          }
          System.out.println();
        }
    }
}
Please choose from the options below:
A. a long line of numbers
B. a long column of numbers
C. a single number will be printed, the number 5
D. numbers organized in a pattern other than those listed above

43. 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.
A. true
B. (x != 0) || (x >= 10)
C. false
D. (x < 0) || (x >= 10)
E. (x < 0) && (x >= 10)

44. 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.
A. true
B. (x != 0) || (x >= 10)
C. false
D. (x < 0) || (x >= 10)
E. (x < 0) && (x >= 10)

45. You compile and run the following program. What does it print?
class One {
  public static void main(String[] args) {
    boolean p = false, 
            q = true, 
            r = true;
    System.out.println(!!!p || !!q && !r); 
  }
} 
A.  true
B.  false
C.  other


46. Consider the following condition.
! (x % 3 != 0 || x % 2 != 0) 
Which of the following represents a simplification of it (obtained perhaps using DeMorgan).
A.   ((x % 2) == 0)
B.   ((x % 3) == 0)
C.   ((x % 6) == 0)
D.   ((x % 6) != 0)
E.   none of the above


47. Assume a is of type boolean and consider the following condition.
(! (a && !a)) == false
Which of the following represents a simplification of it.
A.   a
B.   !!a
C.   true
D.   false
E.   none of the above


48. Consider the following program fragment.
int x = 1, y = 1; if (x < 2) if (x > 1) y = 2; else y = 3; else y = 4; 
What value does y have at the end?
A.   1
B.   2
C.   3
D.   4
E.   none of the above


49. Consider the following program fragment.
int x = 1, y = 1; if (x > 2) y = 2; y = y + 1; else y = y + 1; 
What value does y have at the end?
A.   1
B.   2
C.   3
D.   4
E.   none of the above

50. Consider the following program fragment.
int x = 1, y = 1; if (x > 2) { y = 2; y = y + 1; } else y = y + 1; 
What value does y have at the end?
A.   1
B.   2
C.   3
D.   4
E.   none of the above

51. Consider the following program fragment.
int x = 1, y = 1; if (x < 2)  y = 2; else y = y + 1; y = y - 1;
What value does y have at the end?
A.   1
B.   2
C.   3
D.   4
E.   none of the above

52. How many question marks will be printed by the following code fragment?
for (int i = -10; i <= 10; i = i + 4)
  System.out.print("?"); 
A.   5
B.   10
C.   15
D.   20
E.   none of the above


A201/A597/I210 Final Exam Tue May 6 5-7pm 2003 in RH100