array & arraylists mcq

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

The following incomplete method is intended to sort its array parameter arr in increasing order. // postcondition: arr is sorted in increasing order public static void sortArray(int[] arr) { int j, k; for (j = arr.length - 1; j > 0; j--) { int pos = j; for ( /* missing code */ ) { if (arr[k] > arr[pos]) { pos = k; } } swap(arr, j, pos); } } Assume that swap(arr, j, pos) exchanges the values of arr[j] and arr[pos]. Which of the following could be used to replace /* missing code */ so that executing the code segment sorts the values in array arr? A k = j - 1; k > 0; k-- B k = j - 1; k >= 0; k-- C k = 1; k < arr.length; k++ D k = 1; k > arr.length; k++ E k = 0; k <= arr.length; k++

k = j - 1; k >= 0; k--

Consider the mode method, which is intended to return the most frequently occurring value (mode) in its int[] parameter arr. For example, if the parameter of the mode method has the contents {6, 5, 1, 5, 2, 6, 5}, then the method is intended to return 5. /** Precondition: arr.length >= 1 */ public static int mode(int[] arr) { int modeCount = 1; int mode = arr[0]; for (int j = 0; j < arr.length; j++) { int valCount = 0; for (int k = 0; k < arr.length; k++) { if ( /* missing condition 1 */ ) { valCount++; } } if ( /* missing condition 2 */ ) { modeCount = valCount; mode = arr[j]; } } return mode; } Which of the following can replace /* missing condition 1 */ and /* missing condition 2 */ so the code segment works as intended? A /* missing condition 1 *//* missing condition 2 */ arr[j] == arr[k], valCount > modeCount B /* missing condition 1 *//* missing condition 2 */ arr[j] == arr[k], modeCount > valCount C /* missing condition 1 *//* missing condition 2 */ arr[j] != arr[k], valCount > modeCount D /* missing condition 1 *//* missing condition 2 */ arr[j] != arr[k], modeCount > valCount E /* missing condition 1 *//* missing condition 2 */ arr[j] != arr[k], modeCount != valCount

/* missing condition 1 *//* missing condition 2 */ arr[j] == arr[k], valCount > modeCount

Consider the following method. 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) ? A 5 B 6 C 7 D 10 E 17

7

Consider the following instance variable nums and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended.For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10,10, 10, 15, 10, 10], the call findLongest (10) should return 3, the length of the longest consecutive block of 10s. The method findLongest does not work as intended. Which of the following best describes the value returned by a call to findLongest ? A It is the length of the shortest consecutive block of the value target in nums. B It is the length of the array nums. C It is the number of occurrences of the value target in nums. D It is the length of the first consecutive block of the value target in nums. E It is the length of the last consecutive block of the value target in nums.

It is the number of occurrences of the value target in nums.

Consider a shuffle method that is intended to return a new array that contains all the elements from nums, but in a different order. Let n be the number of elements in nums. The shuffle method should alternate the elements from nums [0] ... nums[n / 2 - 1] with the elements from nums[n / 2] ...nums[n - 1], as illustrated in the following examples. The following implementation of the shuffle method does not work as intended. Which of the following best describes the problem with the given implementation of the shuffle method? A Executing shuffle may cause an ArrayIndexOutOfBoundsException. B The first element of the returned array (result [0] ) may not have the correct value. C The last element of the returned array (result [result.length − 1] ) may not have the correct value. D One or more of nums [0] ... nums [nums.length / 2 − 1] may have been copied to the wrong position(s) in the returned array. E One or more of nums [nums.length / 2] ... nums[nums.length − 1] may have been copied to the wrong position(s) in the returned array.

The last element of the returned array (result [result.length − 1] ) may not have the correct value.

The following question refer to the following information. Consider the following data field and method. The method removeDups is intended to remove all adjacent duplicate numbers from myData, but does not work as intended. private ArrayList myData; public void removeDups () { int k = 1; while (k < myData.size()) { if (myData.get(k).equals(myData.get(k - 1))) { myData.remove(k); } k++; } } For example, if myData has the values 3 3 4 4 4 8 7 7 7, after calling removeDups, myData should have the values 3 4 8 7. Which of the following best describes how to fix the error so that removeDups works as intended? A k should be initialized to 0 at the beginning of the method. B The while condition should be (k < myData.size() - 1). C The if test should be (myData.get(k).equals(myData.get(k + 1))). D The body of the if statement should be: myData.remove(k - 1); E There should be an else before the statement k++;

There should be an else before the statement k++;

Consider the following method. public static String[] strArrMethod(String[] arr) { String[] result = new String[arr.length]; for (int j = 0; j < arr.length; j++) { String sm = arr[j]; for (int k = j + 1; k < arr.length; k++) { if (arr[k].length() < sm.length()) { sm = arr[k]; // Line 12 } } result[j] = sm; } return result; } Consider the following code segment. String[] testOne = {"first", "day", "of", "spring"}; String[] resultOne = strArrMethod(testOne); What are the contents of resultOne when the code segment has been executed? A {"day", "first", "of", "spring"} B {"of", "day", "first", "spring"} C {"of", "day", "of", "spring"} D {"of", "of", "of", "spring"} E {"spring", "first", "day", "of"}

{"of", "of", "of", "spring"}

Consider the following method. public static void methodX(int[] values) { for (int j = 0; j < values.length - 1; j++) { for (int k = j + 1; k < values.length; k++) { if (values[k] < values[j]) { values[j] = values[k]; } } } } The following code segment appears in another method of the same class. int[] numbers = {45, 1, 56, 10}; methodX(numbers); Which of the following represents the contents of the array numbers after the code segment is executed? A {1, 1, 10, 10} B {1, 10, 45, 56} C {45, 1, 56, 10} D {45, 45, 56, 45} E {56, 45, 10, 1}

{1, 1, 10, 10}

Consider the following sort method. This method correctly sorts the elements of array data into increasing order. Assume that sort is called with the array {6, 3, 2, 5, 4, 1}. What will the value of data be after three passes of the outer loop (i.e., when j = 2 at the point indicated by / * End of outer loop * /) ? A {1, 2, 3, 4, 5, 6} B {1, 2, 3, 5, 4, 6} C {1, 2, 3, 6, 5, 4} D {1, 3, 2, 4, 5, 6} E {1, 3, 2, 5, 4, 6}

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

Consider the following code segment from an insertion sort program. Assume that array arr has been defined and initialized with the values {5, 4, 3, 2, 1}. What are the values in array arr after two passes of the for loop (i.e., when j = 2 at the point indicated by / * end of for loop * / ) ? A {2, 3, 4, 5, 1} B {3, 2, 1, 4, 5} C {3, 4, 5, 2, 1} D {3, 5, 2, 3, 1} E {5, 3, 4, 2, 1}

{3, 4, 5, 2, 1}

Consider the following method. public static void sort(String[] arr) { for (int pass = arr.length - 1; pass >= 1; pass--) { String large = arr[0]; int index = 0; for (int k = 0; k <= pass; k++) { if ((arr[k].compareTo(large)) > 0) { large = arr[k]; index = k; } } arr[index] = arr[pass]; arr[pass] = large; } } Assume arr is the following array. "Ann""Mike""Walt""Lisa""Shari""Jose""Mary""Bill" What is the intermediate value of arr after two iterations of the outer for loop in the call sort(arr)? A "Ann""Mike""Walt""Lisa""Shari""Jose""Mary""Bill" B "Ann""Mike""Lisa""Shari""Jose""Mary""Bill""Walt" C "Ann""Bill""Jose""Lisa""Mary""Mike""Shari""Walt" D "Ann""Mike""Bill""Lisa""Mary""Jose""Shari""Walt" E "Walt""Shari""Ann""Lisa""Mike""Jose""Mary""Bill"

"Ann""Mike""Bill""Lisa""Mary""Jose""Shari""Walt"

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 problem of finding the maximum value in an array of integers. The following code segments are proposed solutions to the problem. Assume that the variable arr has been defined as an array of int values and has been initialized with one or more values. Which of the code segments will always correctly assign the maximum element of the array to the variable max ? A I only B II only C III only D II and III only E I, II, and III

I, II, and III

Consider the following instance variable and method. Which of the following is the best postcondition for checkArray ? A Returns the index of the first element in array array whose value is greater than array [loc] B Returns the index of the last element in array array whose value is greater than array [loc] C Returns the largest value in array array D Returns the index of the largest value in array array E Returns the index of the largest value in the second half of array array

Returns the index of the largest value in array array

The following question refer to the following information. Consider the following data field and method. The method removeDups is intended to remove all adjacent duplicate numbers from myData, but does not work as intended. private ArrayList myData; public void removeDups () { int k = 1; while (k < myData.size()) { if (myData.get(k).equals(myData.get(k - 1))) { myData.remove(k); } k++; } } For example, if myData has the values 3 3 4 4 4 8 7 7 7, after calling removeDups, myData should have the values 3 4 8 7. Assume that myData has the following values. 2 7 5 5 5 5 6 6 3 3 3 Which of the following represents myData after the incorrect removeDups is executed? A 2 7 5 6 3 B 2 7 5 6 3 3 C 2 7 5 5 6 3 3 D 2 7 5 5 5 6 3 3 E 2 7 5 5 5 5 6 6 3 3

2 7 5 5 6 3 3

Consider the following method. public static int mystery(int value) { int sum = 0; int[] arr = {1, 4, 2, 5, 10, 3, 6, 4}; for (int item : arr) { if (item > value) { sum += item; } } return sum; } What value is returned as a result of the call mystery(4) ? A 6 B 15 C 21 D 29 E 35

21

Consider the following method. The following code segment appears in another method in the same class. What is printed as a result of executing the code segment? A 5 2 1 3 8 B 5 7 3 4 11 C 5 7 8 11 19 D 7 3 4 11 8 E Nothing is printed because an ArrayIndexOutOfBoundsException is thrown during the execution of method mystery.

5 7 8 11 19

Consider the following instance variable and method. Which of the following best describes the contents of numbers after the following statement has been executed? int m = mystery(n); A All values in positions 0 through m are less than n. B All values in positions m+1 through numbers.length-1 are less than n. C All values in positions m+1 through numbers.length-1 are greater than or equal to n. D The smallest value is at position m. E The largest value that is smaller than n is at position m.

All values in positions m+1 through numbers.length-1 are greater than or equal to n.

Consider the following method. Assume that nums has been declared and initialized as an array of integer values. Which of the following best describes the value returned by the call mystery(nums) ? A The maximum value that occurs in nums B An index of the maximum value that occurs in nums C The number of times that the maximum value occurs in nums D A value that occurs most often in nums E An index of a value that occurs most often in nums

An index of a value that occurs most often in nums

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

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

II only

Consider the following method. Which of the following describes what the method arrayMethod() does to the array nums? A The array nums is unchanged. B The first value in nums is copied to every location in the array. C The last value in nums is copied to every location in the array. D The method generates an ArrayIndexOutOfBoundsException. E The contents of the array nums are reversed.

The contents of the array nums are reversed.

Consider the following instance variable and method. Method findMax is intended to return the largest value in the array arr. Which of the following best describes the conditions under which the method findMax will not work as intended? A The largest value in arr occurs only once and is in arr[0]. B The largest value in arr occurs only once and is in arr[arr.length - 1]. C The largest value in arr is negative. D The largest value in arr is zero. E The largest value in arr occurs more than once.

The largest value in arr is negative.

Consider the following instance variable and method. Assume that animals has been instantiated and initialized with the following contents. What will the contents of animals be as a result of calling manipulate? A ["baboon", "zebra", "bass", "cat", "bear", "koala"] B ["bear", "zebra", "bass", "cat", "koala", "baboon"] C ["baboon", "bear", "zebra", "bass", "cat", "koala"] D ["bear", "baboon", "zebra", "bass", "cat", "koala"] E ["zebra", "cat", "koala", "baboon", "bass", "bear"]

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

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. Which of the following expressions can be used to replace /* index */ so that append will work as intended? A j B k C k + a1.length - 1 D k + a1.length E k + a1.length + 1

k + a1.length

Consider the following instance variable and method. Assume that numbers has been initialized with the following values. {17, 34, 21, 42, 15, 69, 48, 25, 39} Which of the following represents the order of the values in numbers as a result of the call mystery(3)? A {17, 20, 21, 42, 45, 69, 48, 51, 39} B {17, 20, 23, 26, 29, 32, 35, 38, 41} C {17, 37, 21, 42, 18, 69, 48, 28, 39} D {20, 23, 21, 42, 45, 69, 51, 54, 39} E {20, 34, 21, 45, 15, 69, 51, 25, 39}

{17, 20, 21, 42, 45, 69, 48, 51, 39}

The following incomplete method is intended to return the largest integer in the array numbers. // precondition: numbers.length > 0 public static int findMax(int[]numbers) { int posOfMax = O; for (int index = 1; index < numbers.length; index++) { if ( /*condition*/ ) { /* statement */ } } return numbers[posOfMax]; } Which of the following can be used to replace /* condition */ and /* statement */ so that findMax will work as intended? A /* condition */ /* statement */ numbers[index] > numbers[posOfMax], posOfMax = numbers[index]; B /* condition */ /* statement */ numbers[index] > numbers[posOfMax], posOfMax = index; C /* condition */ /* statement */ numbers[index] > posOfMax, posOfMax = numbers[index]; D /* condition */ /* statement */ numbers[index] < posOfMax, posOfMax = numbers[index]; E /* condition */ /* statement */ numbers[index] < numbers[posOfMax], posOfMax = index;

/* condition */ /* statement */ numbers[index] > numbers[posOfMax], posOfMax = index;

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;

Consider the following code segment. What will be printed as a result of executing the code segment? A 0 2 2 3 3 0 B 0 7 2 5 3 3 C 0 7 2 5 5 10 D 1 7 3 5 4 3 E 7 2 5 3 3 0

0 7 2 5 3 3

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

The following sort method correctly sorts the integers in elements into ascending order Which of the following changes to the sort method would correctly sort the integers in elements into descending order? A I only B II only C I and II only D I and III only E I, II, and III

I and III only

Consider the following incomplete method that is intended to return a string formed by concatenating elements from the parameter words. The elements to be concatenated start with startIndex and continue through the last element of words and should appear in reverse order in the resulting string. For example, the following code segment uses a call to the concatWords method. When the code segment is executed, the string "CarHouseGorilla" is printed.The following three code segments have been proposed as replacements for / * missing code * /. Which of these code segments can be used to replace /* missing code */ so that concatWords will work as intended? A I only B II only C III only D I and II E II and III

II and III

Consider the static method selectSort shown below. Method selectSort is intended to sort an array into increasing order; however, it does not always work as intended. // precondition: numbers.length > 0 // postcondition: numbers is sorted in increasing order public static void selectSort(int[] numbers) { int temp; Line 1: for (int j = 0; j < numbers.length - 1; j++) { Line 2: int pos = 0; Line 3: for (int k = j + 1; k < numbers.length; k++) { Line 4: if (numbers[k] < numbers[pos]) { Line 5: pos = k; } } temp = numbers[j]; numbers[j] = numbers[pos]; numbers[pos] = temp; } } Which of the following changes should be made so that selectSort will work as intended? A Line 1 should be changed to for (int j = 0; j < numbers.length - 2; j++) B Line 2 should be changed to int pos = j; C Line 3 should be changed to for (int k = 0; k < numbers.length; k++) D Line 4 should be changed to if (numbers[k] > numbers[pos]) E Line 5 should be changed to k = pos;

Line 2 should be changed to int pos = j;

Consider the following instance variable and incomplete method. The method is intended to return a string from the array words that would be last alphabetically. private String[] words; public String findLastWord() { /* missing implementation */ } Assume that words has been initialized with one or more strings containing only lowercase letters. Which of the following code segments can be used to replace /* missing implementation */ so that findLastWord will work as intended? A int maxIndex = 0; for (int k = 0; k < words.length; k++) { if (words[k].compareTo(maxIndex) > 0) { maxIndex = k; } } return words[maxIndex]; B int maxIndex = 0; for (int k = 1; k <= words.length; k++) { if (words[k].compareTo(words[maxIndex]) > 0) { maxIndex = k; } } return words[maxIndex]; C int maxIndex = 0; for (int k = 1; k < words.length; k++) { if (words[k].compareTo(words[maxIndex]) > 0) { maxIndex = k; } } return maxIndex; D String maxWord = words[0]; for (int k = 1; k < words.length; k++) { if (words[k].compareTo(maxWord) > 0) { maxWord = k; } } return maxWord; E String maxWord = words[0]; for (int k = 1; k < words.length; k++) { if (words[k].compareTo(maxWord) > 0) { maxWord = words[k]; } } return maxWord;

String maxWord = words[0]; for (int k = 1; k < words.length; k++) { if (words[k].compareTo(maxWord) > 0) { maxWord = words[k]; } } return maxWord;

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

Consider the following correct implementation of the insertion sort algorithm. The insertionSort method correctly sorts the elements of ArrayList data into increasing order. public static void insertionSort(ArrayList<Integer> data) { for (int j = 1; j < data.size(); j++) { int v = data.get(j); int k = j; while (k > 0 && v < data.get(k - 1)) { data.set(k, data.get(k - 1)); /* Statement 1 */ k--; } data.set(k, v); /* Statement 2 */ /* End of outer loop */ } } Assume that insertionSort has been called with an ArrayList parameter that has been initialized with the following Integer objects. [5, 2, 4, 1, 3, 6] What will the contents of data be after three passes of the outside loop (i.e., when j == 3 at the point indicated by /* End of outer loop */) ? A [1, 2, 3, 4, 5, 6] B [1, 2, 3, 5, 4, 6] C [1, 2, 4, 5, 3, 6] D [2, 4, 5, 1, 3, 6] E [5, 2, 1, 3, 4, 6]

[1, 2, 4, 5, 3, 6]

Consider the following class definition. public class Book { private int pages; public int getPages() { return pages; } // There may be instance variables, constructors, and methods not shown. } The following code segment is intended to store in maxPages the greatest number of pages found in any Book object in the array bookArr. Book[] bookArr = { /* initial values not shown */ }; int maxPages = bookArr[0].getPages(); for (Book b : bookArr) { /* missing code */ } Which of the following can replace /* missing code */ so the code segment works as intended? A if (b.pages > maxPages) { maxPages = b.pages; } B if (b.getPages() > maxPages) { maxPages = b.getPages(); } C if (Book[b].pages > maxPages) { maxPages = Book[b].pages; } D if (bookArr[b].pages > maxPages) { maxPages = bookArr[b].pages; } E if (bookArr[b].getPages() > maxPages) { maxPages = bookArr[b].getPages(); }

if (b.getPages() > maxPages) { maxPages = b.getPages(); }

Consider the following code segment. Which of the following represents the contents of arr as a result of executing the code segment? A {1, 2, 3, 4, 5, 6, 7} B {1, 2, 3, 5, 6, 7} C {1, 2, 3, 5, 6, 7, 7} D {1, 2, 3, 5, 6, 7, 8} E {2, 3, 4, 5, 6, 7, 7}

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


Set pelajaran terkait

Examen Primer Parcial de Metodología (1 cuatrimestre)

View Set

Nervous System, Part 2 Edpuzzle:

View Set

Weak Points: AWS Cost Management

View Set

Spanish II SPEAKING EXAM: 105-133

View Set