Arrays review

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

What does the following code snippet check for? int[] numbers = {1, 2, 3, 3, 4, 5}; boolean mysteryBoolean = false; for (int i = 0; i < numbers.length - 1; i++) { for (int j = i + 1; j < numbers.length; j++) { if (numbers[i] == numbers[j]) { mysteryBoolean = true; } } }

Finds duplicate values in an array

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

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.

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

When would you use a for-each loop instead of a for loop?

a. If you want to access every element of an array and want to refer to elements through a variable name instead of an array index.

Which of the following arrays has a length of 5?

e. None of these

Given the following code snippet int[ ] values = {150, 34, 320, 2, 11, 100}; for (int i=0; i < values.length; i++) { if (values[i] % 10 == 0) System.out.println(values[i] + " is divisible by 10"); } which for-each loop would produce the same output?

for (int value : values) { if (value % 10 == 0) System.out.println(value + " is divisible by 10"); }

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?

! II only ! III only II and III only

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?

!b. 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. 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--)

What will the following code snippet output? int[ ] values = {17, 34, 56, 2, 19, 100}; for (int value : values) { if (value % 2 == 0) System.out.println(value + " is even"); }

. 34 is even56 is even2 is even100 is even

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

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

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 is the output after this code snippet runs? int[] scores = {80, 92, 91, 68, 88}; int i = 0; while (i < scores.length - 1) { System.out.println(scores[i] * 2); i ++; }

160 184 182 136

What will be the output of the following code snippet? int[] scores = {80, 92, 91, 68, 88}; int myIndex = 0; for (int i = 1; i < scores.length; i++) { if (scores[i] < scores[myIndex]) { myIndex = i; } } System.out.println(scores[myIndex]);

68

int[] scores = {80, 92, 91, 68, 88}; for(int score : scores) { System.out.println(score); }

80 92 91 68 88

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

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 statements is valid? Assume that variable arr is an array of i integers and that the following is true: arr[0] != arr[i] for all values from 1 through i - 1

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

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", "O", "X", "O", "O", "X", "O", "O"]

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

error

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

Which of the following code snippet gets the THIRD element in the scores array and assigns it to an int called myScore , then sets the FOURTH element in the scores array (previously declared) to 72?

int myScore = scores[2]; scores[3] = 72;

When run, which of the following yields the integer 5?

int[] anotherArray = {5, 10, 15, 20, 25};System.out.println(anotherArray[(anotherArray.length - 2)] - 15);

How do you create an array of 5 ints?

int[] arr = new int[5];

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, -14, 6, 9, 21, 34}

int mysteryNumber = 0;String[] mysteryArray = {"Finn", "Jake", "Bubblegum"};for(int i = 0; i < mysteryArray.length; i++){ mysteryNumber += mysteryArray[i].length();}

17

int[] arr = {1, 2, 3, 4, 5};int[] copy = arr;copy[4] = 2; After this code, what is the value of arr[4]?

2

String[] grades = {"A","C","B","A","B", "A"}; int mystery = 0; for (int i = 0; i < grades.length; i++) { if (grades[i].equals("A")) { mystery ++; } } System.out.println(mystery);

3

What does the following code snippet output? int[] numbers = {1, 2, 3, 4, 5}; int[] temp = new int[numbers.length]; for (int i = 0; i < numbers.length - 1; i++) { temp[i + 1] = numbers[i]; } temp[0] = numbers[numbers.length - 1]; numbers = temp; for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); }

5 1 2 3 4

int[] scores = {80, 92, 91, 68, 88}; for(int i = 0; i < scores.length; i--) { System.out.println(scores[i]); }

b. ArrayIndexOutOfBoundsException

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

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

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

int[] someArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};for (int i = someArray.length - 2; i >= 0; i-=2){ someArray[i] = someArray[i] - 5;}

{1, -3, 3, -1, 5, 1, 7, 3, 9, 5, 11}


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

Mankiw Principles of Economics Ch.13

View Set

5.2 STATISTICS PROBABILITIES MARCH 30

View Set

Hexagon Interview includes react, Redux,

View Set

Chapter 14: Communicate Customer Value: Direct, On-line, Social Media and Mobile Marketing

View Set

CH 3 (Making Sense of Arguments)

View Set