L6 - Arrays Flashcards

1
Q

What is an array in Java?

A

An array is a data structure that holds a collection of elements of the same type, indexed sequentially.

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

What are the key characteristics of arrays?

A
  • Fixed size once created
  • Elements are of the same type
  • Indexed from 0 to (size - 1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you declare an array in Java?

A

int[] numbers;

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

How do you create an array in Java?

A

numbers = new int[5]; //Creates an array of size 5

int[] numbers = new int[5]; //Declaration + Creation

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

How do you initialize an array with values?

A

int[] numbers = {10, 20, 30, 40, 50}

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

How do you access an array element?

A

System.out.println(numbers[2]);

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

What is the starting index of an array in Java?

A

Arrays in Java start at index 0.

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

How do you use a for loop to access an array?

A

for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}

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

How do you find the length of an array?

A

System.out.println(numbers.length);

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

What happens if you access an invalid index?

A

Java throws an ArrayIndexOutOfBoundsException.

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

How are arrays stored in memory?

A

Arrays in Java are objects, and the variable stores a reference (memory address) to the array.

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

What happens when you create an array of objects?

A

The array stores references to objects, but the objects must be created separately. Example:

Person[] people = new Person[3]; // Creates an array of 3 person references
people[0] = new Person(“Alice”); // Assigns an object to index 0

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

How do you create an array with a size determined at runtime?

A

Scanner input = new Scanner(System.in);
int size = input.nextInt();
int[] numbers = new int[size];

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

How do you declare a 2D array?

A

int[][] matrix = new int[3][4]; // 3 rows, 4 columns

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

How do you access an element in a 2D array?

A

int value = matrix[1][2]; // Row 1, Column 2

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

How do you iterate over a 2D array using nested loops?

A

for(int row = 0; row < matrix.length; row++){
for(int col = 0; col < matrix[row].length; col++){
System.out.print(matrix[row][col] + “ “);
}
System.out.println();
}

17
Q

What is a ragged array?

A

A 2D array where rows have different lengths. Example:

int[][] ragged = new int[3][];
ragged[0] = new int[5];
ragged[1] = new int[2];
ragged[2] = new int[7];

18
Q

How do you use a for-each loop with an array?

A

for (int num : numbers){
System.out.println(num);
}

19
Q

How does selection sort work?

A

It repeatedly selects the smallest element and swaps it to the front.

20
Q

How do you sort an array using Java’s built-in method?

A

Arrays.sort(arr);

21
Q

How do arrays behave when passed to a method?

A

Arrays are passed by reference, so modifications affect the original array.

22
Q

How do you return an array from a method

A

public static int[] createArray(int size){
return new int[size];
}

23
Q

How do you prevent privacy leaks when returning an array?

A

Instead of returning the reference, return a copy:

public int[] getArray(){
return Arrays.copyOf(data, data.length);
}

24
Q

What is an enumerated type in Java?

A

A custom type with fixed set of values. Example:

enum Day {MONDAY, TUESDAY, WEDNESDAY}
Day today = Day.MONDAY

25
How do you pass a 2D array to a method?
public static void printMatrix(int[] matrix){ for (int[] row: matrix) { for (int num: row) { System.out.print(num + " "); } System.out.println(); } }