Unit 6 CSA

Ace your homework & exams now with Quizwiz!

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

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. 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

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 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 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? checkString(new String[]); checkString(new String[0]); 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 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? int[] arr = {0, 0, 0, 0}; int[] arr = new int[0]; int[] arr = new int[4]; A I only B II only C III only D I and II E I and III

E


Related study sets

anatomy 2 lab quiz 12 Inheritance

View Set

Practice Questions NUR 345 Exam 1

View Set

Survey of the universe week 4 quiz

View Set

NUR202 Evolve: Introductory Quiz - Diabetes

View Set

Massachusetts Driver's Permit Test (Spring 2019)

View Set