AP Computer Science Midterm Review Part 1

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

13. What is the output of the following program segment? int num = 5; while (num >= 0) { num -= 2; } System.out.print(num); (A) -2 (B) -1 (C) 0 (D) 2 (E) 21

Answer: B

1. Consider the following method public void process(String s) { s = s.substring(2, 3) + s.substring(1, 2) + s.substring(0, 1); } What is printed as a result of executing the following statements (in a method in the same class)? String s = "ABCD"; process(s); System.out.println(s); (A) ABCD (B) CBA (C) CDBCA (D) CDBCAB (E) IndexOutOfBoundsException

Answer: A

14. What is the output from int n = 12; System.out.print(goFigure(n)); System.out.print(" " + n); Where the method goFigure is defined as follows: public double doFigure(int n) { n %= 7; return (double)(12 /n); } (A) 2.0 12 (B) 2.4 12 (C) 2.0 5 (D) 2.4 5 (E) 2.4 6

Answer: A

21. Consider the following method. public void change(double[ ] nums, int n) { for (int k = 0; k < n; k++) { nums[k] = 5.4; } n = 2; } What will be stored in samples and len after the following statements are executed? double[ ] samples = {1.0, 2.1, 3.2, 4.3}; int len = samples.length; change(samples, len); (A) samples contains 5.4, 5.4, 5.4, 5.4; len is 4 (B) samples contains 5.4, 5.4, 5.4, 5.4; len is 2 (C) samples contains 1.0, 2.1, 3.2, 4.3; len is 4 (D) samples contains 5.4, 5.4; len is 2 (E) samples contains 1.0, 2.1; len is 2

Answer: A

24. Given the declarations int p = 5, q = 3; which of the following expressions evaluate to 7.5? I. (double)p * (double)q / 2; II. (double)p * (double)(q / 2); III. (double)(p * q / 2); (A) I only (B) II only (C) I and II only (D) I, II, and III (E) None of them

Answer: A

25. Consider the following method fun2. public int fun2(int x, int y) { y -= x; return y; } What are the values of the variables a and b after the following code is executed? int a = 3, b = 7; b = fun2(a, b); a = fun2(b, a); (A) -1 and 4 (B) -4 and 7 (C) -4 and 4 (D) 3 and 7 (E) 3 and 4

Answer: A

6. Which of the following methods are equivalent (always return the same value for the same values of input parameters)? I. public boolean fun(int a, int b, int c) { if (a >= b) if (b >= c) return true; else return false; else retuen false; } II. public boolean fun(int a, int b, int c) { if (a >= b && b >= c) return true; else retuen false; } III. public boolean fun(int a, int b, int c) { return a >= b || b >= c; } (A) I and II only (B) I and III only (C) II and III only (D) All three are equivalent (E) All three are different

Answer: A

7. Consider the following code segment. int[ ] nums = new int[51]; for (int k = 0; k < nums.length; k++) nums[k] = 1; for (int k = 3; k <= 50; k += 3) nums[k] = 0; for (int k = 5; k <= 50; k += 5) nums[k] = 0; How many elements in the array nums have the value 0 after this code has been executed? (A) 23 (B) 25 (C) 26 (D) 27 (E) 28

Answer: A

9. Consider the following method. private int swap(int a, int b) { if (a < b) { b = a; a = b; } return b - a; } What are the values of the variables a, b, and c after the following statements are executed? int a =2, b = 5; int c = swap(a, b); (A) 2, 5, 0 (B) 2, 5, 3 (C) 2, 5, -3 (D) 2, 2, 0 (E) 5 2 3

Answer: A

2. Which of the following statements will result in a syntax error? (A) String x = "123"; (B) Integer x = "123"; (C) Object x = "123"; (D) String[ ] x = {"123"}; (E) All of the above will compile with no errors.

Answer: B

22. Consider the following code segment. List list = new ArrayList( ); list.add("A"); list.add("B"); list.add("C"); for (String s : list) { String t = list.get(list.size( ) - 1); list.set(list.size( ) - 1, s); s = t; } What will list contain after the above code segment has been executed? (A) ["A", "B", "C"] (B) ["A", "B", "B"] (C) ["C", "B", "A"] (D) ["C", "A", "B"] (E) ["C", "C", "C"]

Answer: B

23. Consider the following class. public class Counter { private int count = 0; public Counter( ) { count = 0; } public Counter(int x) { count = x; } // Line 1 public int getCount( ) { return count; } // Line 2 public void setCount(int c) { int count = c; } // Line 3 public void increment( ) { count++; } // Line 4 public String toString( ) { return "" + count; } // Line 5 } The test code Counter c = new Counter( ); c.setCount(3); c.increment( ); System.out.println(c.getCount( )); is supposed to print 4, but the class has an error. What is actually printed, and which line in the class definition should be changed to get the correct output, 4? (A) 0, Line 1 (B) 1, Line 3 (C) 0, Line 4 (D) 3, Line 4 (E) 36, Line 5

Answer: B

18. What will array arr contain after the following code segment has been executed? int[ ] arr = {4, 3, 2, 1, 0}; for (int k = 1; k < arr.length; k++) { arr[k - 1] += arr[k]; } (A) 4, 7, 5, 3, 1 (B) 4, 7, 9, 10, 10 (C) 7, 5, 3, 1, 0 (D) 7, 3, 2, 1, 0 (E) 10, 6, 3, 1, 0

Answer: C

19. The code fragment int x = < an integer value >; System.out.println(x*x); displays -131071. Which of the following is a possible value of x? (A) -1 (B) 2 15 + 1 (C) 2 16 − 1 (D) 2 32 + 1 (E) No such value exists

Answer: C

4. What is the result when the following code segment is compiled and executed? int m = 4, n = 5; double d = Math.sqrt((m + n)/2); System.out.println(d); (A) Syntax error "sqrt(double) in java.lang.Math cannot be applied to int" (B) 1.5 is displayed (C) 2.0 is displayed (D) 2.1213203435596424 is displayed (E) ArithmeticException

Answer: C

5. For which of the following pairs of values a and b does the expression (a > 20 && a < b) || (a > 10 && a > b) evaluate to true? (A) 5 and 0 (B) 5 and 10 (C) 15 and 10 (D) 15 and 20 (E) None of the above

Answer: C

8. What are the contents of the array nums after the following code segment has been executed? int[ ] nums = new int[8]; nums[0] = 0; int n = 1; while (n < nums.length) { int k; for (k = n; k < 2*n; k++) nums[k] = nums[k - n] + 1; n = k; } How many elements in the array nums have the value 0 after this code has been executed? (A) 0 1 1 1 1 1 1 1 (B) 0 1 0 1 0 1 0 1 (C) 0 1 1 2 1 2 2 3 (D) 0 1 2 3 1 2 3 4 (E) 0 1 2 3 4 5 6 7

Answer: C

20. What is the output of the following code segment? List list = new ArrayList( ); nums = Test.doNothing(nums); for (int i = 1; i <= 8; i++) { list.add(new Integer(i)); } for (int i = 0; I < list.size( ); i++) { list.remove(i); } for (Integer x : list) { System.out.print(x + " "); } (A) IndexOutOfBoundsException (B) No output because the resulting list is empty (C) 1 3 5 7 (D) 2 4 6 8 (E) 1 2 3 4 5 6 7 8

Answer: D

10. Consider the following method. public int countSomething(int[ ] arr) { int m = arr[0]; int count = 1; for (int k = 1; k < arr.length; k++) { int a = arr[k]; if (a > m) { m = a; count = 1; } else if (m == a) count++; } return count: } (A) int[ ] arr = {0, 1, 1, 1, 1}; (B) int[ ] arr = {1, 6, 5, 4, 0}; (C) int[ ] arr = {1, 0, 5, 6, 1}; (D) int[ ] arr = {3, 2, 1, 0, 5}; (E) None of the above

Answer: E

11. What is printed when the following code segment is executed? String[ ] xy = {"X", "Y"}; String[ ] yx = xy; yx[0] = xy[1]; yx[1] = xy[0]; System.out.println(xy[0] + xy[1] + yx[0] + yx[1]); (A) XXXX (B) XYYX (C) XYXY (D) XYYY (E) YYYY

Answer: E

12. What is the output from the following code segment? ArrayList cities = new ArrayList( ); cities.add("Atlanta"); cities.add("Boston"); cities.add("Chicago"); for(String city : cities) city = city.substring(1); System.out.println(cities); (A) [A, B, C] (B) [C, B, A] (C) [a, n, o] (D) [tlanta, oston, hicago] (E) [Atlanta, Boston, Chicago]

Answer: E

15. Which of the following expressions will evaluate to true when x and y are boolean variables with different values? I. (x || y) && (!x || !y) II. (x || y) && !(x || y) III. (x || !y) || (!x || y) ( A) I only (B) II only (C) I and II only (D) II and III only (E) I, II, and III

Answer: E

16. What is the output from the following code segment? double x = 5*4/2 - 5/2*4; System.out.println(x); (A) 0 (B) 1 (C) 0.0 (D) 1.0 (E) 2.0

Answer: E

17. What is the output from the following code segment? String band = "anamanaguchi"; System.out.println(band.substring(1, 4).compareTo(band.substring(5, 8))); (A) true (B) false (C) 0 (D) A negative integer (E) A positive integer

Answer: E

3. What is printed as a result of executing the following statements? double x = 2.5, y = 1.99; System.out.println((int)(x/y) + (int)(x*y)); (A) 0 (B) 3 (C) 4 (D) 4.0 (E) 5

Answer: E


Kaugnay na mga set ng pag-aaral

Ch.11 Differential Analysis: The Key to Decision Making

View Set

Social Psych Chapter 7: Persuasion

View Set

Chapter Three: Seawater; It's Makeup and Movements

View Set