One-dimensional arrays - Quiz

Total: 10 questions

1. 

What is an array in Java, and how do you declare a one-dimensional array?

An array is a data structure that stores a group of values of the same type under a single name. Indexing starts from zero. You declare a one-dimensional array by writing the element type followed by square brackets and the variable name, for example int[] monthDays; or double[] monthSalaries;. The brackets may also be placed after the variable name (int monthDays[];), but putting them after the type is the recommended style, since the type and the brackets stay together.

2. 

How do you allocate memory for an array with the new keyword, and what values do its elements get?

Declaring an array does not allocate memory. You allocate it with the new keyword followed by the type and the size in square brackets, for example int[] values = new int[45];. After creation, every element receives the default value for its type: 0 for integer types (byte, short, int, long), 0.0 for float and double, the null character \u0000 for char, false for boolean, and null for reference types such as String.

3. 

What is an array initialization block, and how does the compiler determine the array size?

If you already know the values, you can use an array initialization block: instead of new int[12], list the values in curly braces separated by commas, for example int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};. The compiler infers the array size from the number of listed elements, so you do not specify the size explicitly.

4. 

What is an anonymous (unnamed) array in Java, and when is it used?

An anonymous (unnamed) array combines the new keyword with an initialization block, for example new int[]{4, 7, 2}. It is used in two situations: to reassign a new set of values to an existing array variable (the plain initializer syntax testScores = {4, 7, 2}; only works at declaration and otherwise causes a compilation error), and to pass an array directly as a method argument, for example print(new int[]{4, 6, 2, 3});.

5. 

How do you get the length of an array in Java and iterate over its elements? What happens if you access a non-existent index?

You get the number of elements through the length field — it is a field, not a method, so write it without parentheses (array.length), unlike length() on String or size() on collections. You can iterate with a for loop from index 0 up to length - 1, or with a shorter for-each loop (for (int score : scores)) that protects you from going out of bounds. Accessing a non-existent index compiles fine but throws an ArrayIndexOutOfBoundsException at runtime. The array size is fixed at creation and cannot be changed; use a collection like ArrayList if it must grow.

6. 

What is an array in Java, and how do you declare a one-dimensional array?

An array is a data structure that stores a group of values of the same type under a single name. Indexing starts from zero. You declare a one-dimensional array by writing the element type followed by square brackets and the variable name, for example int[] monthDays; or double[] monthSalaries;. The brackets may also be placed after the variable name (int monthDays[];), but putting them after the type is the recommended style, since the type and the brackets stay together.

7. 

How do you allocate memory for an array with the new keyword, and what values do its elements get?

Declaring an array does not allocate memory. You allocate it with the new keyword followed by the type and the size in square brackets, for example int[] values = new int[45];. After creation, every element receives the default value for its type: 0 for integer types (byte, short, int, long), 0.0 for float and double, the null character \u0000 for char, false for boolean, and null for reference types such as String.

8. 

What is an array initialization block, and how does the compiler determine the array size?

If you already know the values, you can use an array initialization block: instead of new int[12], list the values in curly braces separated by commas, for example int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};. The compiler infers the array size from the number of listed elements, so you do not specify the size explicitly.

9. 

What is an anonymous (unnamed) array in Java, and when is it used?

An anonymous (unnamed) array combines the new keyword with an initialization block, for example new int[]{4, 7, 2}. It is used in two situations: to reassign a new set of values to an existing array variable (the plain initializer syntax testScores = {4, 7, 2}; only works at declaration and otherwise causes a compilation error), and to pass an array directly as a method argument, for example print(new int[]{4, 6, 2, 3});.

10. 

How do you get the length of an array in Java and iterate over its elements? What happens if you access a non-existent index?

You get the number of elements through the length field — it is a field, not a method, so write it without parentheses (array.length), unlike length() on String or size() on collections. You can iterate with a for loop from index 0 up to length - 1, or with a shorter for-each loop (for (int score : scores)) that protects you from going out of bounds. Accessing a non-existent index compiles fine but throws an ArrayIndexOutOfBoundsException at runtime. The array size is fixed at creation and cannot be changed; use a collection like ArrayList if it must grow.

Page 1 of 1