Ch.6: Arrays

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

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

160 182 176

What will the following code segment output? 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 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 even 56 is even 2 is even 100 is even

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

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

What is the output after this code snippet runs? int[] scores = {80, 92, 91, 68, 88}; for(int i = 0; i < scores.length; i++) { System.out.println(scores[i] - 1); }

79 91 90 67 87

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

80 92 91 68 88

True or False: You can change a primitive value stored in an array by updating the enhanced for-loop variable.

False

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 initializes an array of 4 doubles called temperatures with the following initial values: 80.2, 65.3, 12.17, 20.21?

double[] temperatures = {80.2, 65.3, 12.17, 20.21};

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

Which index is the last element in an array called highTemps?

highTemps.length - 1

Which of the following code snippets 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;

int x = 25; int[] numArray = new int[x]; x = 100; System.out.println("x is " + x); System.out.println("The size of numArray is " + numArray.length);

x is 100 The size of numArray is 25

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

ArrayIndexOutOfBoundsException

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

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.

What does the following code snippet do? int[] scores = {80, 92, 91, 68, 88}; int sum = 0; for (int i = 0; i < scores.length; i++){ sum += scores[i]; } System.out.println((double) sum / scores.length);

Prints the average of the array

How do you initialize an array called arr of 5 ints with default values?

int[] arr = new int[5];


Ensembles d'études connexes

Quiz 3: Birth of a Nation Review

View Set

(Psych) Relationship Development and therapeutic communication, testbankgo

View Set

2: Solutions 2.02: The Dissolving Process Wiva k12 Chemistry

View Set