Unit 6 Review AP Comp

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

Question: 5 What value will be held in mysteryNumber when this code finishes running? int mysteryNumber = 0; String[] mysteryArray = {"Finn", "Jake", "Bubblegum"}; for(int i = 0; i < mysteryArray.length; i++) { mysteryNumber += mysteryArray[i].length(); } 0 17 1 16

17

Question: 19 What will print out when the following code executes? int[] nums = {1, 2, 3, 4, 5, 6}; int count = 0; while (nums[count] % 2 != 0) { nums[count+1] ++; count ++; } System.out.println(count); 0 1 2 3 4 5

2

Question: 7 Consider the following code snippet: int[] arr = {1, 2, 3, 4, 5}; int[] copy = arr; copy[4] = 2; After this code runs, what is the value of arr[4]? 5 4 2 The code will error.

2

Question: 3 What is the output of the following program? public class SayHello { public void run() { String stringArray[] = {"h", "e", "l", "l", "o", "w"}; for(int i=0; i <= stringArray.length; i++) { System.out.print(stringArray[i]); } } } hel hello hellow Error h e l l o w

Error

Question: 18 Which of the following will initialize a boolean array of three elements all containing true? I. boolean[] arr = {true, true, true}; II. boolean[] arr = new boolean[3]; III. boolean[] arr = new boolean[3]; for (int i = 0; i < arr.length; i ++) { arr[i] = true; } I only II only III only I and III only I, II, and III

I and III only

Question: 8 Consider the following method that processes an array to find the smallest value in the array. The array has a nonzero length and is initialized with int values. // arr is the array to be processed public static int findMin (int[] arr) { int min = /* some value */; int index = 0; while (index < arr.length) { if (arr[index] < min) { min = arr[index]; } index++; } return min; } Java Which replacement(s) for /* some value */ will always result in correct execution of findMin?I. Integer.MAX_VALUE Java II. Integer.MIN_VALUE Java III. arr[0] Java I only II only III only I and III only II and III only

I and III only

Question: 2 Which of the following are valid arrays? I. int[] coolArray = {1, 2, 3}; II. int[] threeThings = {1.0, 2.0, 3.0}; III. int[] = {"1", "2", "3"}; I only II only III only I and II I and III

I only

Question: 12 The following codes are intended to add 5 to each item in the array.I. int[] numbers = {1, 2, 3, 4}; for (int i = 0; i < numbers.length; i++) { numbers[i] += 5; } II. int[] numbers = {1, 2, 3, 4}; for (int number : numbers) { number += 5; } Which statement is true? Both I and II will correctly add 5 to the numbers array. Neither I nor II will correctly add 5 to the numbers array. I will correctly add 5 to the numbers array. II will not correctly add 5 to the numbers array. II will correctly add 5 to the numbers array. I will not correctly add 5 to the numbers array.

I will correctly add 5 to the numbers array. II will not correctly add 5 to the numbers array.

Question: 14 What does the following code do when executed? int[] highTemp = {88, 92, 94, 90, 88, 83}; int target = 90; int count = 0; for (int temp : highTemp) { if (highTemp > target) { count ++; } } System.out.println(count); It will count the number of times the value in the highTemp array exceeds the target value. It will produce an error because the conditional statement is not set up correctly. It will produce an ArrayIndexOutOfBoundsException It will count the number of times that the value in the array is greater than the previous value in the array.

It will produce an error because the conditional statement is not set up correctly.

Question: 16 The following method is intended to return true if the array element value contains the same number in consecutive array elements. 1: public boolean consecutiveNumbers(int[] nums) 2: { 3: int i = 1; 4: while (i < nums.length) 5: { 6: if (nums[i] == nums[i+1]){ 7: return true; } 8: i++; 9: } 10: return false; 11: } Which of the following are needed to correct the code? I. Change line 3 to: int i = 0;II. Change line 4 to: while (i < nums.length - 1)III. Swap lines 7 and 10 I only II only III only Make both I and II changes

Make both I and II changes

Question: 1 Which of the following arrays has a length of 5? double[] arr = new double[6]; double[] arr = {0, 1, 2, 3, 4, 5}; double[] arr = new double[]{1, 2.3, 4, 5}; All of these None of these

None of these

Question: 13 The following code is designed to return true if a target value is present in an array of integers. public boolean search(int[] arr, int target){ for (int number : arr) { if (number != target) { return false; } } return true; } Which of the following statements is true? The code will not work correctly because the loop will not access each of the numbers correctly. The code will not work correctly because it may return true too soon. The code will not work correctly because the method may return false too soon. The code will work as intended.

The code will not work correctly because the method may return false too soon.

Question: 15 The following code is intended to shift all elements to the right by one space and wrap the last element around to the first element. 1: public void shiftRight(int[] arr) 2: { 3: int lastNum = arr[arr.length - 1]; 4: 5: for (int i = arr.length - 1; i > 0; i--) 6: { 7: arr[i] = arr[i - 1]; 8: } 9: arr[0] = lastNum; 10: } Which statement is true? The code will not work as intended. Line 5 should be changed to for (int i = 0; i < arr.length; i++) The code will not work as intended. Line 5 should be changed to for (int i = arr.length; i > 0; i--) The code will not work as intended. Line 7 should be changed to arr[i-1] = arr[i]; The code will not work as intended. It will produce an ArrayIndexOutOfBoundsException. The code will work as intended.

The code will work as intended

Question: 10 Which of the following statements is valid? Assume that variable arr is an array of n integers and that the following is true: arr[0] != arr[i] for all values of i from 1 through n - 1 The element arr[0] does not occur anywhere else in the array arr has no duplicates arr has duplicates arr is sorted arr is not sorted

The element arr[0] does not occur anywhere else in the array

Question: 6 What will the Array gameBoard contain after this code runs? int WIDTH = 3; int HEIGHT = 3; String[] gameBoard = new String[WIDTH * HEIGHT]; for(int m = 0; m < HEIGHT; m++) { for(int n = 0; n < WIDTH; n++) { int someNumber = m * WIDTH + n; if(someNumber % 3 == 0) { gameBoard[someNumber] = "X"; } else { gameBoard[someNumber] = "O"; } } } ["X", "O", "X", "O", "X", "O", "X", "O", "X"] ["X", "O", "O", "X", "O", "O", "X", "O", "O"] ["O", "X", "X", "O", "X", "X", "O", "X", "X"] ["O", "X", "O", "X", "O", "X", "O", "X", "O"]

["X", "O", "O", "X", "O", "O", "X", "O", "O"]

Question: 11 Given the following array: String[] languages = {"Java", "JavaScript", "Python", "C++"}; Which of the following will produce an ArrayIndexOutOfBoundsException? for (int i = 1; i <= languages.length; i++) { System.out.println(languages[i-1]); } for (int i = 0; i < languages.length; i++) { System.out.println(languages[i]); } for (int i = 0; i < languages.length; i++) { System.out.println(languages[i-1]); } for (int i = 0; i <= languages.length - 1; i++) { System.out.println(languages[i]); }

for (int i = 0; i < languages.length; i++) { System.out.println(languages[i-1]); }

Question: 17 Which of the following loops will NOT print every element in an integer array? for (int i = 1; i < array.length; i++) { System.out.println(array[i-1]); } for (int i = 1; i <= array.length; i++) { System.out.println(array[i-1]); } for (int number : array) { System.out.println(number); } int i = 1; while (i <= array.length) { System.out.println(array[i-1]); i++; } int i = 0; while (i < array.length) { System.out.println(array[i]); i++; }

for (int i = 1; i < array.length; i++){System.out.println(array[i-1]);}

Question: 20 Given the array: String[] fruit = {"Apple", "Pear", "Pineapple", "Carrot", "Banana", "Lettuce"}; Which code will correctly replace Carrot and Lettuce in the array? fruit[4] = "Grape"; fruit[6] = "Blueberry"; fruit[3] = "Grape"; fruit[5] = "Blueberry"; fruit[3, 5] = {"Grape", "Blueberry"}; fruit[4, 6] = {"Grape", "Blueberry"};

fruit[3] = "Grape";fruit[5] = "Blueberry";

int chocolateFlavorCount = 0; for(int i = 0; i < flavorArray.length; i++) { flavorArray[i].indexOf("chocolate") != -1; } System.out.println(chocolateFlavorCount); int chocolateFlavorCount = 0; for(int i = 0; i < flavorArray.length; i--) { if(flavorArray[i].indexOf("chocolate") != -1) { chocolateFlavorCount++; } } System.out.println(chocolateFlavorCount); int chocolateFlavorCount = 0; while(flavorArray.length) { if(flavorArray.indexOf("chocolate") != -1) { chocolateFlavorCount++; } } System.out.println(chocolateFlavorCount); int chocolateFlavorCount = 0; for(int i = 0; i < flavorArray.length; i++) { if(flavorArray[i].indexOf("chocolate") != -1) { chocolateFlavorCount++; } } System.out.println(chocolateFlavorCount);

int chocolateFlavorCount = 0;for(int i = 0; i < flavorArray.length; i++){if(flavorArray[i].contains("chocolate")){chocolateFlavorCount++;}}System.out.println(chocolateFlavorCount);

Question: 9 Given the following values of arr and the mystery method, what will the values of arr be after you execute: mystery()? private int[ ] arr = {-17, -14, 3, 9, 21, 34}; public void mystery() { for (int i = 0; i < arr.length / 2; i += 2) { arr[i] = arr[i] * 2; } } {-34, -28, 6, 18, 42, 68} {-17, -14, 3, 18, 21, 34} {-34, -28, 6, 9, 21, 34} {-34, -14, 6, 9, 21, 34} {-34, -14, 6, 9, 42, 34}

{-34, -14, 6, 9, 21, 34}


Ensembles d'études connexes

Child Psychology Ch. 3 (Prenatal Development)

View Set

Unit 4 Section 1: Animal Form & Function ch. 20 - Mastering Biology hw questions

View Set

Chapter 31 MEDICATION ADMINISTRATION (exam 3)

View Set