ap csa final units 1-4

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Consider the following code segment. for (int outer = 0; outer < n; outer++) { for (int inner = 0; inner <= outer; inner++) { System.out.print(outer + " "); } } If n has been declared as an integer with the value 4, what is printed as a result of executing the code segment? A 0 1 2 3 B 0 0 1 0 1 2 C 0 1 2 2 3 3 3 D 0 1 1 2 2 2 3 3 3 3 E 0 0 1 0 1 2 0 1 2 3

0 1 1 2 2 2 3 3 3 3

Consider the following code segment. for (int outer = 0; outer < n; outer++) { for (int inner = 0; inner <= outer; inner++) { System.out.print(outer + " "); } } If n has been declared as an integer with the value 4, what is printed as a result of executing the code segment? A 0 1 2 3 B 0 0 1 0 1 2 C 0 1 2 2 3 3 3 D 0 1 1 2 2 2 3 3 3 3 E 0 0 1 0 1 2 0 1 2 3

0 1 1 2 2 2 3 3 3 3

Consider the following code segment. int k = 0; while (k < 10) { System.out.print((k % 3) + " "); if ((k % 3) == 0) k = k + 2; else k++; } What is printed as a result of executing the code segment? A 0 2 1 0 2 B 0 2 0 2 0 2 C 0 2 1 0 2 1 0 D 0 2 0 2 0 2 0 E 0 1 2 1 2 1 2

0 2 0 2 0 2 0

Consider the following code segment. int k = 1; while (k < 20) { if ((k % 3) == 1) System.out.print(k + " "); k++; } What is printed as a result of executing this code segment? A 2 5 8 11 14 17 B 3 6 9 12 15 18 C 1 4 7 10 13 16 19 D 1 3 5 7 9 11 13 15 17 19 E 2 4 6 8 10 12 14 16 18 20

1 4 7 10 13 16 19

Consider the following class declaration. Which of the following declarations will compile without error? Student a = new Student(); Student b = new Student("Juan", 15); Student c = new Student("Juan", "15"); A I only B II only C I and II only D I and III only E I, II, and III

I and II only

Consider the following method. public int someCode(int a, int b, int c) { if ((a < b) && (b < c)) return a; if ((a >= b) && (b >= c)) return b; if ((a == b) || (a == c) || (b == c)) return c; } Which of the following best describes why this method does not compile? A The reserved word return cannot be used in the body of an if statement. B It is possible to reach the end of the method without returning a value. C The if statements must have else parts when they contain return statements. D Methods cannot have multiple return statements. E The third if statement is not reachable.

It is possible to reach the end of the method without returning a value.

Consider the following code segment. int x = /* some integer value */ ; int y = /* some integer value */ ; boolean result = (x < y); result = ( (x >= y) && !result ); Which of the following best describes the conditions under which the value of result will be true after the code segment is executed? A Only when x < y B Only when x >= y C Only when x and y are equal D The value will always be true. E The value will never be true.

Only when x >= y

Consider the following data field and method. private int[] seq; // precondition: seq.length > 0 public int lenIncreasing() { int k = 1; while ((k < seq.length) && (seq[k - 1] < seq[k])) { k++; } // assertion return k; } Which of the following assertions is true when execution reaches the line // assertion in lenIncreasing? A (k == seq.length) && (seq[k - 1] >= seq[k]) B (k == seq.length) || (seq[k - 1] >= seq[k]) C (k < seq.length) && (seq[k - 1] < seq[k]) D (k < seq.length) || (seq[k - 1] < seq[k]) E (k == seq.length) || (seq[k - 1] == seq[k])

(k == seq.length) || (seq[k - 1] >= seq[k])

The question refer to the following code segment. int k = a random number such that 1 ≤ k ≤ n ; for (int p = 2; p <= k; p++) for (int r = 1; r < k; r++) System.out.println("Hello"); What is the maximum number of times that Hello will be printed? A 2 B n - 1 C n - 2 D (n - 1)^2 E n2

(n - 1)^2

Consider the following incomplete method. Method findNext is intended to return the index of the first occurrence of the value val beyond the position start in array arr. // returns index of first occurrence of val in arr // after position start; // returns arr.length if val is not found public int findNext(int[] arr, int val, int start) { int pos = start + 1; while ( /* condition */ ) pos++; return pos; } For example, consider the following code segment. int[] arr = {11, 22, 100, 33, 100, 11, 44, 100}; System.out.println(findNext(arr, 100, 2)); The execution of the code segment should result in the value 4 being printed. Which of the following expressions could be used to replace /* condition */ so that findNext will work as intended? A (pos < arr.length) && (arr[pos] != val) B (arr[pos] != val) && (pos < arr.length) C (pos < arr.length) || (arr[pos] != val) D (arr[pos] == val) && (pos < arr.length) E (pos < arr.length) || (arr[pos] == val)

(pos < arr.length) && (arr[pos] != val)

Consider the following method. Method allEven is intended to return true if all elements in array arr are even numbers; otherwise, it should return false. public boolean allEven(int[] arr) { boolean isEven = /* expression */ ; for (int k = 0; k < arr.length; k++) { /* loop body */ } return isEven; } Which of the following replacements for /* expression */ and /* loop body */ should be used so that method allEven will work as intended? A /* expression *//* loop body */ false if ((arr[k] % 2) == 0) isEven = true; B /* expression *//* loop body */ false if ((arr[k] % 2) != 0) isEven = false; else isEven = true; C /* expression *//* loop body */ true if ((arr[k] % 2) != 0) isEven = false; D /* expression *//* loop body */ true if ((arr[k] % 2) != 0) isEven = false; else isEven = true; E /* expression *//* loop body */ true if ((arr[k] % 2) == 0) isEven = false; else isEven = true;

/* expression *//* loop body */ true if ((arr[k] % 2) != 0) isEven = false;

The question refer to the following code segment. int k = a random number such that 1 ≤ k ≤ n ; for (int p = 2; p <= k; p++) for (int r = 1; r < k; r++) System.out.println("Hello"); What is the minimum number of times that Hello will be printed? A 0 B 1 C 2 D n - 1 E n - 2

0

Consider the following class declaration public class SomeClass { private int num; public SomeClass(int n) { num = n; } public void increment(int more) { num = num + more; } public int getNum() { return num; } } The following code segment appears in another class. SomeClass one = new SomeClass(100); SomeClass two = new SomeClass(100); SomeClass three = one; one.increment(200); System.out.println(one.getNum() + " " + two.getNum() + " " + three.getNum()); What is printed as a result of executing the code segment? A 100 100 100 B 300 100 100 C 300 100 300 D 300 300 100 E 300 300 300

300 100 300

Consider the following data field and method. private int[][] mat; public void mystery () { for (int row = 1; row < mat.length; row++) { for (int col = 0; col < mat[0].length; col++) { if (row != col) mat[row][col] = mat[row - 1][col]; } } } Assume that mat contains the following values. Note that mat[0][4] is 2. 4 1 3 4 21 8 7 5 37 4 6 9 23 8 1 2 45 6 7 0 3 What values does mat contain after a call to mystery? A 4 1 3 4 2 4 8 3 4 2 4 8 6 4 2 4 8 6 2 2 4 8 6 2 3 B 4 1 3 4 2 4 1 3 4 2 4 1 3 4 2 4 1 3 4 2 4 1 3 4 2 C 4 1 3 4 2 4 1 3 4 2 1 8 7 5 3 7 4 6 9 2 3 8 1 2 4 D 4 4 4 4 4 1 1 1 1 1 7 7 7 7 7 3 3 3 3 3 5 5 5 5 5 E 4 8 6 2 3 4 8 6 2 3 4 8 6 2 3 4 8 6 2 3 4 8 6 2 3

4 1 3 4 2 4 8 3 4 2 4 8 6 4 2 4 8 6 2 2 4 8 6 2 3

Consider the following method. public void numberCheck(int maxNum) { int typeA = 0; int typeB = 0; int typeC = 0; for (int k = 1; k <= maxNum; k++) { if (k % 2 == 0 && k % 5 == 0) typeA++; if (k % 2 == 0) typeB++; if (k % 5 == 0) typeC++; } System.out.println(typeA + " " + typeB + " " + typeC); } What is printed as a result of the call numberCheck(50) ? A 5 20 5 B 5 20 10 C 5 25 5 D 5 25 10 E 30 25 10

5 25 10

Consider the following method. public int sol(int lim) { int s = 0; for (int outer = 1; outer <= lim; outer++) { for (int inner = outer; inner <= lim; inner++) { s++; } } return s; } What value is returned as a result of the call sol(10)? A 20 B 45 C 55 D 100 E 385

55

Consider the following code segment. int a = 24; int b = 30; while (b != 0) { int r = a % b; a = b; b = r; } System.out.println(a); What is printed as a result of executing the code segment? A 0 B 6 C 12 D 24 E 30

6

Consider the following code segment. int value = 15; while (value < 28) { System.out.println(value); value++; } What are the first and last numbers output by the code segment? A First Last 15 27 B First Last 15 28 C First Last 16 27 D First Last 16 28 E First Last 16. 29

First Last 15 27

Consider the following code segment. int value = 15; while (value < 28) { System.out.println(value); value++; } What are the first and last numbers output by the code segment? A FirstLast 15 27 B FirstLast 15 28 C FirstLast 16 27 D FirstLast 16 28 E FirstLast 16 29

FirstLast 15 27

Consider the following code segment. int value = 15; while (value < 28) { System.out.println(value); value++; } What are the first and last numbers output by the code segment? A FirstLast 15 27 B FirstLast 15 28 C FirstLast 16 27 D FirstLast 16 28 E FirstLast 16 29

FirstLast 15 27

Consider the following method. public void conditionalTest(int a, int b) { if ((a > 0) && (b > 0)) { if (a > b) System.out.println("A"); else System.out.println("B"); } else if ((b < 0) || (a < 0)) System.out.println("C"); else System.out.println("D"); } What is printed as a result of the call conditionalTest(3, -2)? A A B B C C D D E Nothing is printed.

C

Consider the following instance variable and incomplete method. The method calcTotal is intended to return the sum of all values in vals. private int[] vals; public int calcTotal() { int total = 0; /* missing code */ return total; } Which of the code segments shown below can be used to replace /* missing code */ so that calcTotal will work as intended? I. for (int pos = 0; pos < vals.length; pos++) { total += vals[pos]; } II. for (int pos = vals.length; pos > 0; pos--) { total += vals[pos]; } III. int pos = 0; while (pos < vals.length) { total += vals[pos]; pos++; } A I only B II only C III only D I and III E II and III

I and III

A teacher put three bonus questions on a test and awarded 5 extra points to anyone who answered all three bonus questions correctly and no extra points otherwise. Assume that the boolean variables bonusOne, bonusTwo, and bonusThree indicate whether a student has answered the particular question correctly.Each variable was assigned true if the answer was correct and false if the answer was incorrect. Which of the following code segments will properly update the variable grade based on a student's performance on the bonus questions? I. if (bonusOne && bonusTwo && bonusThree) grade += 5; II. if (bonusOne || bonusTwo || bonusThree) grade += 5; III. if (bonusOne) grade += 5; if (bonusTwo) grade += 5; if (bonusThree) grade += 5; A I only B II only C III only D I and III E II and III

I only

Consider the following code segments. I. int k = 1; while (k < 20) { if (k % 3 == 1) System.out.print( k + " "); k = k + 3; } II. for (int k = 1; k < 20; k++) { if (k % 3 == 1) System.out.print( k + " "); } III. for (int k = 1; k < 20; k = k + 3) { System.out.print( k + " "); } Which of the code segments above will produce the following output? 1 4 7 10 13 16 19 A I only B II only C I and II only D II and III only E I, II, and III

I, II, and III

Assume that a, b, and c are variables of type int. Consider the following three conditions. I. (a == b) && (a == c) && (b == c) II. (a == b) || (a == c) || (b == c) III. ((a - b) * (a - c) * (b - c)) == 0 Assume that subtraction and multiplication never overflow. Which of the conditions above is (are) always true if at least two of a, b, and c are equal? A I only B II only C III only D I and II E II and III

II and III

Consider the following declaration of the class NumSequence, which has a constructor that is intended to initialize the instance variable seq to an ArrayList of numberOfValues random floating-point values in the range [0.0, 1.0). public class NumSequence { private ArrayList<Double> seq; // precondition: numberOfValues > 0 // postcondition: seq has been initialized to an ArrayList of // length numberOfValues; each element of seq // contains a random Double in the range [0.0, 1.0) public NumSequence(int numberOfValues) { /* missing code */ } } Which of the following code segments could be used to replace /* missing code */ so that the constructor will work as intended? I. ArrayList<Double> seq = new ArrayList<Double>(); for (int k = 0; k < numberOfValues; k++) seq.add(new Double(Math.random())); II. seq = new ArrayList<Double>(); for (int k = 0; k < numberOfValues; k++) seq.add(new Double(Math.random())); III. ArrayList<Double> temp = new ArrayList<Double>(); for (int k = 0; k < numberOfValues; k++) temp.add(new Double(Math.random())); seq = temp; A II only B III only C I and II D I and III E II and III

II and III

Consider the following incomplete method. public int someProcess(int n) { /* body of someProcess */ } The following table shows several examples of input values and the results that should be produced by calling someProcess. < table > Which of the following code segments could be used to replace /* body of someProcess */ so that the method will produce the results shown in the table? I. if ((n % 3 == 0) && (n % 4 == 0)) return n * 10; else return n; II. if ((n % 3 == 0) || (n % 4 == 0)) return n * 10; return n; III. if (n % 3 == 0) if (n % 4 == 0) return n * 10; return n; A I only B II only C III only D I and III E II and III

II only

Consider the following incomplete method. public int someProcess(int n) { /* body of someProcess */ } The following table shows several examples of input values and the results that should be produced by calling someProcess. <table> Which of the following code segments could be used to replace /* body of someProcess */ so that the method will produce the results shown in the table? I. if ((n % 3 == 0) && (n % 4 == 0)) return n * 10; else return n; II. if ((n % 3 == 0) || (n % 4 == 0)) return n * 10; return n; III. if (n % 3 == 0) if (n % 4 == 0) return n * 10; return n; A I only B II only C III only D I and III E II and III

II only

Consider the following incomplete method. public int someProcess(int n) { /* body of someProcess */ } The following table shows several examples of input values and the results that should be produced by calling someProcess. <table> Which of the following code segments could be used to replace /* body of someProcess */ so that the method will produce the results shown in the table? I. if ((n % 3 == 0) && (n % 4 == 0)) return n * 10; else return n; II. if ((n % 3 == 0) || (n % 4 == 0)) return n * 10; return n; III. if (n % 3 == 0) if (n % 4 == 0) return n * 10; return n; A I only B II only C III only D I and III E II and III

II only

Consider the following method. public String inRangeMessage(int value) { if (value < 0 || value > 100) return "Not in range"; else return "In range"; } Consider the following code segments that could be used to replace the body of inRangeMessage. I. if (value < 0) { if (value > 100) return "Not in range"; else return "In range"; } else return "In range"; II. if (value < 0) return "Not in range"; else if (value > 100) return "Not in range"; else return "In range"; III. if (value >= 0) return "In range"; else if (value <= 100) return "In range"; else return "Not in range"; Which of the replacements will have the same behavior as the original version of inRangeMessage ? A I only B II only C III only D I and III E II and III

II only

Consider the following code segment. double a = 1.1; double b = 1.2; if ((a + b) * (a - b) != (a * a) - (b * b)) { System.out.println("Mathematical error!"); } Which of the following best describes why the phrase "Mathematical error!" would be printed? (Remember that mathematically (a + b) * (a - b) = a2 - b2 .) A Precedence rules make the if condition true. B Associativity rules make the if condition true. C Roundoff error makes the if condition true. D Overflow makes the if condition true. E A compiler bug or hardware error has occurred.

Roundoff error makes the if condition true.

Consider the following code segment. The code is intended to read nonnegative numbers and compute their product until a negative number is read; however, it does not work as intended. (Assume that the readInt method correctly reads the next number from the input stream.) int k = 0; int prod = 1; while (k >= 0) { System.out.print("enter a number: "); k = readInt(); // readInt reads the next number from input prod = prod * k; } System.out.println("product: " + prod); Which of the following best describes the error in the program? A The variable prod is incorrectly initialized. B The while condition always evaluates to false. C The while condition always evaluates to true. D The negative number entered to signal no more input is included in the product. E If the user enters a zero, the computation of the product will be terminated prematurely.

The negative number entered to signal no more input is included in the product.

The question refer to the following data field and method. private int[] arr; // precondition: arr.length > 0public void mystery() { int sl = 0; int s2 = 0; for (int k = 0; k < arr.length; k++) { int num = arr[k]; if ((num > 0) && (num % 2 == 0)) sl += num; else if (num < 0) s2 += num; } System.out.println(s1); System.out.println(s2); } Which of the following best describes the value of s2 output by the method mystery ? A The sum of all positive values in arr B The sum of all positive even values in arr C The sum of all negative values in arr D The sum of all negative even values in arr E The sum of all negative odd values in arr

The sum of all negative values in arr

The question refer to the following data field and method. private int[] arr; // precondition: arr.length > 0public void mystery() { int sl = 0; int s2 = 0; for (int k = 0; k < arr.length; k++) { int num = arr[k]; if ((num > 0) && (num % 2 == 0)) sl += num; else if (num < 0) s2 += num; } System.out.println(s1); System.out.println(s2); } Which of the following best describes the value of s1 output by the method mystery ? A The sum of all positive values in arr B The sum of all positive even values in arr C The sum of all positive odd values in arr D The sum of all values greater than 2 in arr E The sum of all values less than 2 in arr

The sum of all positive even values in arr

Assume obj1 and obj2 are object references. Which of the following best describes when the expression obj1 == obj2 is true? A When obj1 and obj2 are defined within the same method B When obj1 and obj2 are instances of the same class C When obj1 and obj2 refer to objects that contain the same data D When obj1 and obj2 refer to the same object E When obj1 and obj2 are private class variables defined in the same class

When obj1 and obj2 refer to the same object

Consider the following data field and method. private ArrayList list; public void mystery(int n) { for (int k = 0; k < n; k++) { Object obj = list.remove(0); list.add(obj); } } Assume that list has been initialized with the following Integer objects. [12, 9, 7, 8, 4, 3, 6, 11, 1] Which of the following represents the list as a result of a call to mystery(3)? A [12, 9, 8, 4, 3, 6, 11, 1, 7] B [12, 9, 7, 8, 4, 6, 11, 1, 3] C [12, 9, 7, 4, 3, 6, 11, 1, 8] D [8, 4, 3, 6, 11, 1, 12, 9, 7] E [1, 11, 6, 12, 9, 7, 8, 4, 3]

[8, 4, 3, 6, 11, 1, 12, 9, 7]

Consider the following code segment. ArrayList<String> items = new ArrayList<String>(); items.add("A"); items.add("B"); items.add("C"); items.add(0, "D"); items.remove(3); items.add(0, "E"); System.out.println(items); What is printed as a result of executing the code segment? A [A, B, C, E] B [A, B, D, E] C [E, D, A, B] D [E, D, A, C] E [E, D, C, B]

[E, D, A, B]

Assume that a and b are variables of type int. The expression !(a < b) && !(a > b) is equivalent to which of the following? A true B false C a == b D a != b E !(a < b) && (a > b)

a == b

Consider the following code segment. String str = "abcdef"; for (int rep = 0; rep < str.length() - 1; rep++) { System.out.print(str.substring(rep, rep + 2)); } What is printed as a result of executing this code segment? A abcdef B aabbccddeeff C abbccddeef D abcbcdcdedef E Nothing is printed because an IndexOutOfBoundsException is thrown.

abbccddeef

Assume that x and y are boolean variables and have been properly initialized. (x && y) || !(x && y) The result of evaluating the expression above is best described as A always true B always false C true only when x is true and y is true D true only when x and y have the same value E true only when x and y have different values

always true

Consider the following output. 11 21 2 31 2 3 41 2 3 4 51 2 3 4 5 6 Which of the following code segments will produce the output shown above? A for (int j = 1; j <= 6; j++) { for (int k = 1; k < j; k++) System.out.print(" " + k); System.out.println(); } B for (int j = 1; j <= 6; j++) { for (int k = 1; k <= j; k++) System.out.print(" " + j); System.out.println(); } C for (int j = 1; j <= 6; j++) { for (int k = 1; k <= j; k++) System.out.print(" " + k); System.out.println(); } D for (int j = 1; j < 6; j++) { for (int k = 1; k <= j; k++) System.out.print(" " + k); System.out.println(); } E for (int j = 1; j < 6; j++) { for (int k = 1; k < j; k++) System.out.print(" " + k); System.out.println(); }

for (int j = 1; j <= 6; j++) { for (int k = 1; k <= j; k++) System.out.print(" " + k); System.out.println(); }

Assume that an array of integer values has been declared as follows and has been initialized. int[] arr = new int[10]; Which of the following code segments correctly interchanges the value of arr[0] and arr[5] ? A arr[0] = 5; arr[5] = 0; B arr[0] = arr[5]; arr[5] = arr[0]; C int k = arr[5]; arr[0] = arr[5]; arr[5] = k; D int k = arr[0]; arr[0] = arr[5]; arr[5] = k; E int k = arr[5]; arr[5] = arr[0]; arr[0] = arr[5];

int k = arr[0]; arr[0] = arr[5]; arr[5] = k;

Consider the following method that is intended to modify its parameter nameList by replacing all occurrences of name with newValue. public void replace(ArrayList<String> nameList, String name, String newValue) { for (int j = 0; j < nameList.size(); j++) { if ( /* expression */ ) { nameList.set(j, newValue); } } } Which of the following can be used to replace /* expression */ so that replace will work as intended? A nameList.get(j).equals(name) B nameList.get(j) == name C nameList.remove(j) D nameList[j] == name E nameList[j].equals(name)

nameList.get(j).equals(name)

Consider the following code segment. int num1 = 0; int num2 = 3; while ((num2 != 0) && ((num1 / num2) >= 0)) { numl = numl + 2; num2 = num2 - 1; } What are the values of numl and num2 after the while loop completes its execution? A numl = 0, num2 = 3 B num1 = 8, num2 = -1 C numl = 4, num2 = 1 D num1 = 6, num2 = 0 E The loop will never complete its execution because a division by zero will generate an ArithmeticException.

num1 = 6, num2 = 0

Assume that the following variable declarations have been made. double d = Math.random(); double r; Which of the following assigns a value to r from the uniform distribution over the range 0.5 ≤ r < 5.5 ? A r = d + 0.5; B r = d + 0.5 * 5.0; C r = d * 5.0; D r = d * 5.0 + 0.5; E r = d * 5.5;

r = d * 5.0 + 0.5;

Consider the following method, between, which is intended to return true if x is between lower and upper, inclusive, and false otherwise. // precondition: lower <= upper // postcondition: returns true if x is between lower and upper, // inclusive; otherwise, returns false public boolean between(int x, int lower, int upper) { /* missing code */ } Which of the following can be used to replace /* missing code */ so that between will work as intended? A return (x <= lower) && (x >= upper); B return (x <= lower) || (x >= upper); C return lower <= x <= upper; D return (x >= lower) && (x <= upper); E return (x >= lower) || (x <= upper);

return (x >= lower) && (x <= upper);

Consider the following static method. public static int calculate(int x) { x = x + x; x = x + x; x = x + x; return x; } Which of the following can be used to replace the body of calculate so that the modified version of calculate will return the same result as the original version for all x ? A return 3 + x; B return 3 * x; C return 4 * x; D return 6 * x; E return 8 * x;

return 8 * x;

Consider the following static method. public static int calculate(int x) { x = x + x; x = x + x; x = x + x; return x; } Which of the following can be used to replace the body of calculate so that the modified version of calculate will return the same result as the original version for all x ? A return 2 * x; B return 4 * x; C return 8 * x; D return 3 * calculate(x); E return x + calculate(x - 1);

return 8 * x;

Consider the following declarations. int valueOne, valueTwo; Assume that valueOne and valueTwo have been initialized. Which of the following evaluates to true if valueOne and valueTwo contain the same value? A valueOne.equals((Object) valueTwo) B valueOne == valueTwo C valueOne.compareTo((Object) valueTwo) == 0 D valueOne.compareTo(valueTwo) == 0 E valueOne.equals(valueTwo)

valueOne == valueTwo


Ensembles d'études connexes

Factors and Multiples Pre-Test and Quiz

View Set

Chapter 39: Oxygenation and Perfusion

View Set

CH8 Final exam short answer practice questions

View Set