Please don't.
Exam is closed-book, closed-notes, and individual work.

CSCI A201/A597/I210

Second Midterm Examination

Spring Semester 2003


Directions:

Good luck and do well!


1. 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); 
  }
} 
  1.   true
  2.   false
  3.   other


2. Consider the following condition.
! (x % 3 != 0 || x % 2 != 0) 
Which of the following represents a simplification of it (obtained perhaps using DeMorgan).
  1.   ((x % 2) == 0)
  2.   ((x % 3) == 0)
  3.   ((x % 6) == 0)
  4.   ((x % 6) != 0)
  5.   none of the above


3. Assume a is of type boolean and consider the following condition.
(a && !a) == false
Which of the following represents a simplification of it.
  1.   a
  2.   !a
  3.   true
  4.   false
  5.   none of the above


4. 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?
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other


5. 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?
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other

6. 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?
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other

7. 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?
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other

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


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


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

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


12. 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 + ")"); 
  1.   (1, 1)
  2.   (1, 2)
  3.   (2, 1)
  4.   (2, 2)
  5.   other

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

14. Consider the following fragment. What does it print?
int[] v = { 1, 2, 3, 3, 2, 1},
      m = { 6, 5, 4, 4, 5, 6};      

int sum = 0; 

for (int i = 0; i < v.length; i++) 
  sum += (v[i] - m[i]); 

System.out.print(sum);
  1.   -18
  2.   -16
  3.   16
  4.   18
  5.   other

15. Consider the following program. What does it print?
int[] a = {1, 2, 3, 4, 5}; 

int value = a[a.length - 1];

for (int i = a.length - 1; i >= 0; i--) 
  if (a[i] > value) 
    value = a[i];

System.out.println(value); 
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other

16. Consider the following program fragment. What would it print?
int[] a = { 6, 5, 4, 3, 2, 1 }; 
boolean done = false;
while (! done) {
  done = true; 
  for (int i = 0; i < a.length - 1; i++) 
    if (a[i] > a[i + 1]) { 
     done = false; 
     int temp = a[i]; 
     a[i] = a[i + 1];
     a[i + 1] = temp; 
    } 
} 
for (int i = 0; i < a.length; i++)
  System.out.print(a[i]);
System.out.println(); 
  1.   123456
  2.   654321
  3.   543216
  4.   612345
  5.   other

17. Consider the following program fragment. What would it print?
int[] a = { 6, 5, 4, 3, 2, 1 }; 

for (int i = 0; i < a.length - 1; i++) 
  if (a[i] > a[i + 1]) { 

    int temp = a[i]; 
    a[i] = a[i + 1];
    a[i + 1] = temp; 

  } 
 
for (int i = 0; i < a.length; i++)
  System.out.print(a[i]);

System.out.println(); 
  1.   123456
  2.   654321
  3.   543216
  4.   612345
  5.   other

18. 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 ( nuf == 0 || 
             nuf == size - 1 || 
             __________________ )
          System.out.print(" *"); 
        else 
          System.out.print("  "); 
      System.out.println(); 
    }
  } 
}
What's needed for the output to resemble an Z?
  1.   fun == size - 1
  2.   fun == 0
  3.   fun + nuf == size - 1
  4.   fun + nuf == 0
  5.   other


19. Consider the following code fragment, what does it print?
String seven = "eleven"; 

System.out.println(seven.substring(0, 3)); 
  1.   sev
  2.   eve
  3.   ele
  4.   lev
  5.   other


20. Consider the following code fragment, what does it print?
String one = "BC";  
System.out.println((char)('A' + one.length())); 
  1.   A
  2.   ABC
  3.   BC
  4.   C
  5.   other

21. Consider the following code fragment, what does it print?
String one = "3";  
System.out.println((char)('3' + one.length() + 3)); 
  1.   0
  2.   3
  3.   6
  4.   9
  5.   other

22. Does this source code compile?
class One { } 
class Two extends One { }
class Three {
  public static void main(String[] args) {
    Two a = new One(); 
  }
}
  1.   yes
  2.   no


23. Does this source code compile?
class Three { } 
class Two extends Three { }
class One {
  public static void main(String[] args) {
    Three a = new Two(); 
  }
}
  1.   yes
  2.   no


24. Does this source code compile?
class One { } 
class Two extends One { }
class Three extends Two { } 
class Four {
  public static void main(String[] args) {
    Two a = new Three(); 
  }
}
  1.   yes
  2.   no


25. Does this source code compile?
class Four { } 
class Three extends Four { }
class Two extends Three { } 
class One {
  public static void main(String[] args) {
    Three a = new Four(); 
  }
}
  1.   yes
  2.   no


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

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

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

} 
  1.   6
  2.   5
  3.   4
  4.   3
  5.   other

27. 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]; 

  }
} 
  1.   6
  2.   5
  3.   4
  4.   3
  5.   other

28. What gets printed when you compile and run the following program?
class One {

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

  public static int fun(int a, int b) {
    return b;
  } 

  public static int nuf(int a, int b) {
    return a; 
  }

} 
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other


29. 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;  
  } 

} 
  1.   1
  2.   2
  3.   3
  4.   4
  5.   other


30. What does the next fragment print if embedded in a complete program?
String two = "to".charAt(0) + "two".charAt(1) + "ew"; 
System.out.println(two); 
  1.   230ew
  2.   twew
  3.   two
  4.   235ew
  5.   other

31. 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; 

} 
  1.   cbadef
  2.   abcfed
  3.   cfbead
  4.   fedabc
  5.   other


32. 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 = two.charAt(i) + answer + one.charAt(i); 
  }  

  return answer; 
} 
  1.   cbadef
  2.   abcfed
  3.   cfbead
  4.   fedabc
  5.   other


33. 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) + answer + two.charAt(i); 
  }  

  return answer; 

} 
  1.   cbadef
  2.   abcfed
  3.   cfbead
  4.   fedabc
  5.   other


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


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

36. 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.
  1.   int[] fun(int[], int)
  2.   int[] fun(int, int[])
  3.   int fun(int, int[])
  4.   int fun(int[], int[])
  5.   none of the above


A201/A597/I210 Second Midterm Exam Makeup Thu Apr 24 2003 in RH100