Please don't. Exam is closed-book, closed-notes, and individual work. CSCI A201/A597/I210 Second Midterm Examination Spring Semester 2003

Directions:
• Please read the questions carefully and select the ONE ANSWER that is best, then mark the corresponding space on the answer sheet for each question.

• All correct answers are worth exactly one point. Leaving a question unanswered will count as a zero. There is no penalty for guessing. DO NOT MARK more than one answer for each question.

• Don't forget to write your name and student ID on the scannable answer sheet provided. You have the entire class period (75 minutes) to complete the test.

The test is closed-book and closed-notes.

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); } } true   false   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). ((x % 2) == 0)   ((x % 3) == 0)   ((x % 6) == 0)   ((x % 6) != 0)   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. a   !a   true   false   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   2   3   4   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   2   3   4   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   2   3   4   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   2   3   4   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("?"); 5   10   15   20   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("?"); 0   2   4   6   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("?"); } 0   1   2   3   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)   (2, 1)   (2, 2)   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)   (2, 1)   (2, 2)   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   2   3   4   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); -18   -16   16   18   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   2   3   4   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(); 123456   654321   543216   612345   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(); 123456   654321   543216   612345   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? fun == size - 1   fun == 0   fun + nuf == size - 1   fun + nuf == 0   other

 19. Consider the following code fragment, what does it print? String seven = "eleven"; System.out.println(seven.substring(0, 3)); sev   eve   ele   lev   other

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

 21. Consider the following code fragment, what does it print? String one = "3"; System.out.println((char)('3' + one.length() + 3)); 0   3   6   9   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(); } } yes   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(); } } yes   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(); } } yes   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(); } } yes   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; } } 6   5   4   3   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]; } } 6   5   4   3   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   2   3   4   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   2   3   4   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); 230ew   twew   two   235ew   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; } cbadef   abcfed   cfbead   fedabc   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; } cbadef   abcfed   cfbead   fedabc   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; } cbadef   abcfed   cfbead   fedabc   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.   int[] fun(int[], int)   int[] fun(int, int[])   int fun(int, int[])   int fun(int[], int[])   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.   int[] fun(int[], int)   int[] fun(int, int[])   int fun(int, int[])   int fun(int[], int[])   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.   int[] fun(int[], int)   int[] fun(int, int[])   int fun(int, int[])   int fun(int[], int[])   none of the above

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