Arrays

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Create an array called grams with the initial values 10.2, 11.5, 6.35, 4.25, and 8.75

double [ ] grams = {10.2, 11.5, 6.35, 4.25, 8.75};

What is the output of the following code snippet? int[] scores = {80, 92, 91, 68, 88}; for(int score : scores) { System.out.println(score); }

80 92 91 68 88

You can change the values stored in an array by

updating the enhanced for-loop variable

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

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] = scores[i] * 2); }

160 182 176

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

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 is the difference between the default values of a String array and the default values of an int array?

The default value for an element in a String array is *null* while the default value for an element in an int array is *0*.

How do you find the last element in an array called arr?

arr.length - 1

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

int[ ] arr = new int [n];

How do you define an array called arr that holds 7 ints?

int[ ] arr = new int[7];

What is the output of the following code snippet? 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

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

ArrayIndexOutOfBoundsException

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;


Set pelajaran terkait

Chapters 5, 7p2, 3, 9, and Test 2 Review Questions

View Set

World History Fall Final: Short Answers

View Set

Tutorial 08: Enhancing a Website with Multimedia

View Set

Chapter 8 Classes and Objects: A Deeper Look, Java Chapter 8 quiz, JAVA CH8, Chapter 9 Java 401, Ch. 10, Java Quizzes 10-end, Chapter 9 Inheritance

View Set