CodeHS Array Quiz 20.1.6

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

What is the output of the following program?

Error

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; } Which replacement(s) for /* some value */ will always result in correct execution of findMin?

I and III only

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 and III only

Which of the following are valid arrays?

I only

The following codes are intended to add 5 to each item in the array.

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

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 produce an error because the conditional statement is not set up correctly.

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

Make both I and II changes

Which of the following arrays has a length of 5?

None of these

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 method may return false too soon.

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 work as intended.

Which of the following statements is valid? Assume that variable arr is an array of n integers and that the following is true:

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

Given the following array: String[] languages = {"Java", "JavaScript", "Python", "C++"}; Which of the following will produce an ArrayIndexOutOfBoundsException?

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

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]); }

Given the array: String[] fruit = {"Apple", "Pear", "Pineapple", "Carrot", "Banana", "Lettuce"}; Which code will correctly replace Carrot and Lettuce in the array?

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

What value will be held in mysteryNumber when this code finishes running?

17

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]?

2

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);

2

What will the Array gameBoard contain after this code runs?

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

You are working at the frozen yogurt shop in the mall. Your boss asks you to count how many flavors have the word chocolate in their name. You begin going through the list of flavors, but quickly realize that their are over 9000 flavors to check! Luckily, your boss stored all of the different flavors in a Java Array named flavorArray on the company computer. Write a Java program to count the number of flavors that have chocolate in their name.

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

Given the following values of arr and the mystery method, what will the values of arr be after you execute: mystery()?

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


Ensembles d'études connexes

Platelet and Coagulation Disorders (Exam 5)

View Set

Great Britain raises taxes S.S. lesson

View Set