Topic 4.1 and 4.2: Creating and Using Arrays - Declare, instantiate, initialize and use a one-dimensional array and Declare, instantiate, initialize and use multi-dimensional arrays Flashcards

1
Q

What is
array initialization
in Java?

A

This refers to assigning values to the elements of the array after it has been instantiated. It involves specifying the initial values of the array elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

This sets numeric types to 0, booleans to false, and object references to null. If we do not explicitly initialize the array at the point of declaration, the elements of the array will have these default values.

A

What is
default initialization of an array
in Java?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an array in Java?

A

This is a data structure that stores a fixed-size sequence of elements of the same type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What should you consider when choosing between a for loop and an enhanced for-each loop for looping over arrays?

A

When looping over arrays, consider whether you need access to the index or only the values held in the elements. If you require knowledge of the index, a for loop is the appropriate choice. However, if you only need the values of the array elements, you can opt for the enhanced for-each loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the
range of indices for accessing elements in an array?

A

This start from 0 and go up to (array length - 1). This means that the first element is accessed using index 0, and the last element is accessed using index (array length - 1).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

to accomplish this, you use the array name followed by the index in square brackets.

For example, “int firstElement = numbers[0];

A

How do you
access elements in an array
in Java?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

to accomplish this you specify the type of the elements it will hold, followed by square brackets ([]), and then the name of the array.

For example:

int[] numbers;

A

How do you declare an array in Java?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

This refers to creating a new array object in memory.

It involves allocating memory space for the array and setting up the necessary data structures to hold the elements.

A

What does it mean to
instantiate an array
in Java?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you initialize a multi-dimensional array using explicit initialization?

A

To initialize a multi-dimensional array using this syntax, you can provide the values for each element of the array during declaration. Here’s an example:

int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

This refers to assigning values to the elements of the array after it has been instantiated. It involves specifying the initial values of the array elements.

A

What is
array initialization
in Java?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

This involves specifying specific values for the elements of the array during instantiation using the new keyword.

For example, “int[] numbers = new int[]{1, 2, 3, 4, 5};” explicitly initializes the array with the values 1, 2, 3, 4, and 5.

A

What is
explicit initialization
of an array in Java?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you
access elements in an array
in Java?

A

to accomplish this, you use the array name followed by the index in square brackets.

For example, “int firstElement = numbers[0];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

This is a data structure that stores a fixed-size sequence of elements of the same type.

A

What is an array in Java?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

This is carried out after declaring an array

you need to allocate memory for it using the new keyword. For example, “numbers = new int[5];” creates an integer array of size 5 and assigns it to the “numbers” variable.

Alternatively, you can combine declaration and this into a single statement: “int[] numbers = new int[5];”.

A

How do you
instantiate an array
in Java?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you
instantiate an array
in Java?

A

This is carried out after declaring an array

you need to allocate memory for it using the new keyword. For example, “numbers = new int[5];” creates an integer array of size 5 and assigns it to the “numbers” variable.

Alternatively, you can combine declaration and this into a single statement: “int[] numbers = new int[5];”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

This allows you to initialize an array using a more concise syntax.

For example, “int[] numbers = {1, 2, 3, 4, 5};” initializes the array with the values 1, 2, 3, 4, and 5.

A

What is
array initializer
initialization in Java?

17
Q

What is
default initialization of an array
in Java?

A

This sets numeric types to 0, booleans to false, and object references to null. If we do not explicitly initialize the array at the point of declaration, the elements of the array will have these default values.

18
Q

To initialize a multi-dimensional array using this syntax, you can provide the values for each element of the array during declaration. Here’s an example:

int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

A

How can you initialize a multi-dimensional array using explicit initialization?

19
Q

How do you declare an array in Java?

A

to accomplish this you specify the type of the elements it will hold, followed by square brackets ([]), and then the name of the array.

For example:

int[] numbers;

20
Q

to accomplish this you specify multiple sets of square brackets ([]).

For example, “int[][] matrix;

A

How do you
declare a multi-dimensional array
in Java?

21
Q

What is
array initializer
initialization in Java?

A

This allows you to initialize an array using a more concise syntax.

For example, “int[] numbers = {1, 2, 3, 4, 5};” initializes the array with the values 1, 2, 3, 4, and 5.

22
Q

To initialize a multi-dimensional array using this syntax, you can provide the values for each element of the array within curly braces. Here’s an example:

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

A

How can you initialize a multi-dimensional array using the array initializer syntax?

23
Q

When might explicit initialization using the new keyword and array initializer syntax be preferred for initializing arrays?

A

Explicit initialization using the new keyword is preferred when the declaration and initialization of the array are separated in the code, allowing for flexibility in initializing the array at a later stage while maintaining readability.

Int[] hello; // Declaration
// ...
hello = new int[]{1, 2, 3, 4, 5}; // Initialization

Array initializer syntax, on the other hand, is preferred when the declaration and initialization can be done together, promoting code readability and conciseness.

24
Q

What does it mean to
instantiate an array
in Java?

A

This refers to creating a new array object in memory.

It involves allocating memory space for the array and setting up the necessary data structures to hold the elements.

25
Q

How can you initialize a multi-dimensional array using the array initializer syntax?

A

To initialize a multi-dimensional array using this syntax, you can provide the values for each element of the array within curly braces. Here’s an example:

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

26
Q

What is
explicit initialization
of an array in Java?

A

This involves specifying specific values for the elements of the array during instantiation using the new keyword.

For example, “int[] numbers = new int[]{1, 2, 3, 4, 5};” explicitly initializes the array with the values 1, 2, 3, 4, and 5.

27
Q

How do you
declare a multi-dimensional array
in Java?

A

to accomplish this you specify multiple sets of square brackets ([]).

For example, “int[][] matrix;

28
Q

This start from 0 and go up to (array length - 1). This means that the first element is accessed using index 0, and the last element is accessed using index (array length - 1).

A

What is the
range of indices for accessing elements in an array?