Unit 6 and 7 MCQ

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Consider the following code segment. ArrayList<String> arrList = new ArrayList<String>(); arrList.add("A"); arrList.add("B"); arrList.add("C"); arrList.add("D"); for (int i = 0; i < arrList.size(); i++) { System.out.print(arrList.remove(i)); } What, if anything, is printed as a result of executing the code segment? A AC B BD C ABC D ABCD E Nothing is printed.

A

Consider the following code segment. int[] arr = {1, 2, 3, 4, 5}; Which of the following code segments would correctly set the first two elements of array arr to 10 so that the new value of array arr will be {10, 10, 3, 4, 5} ? A arr[0] = 10; arr[1] = 10; B arr[1] = 10; arr[2] = 10; C arr[0, 1] = 10; D arr[1, 2] = 10; E arr = 10, 10, 3, 4, 5;

A

Consider the following method, which is intended to return the index of the first negative integer in a given array of integers. public int positionOfFirstNegative(int[] values) { int index = 0; while (values[index] >= 0) { index++; } return index; } What precondition is needed on the values array so that the method will work as intended? A The array values must contain at least one negative integer. B The array values must contain at least one nonnegative integer. C The array values must contain at least one positive integer. D No precondition is needed. The method will never work as intended. E No precondition is needed. The method will always work as intended.

A

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? A for (int k = 0; k < numArr.length / 2; k++) B for (int k = 0; k < numArr.length; k++) C for (int k = 0; k < numArr.length / 2; k--) D for (int k = numArr.length - 1; k >= 0; k--) E for (int k = numArr.length - 1; k >= 0; k++)

A

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? A 4 B 3 C 2 D 1 E 0

A

The array fruits is declared below. String [] fruits = {"apples", "bananas", "cherries", "dates"}; Which of the following code segments will cause an ArrayIndexOutOfBoundsException ? I. for (int i = 0; i <= fruits.length; i++) { System.out.println(fruits[i]); } II. for (int i = 0; i <= fruits.length - 1; i++) { System.out.println(fruits[i]); } III. for (int i = 1; i <= fruits.length; i++) { System.out.println(fruits[i - 1]); } A I only B II only C I and III only D II and III only E I, II, and III

A

Consider the following code segment, which is intended to print the maximum value in an integer array values. Assume that the array has been initialized properly and that it contains at least one element. int maximum = /* missing initial value */; for (int k = 1; k < values.length; k++) { if (values[k] > maximum) { maximum = values[k]; } } System.out.println(maximum); Which of the following should replace /* missing initial value */ so that the code segment will work as intended? A 0 B values[0] C values[1] D Integer.MIN_VALUE E Integer.MAX_VALUE

B

Consider the following code segment. ArrayList<String> syllables = new ArrayList<String>(); syllables.add("LA"); syllables.add(0, "DI"); syllables.set(1, "TU"); syllables.add("DA"); syllables.add(2, syllables.get(0)); syllables.remove(1); System.out.println(syllables.toString()); What is printed as a result of executing the code segment? A [DI, DA, DI] B [DI, DI, DA] C [LA, LA, DA] D [TU, DI, DA] E [TU, TU, DA]

B

Consider the following code segment. ArrayList<String> words = new ArrayList<String>(); words.add("mat"); words.add("new"); words.add("open"); words.add("pet"); int i = 0; while (i < words.size()) { words.remove(i); i++; } System.out.println(words.toString()); What is printed when the code segment is executed? A [] B [new, pet] C [open, pet] D [new, open, pet] E [mat, new, open, pet]

B

Consider the following code segment. int[] arr = {4, 3, 2, 1, 0}; int total = 0; for (int k = 0; k <= total; k++) { if (arr[k] % 2 == 0) { total += arr[k]; } else { total -= arr[k]; } } System.out.print(total); What, if anything, is printed as a result of executing the code segment? A 2 B 1 C 0 D -4 E Nothing is printed because the code segment causes a runtime error.

B

Consider the following statement, which is intended to create an ArrayList named a to store only elements of type Thing. Assume that the Thing class has been properly defined and includes a no-parameter constructor. ArrayList<Thing> a = /* missing code */; Which of the following can be used to replace /* missing code */ so that the statement works as intended? A new Thing() B new ArrayList<Thing>() C new ArrayList(Thing) D new ArrayList(<Thing>) E new ArrayList<>(Thing)

B

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? A The modification has no effect: the modified method will continue to return 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. B 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. C The modified method will throw an ArrayIndexOutOfBoundsException. D The modified method will return -1 regardless of the inputs. E The modified method will not compile.

B

The Fibonacci numbers are a sequence of integers. The first two numbers are 1 and 1. Each subsequent number is equal to the sum of the previous two integers. For example, the first seven Fibonacci numbers are 1, 1, 2, 3, 5, 8, and 13. The following code segment is intended to fill the fibs array with the first ten Fibonacci numbers. The code segment does not work as intended. int[] fibs = new int[10]; fibs[0] = 1; fibs[1] = 1; for (int j = 1; j < fibs.length; j++) { fibs[j] = fibs[j - 2] + fibs[j - 1]; } Which of the following best identifies why the code segment does not work as intended? A In the for loop header, the initial value of j should be 0. B In the for loop header, the initial value of j should be 2. C The for loop condition should be j < fibs.length - 1. D The for loop condition should be j < fibs.length + 1. E The for loop should increment j by 2 instead of by 1.

B

Consider the following class definition. public class Toy { private int yearFirstSold; public int getYearFirstSold() { return yearFirstSold; } /* There may be instance variables, constructors, and other methods not shown. */ } The following code segment, which appears in a class other than Toy, prints the year each Toy object in toyArray was first sold by its manufacturer. Assume that toyArray is a properly declared and initialized array of Toy objects. for (Toy k : toyArray) { System.out.println(k.getYearFirstSold()); } Which of the following could be used in place of the given code segment to produce the same output? I. for (int k = 0; k < toyArray.length; k++) { System.out.println(getYearFirstSold(k)); } II. for (int k = 0; k < toyArray.length; k++) { System.out.println(k.getYearFirstSold()); } III. for (int k = 0; k < toyArray.length; k++) { System.out.println(toyArray[k].getYearFirstSold()); } A I only B II only C III only D I and II E II and III

C

Consider the following code segment. int[] arr = {10, 20, 30, 40, 50}; for(int x = 1; x < arr.length - 1; x++) { arr[x + 1] = arr[x] + arr[x + 1]; } Which of the following represents the contents of arr after the code segment has been executed? A {10, 20, 30, 70, 120} B {10, 20, 50, 90, 50} C {10, 20, 50, 90, 140} D {10, 30, 60, 100, 50} E {10, 30, 60, 100, 150}

C

Consider the following code segment. int[] numbers = {1, 2, 3, 4, 5, 6}; for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } Which of the following for loops produces the same output as the code segment? A for (int x : numbers) { System.out.println(numbers[x]); } B for (int x : numbers) { System.out.println(numbers); } C for (int x : numbers) { System.out.println(x); } D for (numbers : int x) { System.out.println(numbers[x]); } E for (numbers : int x) { System.out.println(x); }

C

Consider the following correct implementation of the insertion sort algorithm. public static void insertionSort(int[] elements) { for (int j = 1; j < elements.length; j++) { int temp = elements[j]; int possibleIndex = j; while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) { elements[possibleIndex] = elements[possibleIndex - 1]; possibleIndex--; } elements[possibleIndex] = temp; // line 12 } } The following declaration and method call appear in a method in the same class as insertionSort. int[] nums = {8, 7, 5, 4, 2, 1}; insertionSort(nums); How many times is the statement elements[possibleIndex] = temp; in line 12 of the method executed as a result of the call to insertionSort ? A 3 B 4 C 5 D 6 E 7

C

Consider the following correct implementation of the selection sort algorithm. public static void selectionSort(int[] elements) { for (int j = 0; j < elements.length - 1; j++) { int minIndex = j; for (int k = j + 1; k < elements.length; k++) { if (elements[k] < elements[minIndex]) { minIndex = k; } } if (j != minIndex) { int temp = elements[j]; elements[j] = elements[minIndex]; elements[minIndex] = temp; // line 19 } } } The following declaration and method call appear in a method in the same class as selectionSort. int[] arr = {30, 40, 10, 50, 20}; selectionSort(arr); How many times is the statement elements[minIndex] = temp; in line 19 of the method executed as a result of the call to selectionSort ? A 1 B 2 C 3 D 4 E 5

C

Consider the following method definition. The method isReversed is intended to return true if firstList and secondList contain the same elements but in reverse order, and to return false otherwise. /** Precondition: firstList.size() == secondList.size() */ public static boolean isReversed(ArrayList<Integer> firstList, ArrayList<Integer> secondList) { for (int j = 0; j < firstList.size() / 2; j++) { if (firstList.get(j) != secondList.get(secondList.size() - 1 - j)) { return false; } } return true; } The method does not always work as intended. For which of the following inputs does the method NOT return the correct value? A When firstList is {1, 3, 3, 1} and secondList is {1, 3, 3, 1} B When firstList is {1, 3, 3, 1} and secondList is {3, 1, 1, 3} C When firstList is {1, 3, 5, 7} and secondList is {5, 5, 3, 1} D When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 1} E When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 3}

C

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]; ? A The modified method will return the index of the first occurrence of target in arr. B The modified method will return the index of the last occurrence of target in arr. C The modified method will return target if target appears in arr and will return -1 otherwise. D The modified method will return -1 if target appears in arr and will return target otherwise. E The modified method will return -1 for all possible inputs.

C

Consider the following statement, which is intended to create an ArrayList named numbers that can be used to store Integer values. ArrayList<Integer> numbers = /* missing code */; Which of the following can be used to replace /* missing code */ so that the statement works as intended? new ArrayList() new ArrayList<Integer> new ArrayList<Integer>() A III only B I and II only C I and III only D II and III only E I, II, and III

C

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? A {0, 1, 2, 3} B {0, 1, 0, 2} C {1, 0, 0, 2} D {1, 2, 3, 0} E {1, 2, 3, 4}

C

Consider the following code segment. ArrayList<Integer> myList = new ArrayList(); for (int i = 0; i < 4; i++) { myList.add(i + 1); } for (int i = 0; i < 4; i++) { if (i % 2 == 0) { System.out.print(myList.get(i) + " "); } } What output is produced as a result of executing the code segment? A 0 1 2 3 B 1 2 3 4 C 0 2 D 1 3 E 2 4

D

Consider the following code segment. ArrayList<Integer> nums = new ArrayList<>(); nums.add(3); nums.add(2); nums.add(1); nums.add(0); nums.add(0, 4); nums.set(3, 2); nums.remove(3); nums.add(2, 0); Which of the following represents the contents of nums after the code segment has been executed? A [2, 4, 3, 2, 0] B [3, 2, 0, 1, 0] C [4, 2, 0, 2, 0] D [4, 3, 0, 2, 0] E [4, 3, 0, 3, 0]

D

Consider the following correct implementation of the insertion sort algorithm. public static void insertionSort(int[] elements) { for (int j = 1; j < elements.length; j++) { int temp = elements[j]; int possibleIndex = j; while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) { elements[possibleIndex] = elements[possibleIndex - 1]; possibleIndex--; // line 10 } elements[possibleIndex] = temp; } } The following declaration and method call appear in a method in the same class as insertionSort. int[] arr = {10, 8, 3, 4}; insertionSort(arr); How many times is the statement possibleIndex--; in line 10 of the method executed as a result of the call to insertionSort ? A 0 B 1 C 4 D 5 E 6

D

Consider the following method, which is intended to return the number of strings of length greater than or equal to 3 in an array of String objects. public static int checkString(String[] arr) { int count = 0; for (int k = 0; k < arr.length; k++) { if (arr[k].length() >= 3) { count++; } } return count; } Which of the following code segments compile without error? I. checkString(new String[]); II. checkString(new String[0]); III. String[] str = {"cat", "dog"};checkString(str); A II only B III only C I and III only D II and III only E I, II, and III

D

Consider the following two code segments. I. int[] arr = {1, 2, 3, 4, 5}; for (int x = 0; x < arr.length; x++) { System.out.print(arr[x + 3]); } II. int[] arr = {1, 2, 3, 4, 5}; for (int x : arr) { System.out.print(x + 3); } Which of the following best describes the behavior of code segment I and code segment II ? A Both code segment I and code segment II will print 45. B Both code segment I and code segment II will print 45678. C Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 45. D Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will print 45678. E Both code segment I and code segment II will cause an ArrayIndexOutOfBoundsException.

D

The code segment below is intended to set the boolean variable duplicates to true if the int array arr contains any pair of duplicate elements. Assume that arr has been properly declared and initialized. boolean duplicates = false; for (int x = 0; x < arr.length - 1; x++) { /* missing loop header */ { if (arr[x] == arr[y]) { duplicates = true; } } } Which of the following can replace /* missing loop header */ so that the code segment works as intended? A for (int y = 0; y <= arr.length; y++) B for (int y = 0; y < arr.length; y++) C for (int y = x; y < arr.length; y++) D for (int y = x + 1; y < arr.length; y++) E for (int y = x + 1; y <= arr.length; y++)

D

Consider the following code segment. ArrayList<Integer> vals = new ArrayList<Integer>(); vals.add(vals.size(), vals.size()); vals.add(vals.size() - 1, vals.size() + 1); vals.add(vals.size() - 2, vals.size() + 2); System.out.println(vals.toString()); What is printed as a result of executing the code segment? A [0, 1, 2] B [0, 2, 4] C [1, 2, 3] D [2, 1, 0] E [4, 2, 0]

E

Consider the following method. public int[] transform(int[] a) { a[0]++; a[2]++; return a; } The following code segment appears in a method in the same class as transform. /* missing code */ arr = transform(arr); After executing the code segment, the array arr should contain {1, 0, 1, 0}. Which of the following can be used to replace /* missing code */ so that the code segment works as intended? I. int[] arr = {0, 0, 0, 0}; II. int[] arr = new int[0]; III. int[] arr = new int[4]; A I only B II only C III only D I and II E I and III

E

Consider the following statement, which is intended to create an ArrayList named arrList to store elements only of type String. /* missing code */ = new ArrayList<String>(); Which of the following can be used to replace /* missing code */ so that the statement works as intended? A ArrayList arrList() B ArrayList arrList C ArrayList<> arrList D ArrayList arrList<String> E ArrayList<String> arrList

E

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? A The modification in seqSearch2 has no effect: seqSearch2 will always behave exactly as seqSearch does. B The modification in seqSearch2 will cause a compilation error. C The modification in seqSearch2 will cause an ArrayIndexOutOfBoundsException to be thrown for some inputs. D The modification in seqSearch2 will cause -1 to be returned for all inputs. E 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.

E


संबंधित स्टडी सेट्स

Questions : Completing the Application, Underwriting , and delivering the policy

View Set

the 4 major regions of the brain

View Set

Ethics in Technology - C961: Pre-assessment

View Set