uc scout ap comp sci midterm 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

(x && y) || ! (x && y) The result of evaluating the expression above is best described as

always true

What is printed as a result of executing the following code segment? List<Integer> list1 = new ArrayList<Integer>(); list1.add(new Integer(10)); list1.add(new Integer(27)); list1.add(new Integer(5)); list1.set(2, new Integer(0)); list1.add(2, new Integer(1)); list1.add(new Integer(33)); System.out.println(list1.isEmpty());

false

In the code segment below, myList is an ArrayList of integers. The code segment is intended to remove all elements with the value 0 from myList. int j = 0; while (j < myList.size()) { if (myList.get(j) == 0) { myList.remove(j); } j++; } The code segment does not always work as intended. For which of the following lists does the code segment NOT produce the correct result?

{1, 0, 0, 2}

Assume that a and b have been defined and initialized as int values. The expression! ( ! (a != b ) && (b > 7) )is equivalent to which of the following?

(a != b) || (b <=7)

Assume that myList is an ArrayList that has been correctly constructed and populated with objects.Which of the following expressions produces a valid random index for myList ?

(int)( Math.random() * myList.size() )

Consider the following code segment. int[] arr = {7, 2, 5, 3, 0, 10}; for (int k = 0; k < arr.length - 1; k++) { if (arr[k] > arr[k + 1]) System.out.print(k + " " + arr[k] + " "); } What will be printed as a result of executing the code segment?

0 7 2 5 3 3

public static int mystery(int n) { int x = 1; int y = 1; // Point A while (n > 2) { x = x + y; // Point B y = x - y; n--; } // Point C return x; } 19. What value is returned as a result of the call mystery(7) ?

13

Given the following code segment, which of the following will cause an infinite loop? Assume that temp is an int variable initialized to be greater than zero and that a is an array of ints. for ( int k = 0; k < a.length; k++ ) { while ( a[ k ] < temp ) { a[ k ] *= 2; } }

Whenever a includes a value that is less than or equal to zero.

Consider the following instance variable and method. private List<String> animals;public void manipulate() { for (int k = animals.size() - 1; k > 0; k--) { if (animals.get(k).substring(0, 1).equals("b")) { animals.add(animals.size() - k, animals.remove(k)); } } } Assume that animals has been instantiated and initialized with the following contents. ["bear", "zebra", "bass", "cat", "koala", "baboon"] What will the contents of animals be as a result of calling manipulate?

["bear", "zebra", "bass", "cat", "koala", "baboon"]

Which of the following could be used to replace / missing code / so that findClosest will work asintended?

Math.abs(num - val) < minDiff

How many stars are output when the following code is executed? for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) System.out.println("*"); }

25

Consider the following code segment. int[] arr = {1, 2, 3, 4, 5, 6, 7}; for (int k = 3; k < arr.length - 1; k++) arr[k] = arr[k + 1]; Which of the following represents the contents of arr as a result of executing the code segment?

{1, 2, 3, 5, 6, 7, 7}

In the code segment below, numList is an ArrayList of integers that is sorted in descending order. The code segment is intended to insert the integer value val into numList so that numList is still sorted in descending order. int j = 0; while (val != numList.get(j)) { j++; } numList.add(j, val); The code segment does not always work as intended. Assuming that numList has been initialized to {3, 2, 1, 0}, for which value of val does the code segment NOT produce the expected result?

4

int[] values = {5, 2, 1, 3, 8}; mystery(values); for (int v : values) System.out.print(v + " "); System.out.println(); What is printed as a result of executing the code segment?

5 7 8 11 19

Consider the following code segment: String[] myPets; myPets = new String[6]; myPets[0] = "Sharkey"; myPets[1] = "MyGuy"; myPets[2] = "Rio"; int r = myPets.length; r now has the value:

6

Consider the following method. public static int mystery(int[] arr) { int x = 0; for (int k = 0; k < arr.length; k = k + 2) x = x + arr[k]; return x; } Assume that the array nums has been declared and initialized as follows: int[] nums = {3, 6, 1, 0, 1, 4, 2}; What value will be returned as a result of the call mystery(nums)?

7

Consider the following method, inCommon, which takes two Integer ArrayList parameters. The method returns true if the same integer value appears in both lists at least one time, and false otherwise. public static boolean inCommon(ArrayList<Integer> a, ArrayList<Integer> b) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) // Line 5 { if (a.get(i).equals(b.get(j))) { return true; } } } return false; } Which of the following best explains the impact to the inCommon method when line 5 is replaced by for (int j = b.size() - 1; j > 0; j--) ?

After the change, the method will never check the first element in list b.

Consider the following code segment. ArrayList<Integer> nums = new ArrayList<Integer>();nums.add(new Integer(37)); nums.add(new Integer(3)); nums.add(new Integer(0)); nums.add(1, new Integer(2)); nums.set(0, new Integer(1)); nums.remove(2); System.out.println(nums); What is printed as a result of executing the code segment?

[1, 2, 0]

There is a method called checkString that determines whether a string is the same forwards and backwards. The following data sets can be used for testing the method. What advantage does Data Set 2 have over Data Set 1? Data Set 1 Data Set 2 aba bcb abba bcd aBa

Data Set 2 contains one string which should return true and one that should return false.

At a certain high school students receive letter grades based on the following scale.Integer Score --> Letter Grade93 or above --> AFrom 84 to 92 inclusive --> BFrom 75 to 83 inclusive --> CBelow 75 --> FWhich of the following code segments will assign the correct string to grade for a given integer score?I. if (score >= 93)grade = "A";if (score >= 84 && score <= 92)grade = "B";if (score >= 75 && score <= 83)grade = "C";if (score < 75)grade = "F";II. if (score >= 93)grade = "A";if (84 <= score <= 92)grade = "B";if (75 <= score <= 83)grade = "C";if (score < 75)grade = "F";III. if (score >= 93)grade = "A";else if (score >= 84)grade = "B";else if (score >= 75)grade = "C";elsegrade = "F";

I and III only

Consider the following method, isSorted, which is intended to return true if an array of integers is sorted in nondecreasing order and to return false otherwise. /** @param data an array of integers * @return true if the values in the array appear in sorted (nondecreasing) order */ public static boolean isSorted(int[] data) { / missing code / } Which of the following can be used to replace / missing code / so that isSorted will work as intended?(B) I. for (int k = 0; k < data.length; k++) { if (data[k] > data[k + 1]) return false; } return true; II. for (int k = 1; k < data.length; k++) { if (data[k - 1] > data[k]) return false; } return true; III. for (int k = 0; k < data.length - 1; k++) { if (data[k] > data[k + 1]) return false; else return true; } return true;

I only

Consider the following instance variable and methods. You may assume that data has been initialized with length > 0. The methods are intended to return the index of an array element equal to target, or -1 if no such element exists. private int[] data; public int seqSearchRec(int target) { return seqSearchRecHelper(target, data.length - 1); } private int seqSearchRecHelper(int target, int last) { // Line 1 if (data[last] == target) return last; else return seqSearchRecHelper(target, last - 1); } For which of the following test cases will the call seqSearchRec(5) always result in an error?

II only

The following method attempts to perform an insertion sort: public void sort() 0: { 1: for (int i = 1; i < a.length; i++) 2: { 3: int next = a[i]; 4: // Move all larger elements to the right 5: int j = i; 6: while (j > 0 && a[j - 1] > next) 7: { 8: a[j-1] = a[j]; 9: j--; 10: } 11: // Insert the element 12: a[j] = next; 13: } 14: }

In line 8 it should be a[j] = a[j-1];

Consider the following method countNegatives, which searches an ArrayList of Integer objects and returns the number of elements in the list that are less than 0. public static int countNegatives(ArrayList<Integer> arr) { int count = 0; for (int j = 0; j < arr.size(); j++) { // Line 4 If (arr.get(j) < 0) { count++; } } return count; } Which of the following best explains the impact to the countNegatives method when, in line 4, j < arr.size() is replaced with j <= arr.size() - 1 ?

It has no impact on the behavior of the method.

Line 1: Book[] books = new Book[2]; Line 2: books[0] = new AudioBook(100, 300, "The Jungle"); Line 3: books[1] = new Book(400, "Captains Courageous"); Line 4: System.out.println(books[0].pagesPerMinute()); Line 5: System.out.println(books[0].toString()); Line 6: System.out.println(books[0].length()); Line 7: System.out.println(books[1].toString()); Which of the following best explains why the code segment will not compile?

Line 4 will not compile because variables of type Book may only call methods in the Book class.

Consider the following method public static int mystery (int n). { int x = 1; int y = 1; // Point A while (n 2) { x = x + y; // Point B y = x - y; } // Point C return x; }

N wil always be greater than 2 at //Point B.

Consider the following method. public static void arrayMethod(int nums[]) { int j = 0; int k = nums.length - 1; while (j < k) { int x = nums[j]; nums[j] = nums[k]; nums[k] = x; j++; k--; } } Which of the following describes what the method arrayMethod() does to the array nums?

The contents of the array nums are reversed.

Consider the method seqSearch, which implements a sequential search algorithm. public int seqSearch(int[] arr, int target) { for (int j = 0; j < arr.length; j++) { if (arr[j] == target) { return j; } } return -1; } Consider another method, seqSearch2, which modifies seqSearch to use an enhanced for loop. public int seqSearch2(int[] arr, int target) { for (int j : arr) { if (j == target) { return j; } } return -1; } Which of the following best describes the difference in the behavior of seqSearch2 relative to seqSearch as a result of the modification?

The modification in seqSearch2 will cause the value of target to be returned instead of the index of target in cases where target appears in arr.

Consider the following search method. public static int search(int[] arr, int target) { int result = -1; for (int j = 0; j < arr.length; j++) { if (arr[j] == target) { result = j; // Line 8 } } return result; } Which of the following describes the effect of replacing the statement in line 8 of the method with result = arr[j]; ?

The modified method will return target if target appears in arr and will return -1 otherwise.

Consider the method sequentialSearch, which takes an ArrayList of Integer elements and an int value as parameters and returns the index of the first appearance of the target value in the list or -1 if the target value does not appear in the list. public static int sequentialSearch(ArrayList<Integer> elements, int target) { for (int j = 0; j < elements.size(); j++) // Line 3 { if (elements.get(j) == target) { return j; } } return -1; } Which of the following explains how replacing Line 3 with for (int j = (elements.size() - 1); j >= 0; j--) will affect the behavior of sequentialSearch?

The modified method will return the index of the last appearance of the target value in the list, or -1 if the target value does not appear in the list.

What is printed as a result of executing the following code segment? List<Integer> list1 = new ArrayList<Integer>(); list1.add(new Integer(10)); list1.add(new Integer(27)); list1.add(new Integer(5)); list1.set(2, new Integer(0)); list1.add(2, new Integer(1)); list1.add(new Integer(33)); System.out.println(list1);

[10, 27, 1, 0, 33]

Consider the following code segment. ArrayList<Integer> oldList = new ArrayList(); oldList.add(100); oldList.add(200); oldList.add(300); oldList.add(400); ArrayList<Integer> newList = new ArrayList(); newList.add(oldList.remove(1)); newList.add(oldList.get(2)); System.out.println(newList); What, if anything, is printed as a result of executing the code segment?

[200, 400]

Consider the following method. public ArrayList mystery(int n) { ArrayList seq = new ArrayList(); for (int k = 1; k <= n; k++) seq.add(new Integer(k * k + 3)); return seq; } Which of the following is printed as a result of executing the following statement? System.out.println(mystery(6));

[4, 7, 12, 19, 28, 39]

Consider the following method. public ArrayList mystery(int n) { ArrayList seq = new ArrayList(); for (int k = 1; k <= n; k++) { seq.add(new Integer(k * k + 3)); } return seq; } Which of the following is printed as a result of executing the following statement? System.out.println(mystery(6));

[4, 7, 12, 19, 28, 39]

Consider the following code segment. ArrayList<Double> conditionRating = new ArrayList<Double>(); conditionRating.add(9.84); conditionRating.add(8.93); conditionRating.add(7.65); conditionRating.add(6.24); conditionRating.remove(2); conditionRating.set(2, 7.63); System.out.println(conditionRating); What is printed when this code segment is executed?

[9.84, 8.93, 7.63]

Consider the following code segment. List<String> animals = new ArrayList<String>(); animals.add("dog"); animals.add("cat"); animals.add("snake"); animals.set(2, "lizard"); animals. add(1, "fish"); animals.remove(3); System.out.println(animals); What is printed as a result of executing the code segment?

[dog, fish, cat]

In the code segment below, assume that the int array numArr has been properly declared and initialized. The code segment is intended to reverse the order of the elements in numArr. For example, if numArr initially contains {1, 3, 5, 7, 9}, it should contain {9, 7, 5, 3, 1} after the code segment executes. / missing loop header / { int temp = numArr[k]; numArr[k] = numArr[numArr.length - k - 1]; numArr[numArr.length - k - 1] = temp; } Which of the following can be used to replace / missing loop header / so that the code segment works as intended?

for (int k = 0; k < numArr.length / 2; k++)

Consider the following code segment.for( int k = 1; k <= 100; k++)if ( ( k % 4 ) == 0 )System.out.println( k );Which of the following code segments will produce the same output as the code segment above?

for(int k = 4;k <= 100; k = k + 4)System.out.println(k);

Consider the following code segment. ArrayList<String> animals = new ArrayList<>(); animals.add("fox"); animals.add(0, "squirrel"); animals.add("deer"); animals.set(2, "groundhog"); animals.add(1, "mouse"); System.out.println(animals.get(2) + " and " + animals.get(3)); What is printed as a result of executing the code segment?

fox and groundhog

What all gets printed when the following method is run? public static void test() { for(int i = 0; i < 2; i++) { for(int j = 2; j>= 0; j--) { if(i != j) { System.out.println("i=" + i + " j="+j); } } } }

i=0 j=2 i=0 j=1 i=1 j=2 i=1 j=0

private int[] data; public int seqSearchRec(int target) { return seqSearchRecHelper(target, data.length - 1); } private int seqSearchRecHelper(int target, int last) { // Line 1 if (data[last] == target) return last; else return seqSearchRecHelper(target, last - 1); } Which of the following should be used to replace // Line 1 in seqSearchRecHelper() so that seqSearchRec() will work as intended?

if (last < 0) return -1;

Consider the following method. public void changeIt(int[] arr, int index, int newValue) { arr[index] += newValue; } Which of the following code segments, if located in a method in the same class as changeIt, will cause the array myArray to contain {0, 5, 0, 0} ?

int[] myArray = new int[4]; changeIt(myArray, 1, 5);

Consider the following incomplete method that is intended to return an array that contains the contents of its first array parameter followed by the contents of its second array parameter. public static int[] append(int[] a1, int[] a2) { int[] result = new int[a1.length + a2.length]; for (int j = 0; j < a1.length; j++) { result[j] = a1[j]; } for (int k = 0; k < a2.length; k++) { result[ / index / ] = a2[k]; } return result; } Which of the following expressions can be used to replace / index / so that append will work as intended?

k + a1.length

Consider the following method.public int compute(int n, int k){int answer = 1;for (int i = 1; i <= k; i++) answer *= n;return answer;}Which of the following represents the value returned as a result of the call compute(n, k) ?

n^k

Which of the following should replace / missing code / so that almostEqual will work as intended?

return Math.abs(d1 - d2) <= tolerance;

Consider the following code segment: if(!somethingIsTrue()) return false; else return true; Which one of the following statements would be an accurate replacement for this code?

return somethingIsTrue();

Consider the following method that is intended to return the sum of the elements in the array key. public static int sumArray(int[] key) { int sum = 0; for (int i = 1; i <= key.length; i++) { / missing code / } return sum; } Which of the following statements should be used to replace / missing code / so that sumArray will work as intended?

sum += key[i - 1];


Conjuntos de estudio relacionados

Adverse and Emergency Conditions

View Set

Chapter 2 Debits and Credits Mortuary Management

View Set

Physics Concepts: Chapters 15, 16, 17 & 18

View Set

Chapter 4: From Concepts To Models Review

View Set

Promulgated Contracts, Forms, and Addenda

View Set

CH 15, 16) The Term Structure of Interest Rates and Managing Bond Portfolios

View Set

A Mid-Summer's Night Dream Demetrius' lines Act 1,2,3,4,5

View Set

test three multiple choice (69 & 71)

View Set