Chapter 5 but shorter Flashcards

1
Q

What is an array in Java?

A

A collection of variables of the same type stored in a fixed-size sequential order.

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

Why use arrays?

A

✅ Easier management of multiple values
✅ Efficient storage and access
✅ Helps organize large data sets

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

Example of declaring an array?

A

```java
int[] scores = new int[50];
~~~
✅ Creates an array named scores that holds 50 integers.

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

How are array elements accessed?

A

Using an index, which starts from 0.

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

Example of accessing an element?

A

```java
int[] scores = {79, 87, 94, 82};
System.out.println(scores[2]); // Output: 94
~~~
scores[2] refers to the third element (index 2).

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

Different ways to declare an array?

A

Declare only: int[] scores;
Declare & initialize: int[] scores = new int[10];
Using an initializer list: int[] scores = {79, 87, 94};

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

What happens if an array is not initialized?

A

It contains default values:
- 0 for numeric types
- false for boolean
- null for objects

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

Where are arrays stored in memory?

A

Arrays are objects stored in heap memory.

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

Example of storing an array?

A

```java
int[] numbers = new int[5]; // Stores integers in contiguous memory locations
~~~

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

What is a 2D array?

A

An array with rows and columns (like a table).

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

Example of declaring a 2D array?

A

```java
int[][] matrix = new int[2][3];
~~~
✅ Creates 2 rows and 3 columns.

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

Example of initializing a 2D array?

A

```java
int[][] values = { {1, 2, 3}, {4, 5, 6} };
System.out.println(values[1][2]); // Output: 6
~~~
values[1][2] refers to row 1, column 2.

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

What happens if an index is out of bounds?

A

ArrayIndexOutOfBoundsException is thrown.

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

Example of an out-of-bounds error?

A

```java
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]); // Error: Index 5 is out of bounds!
~~~
✅ Java prevents access beyond valid indices.

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

How to loop through an array using a for loop?

A

```java
for (int i = 0; i < scores.length; i++) {
System.out.println(scores[i]);
}
~~~
✅ Prints all elements from first to last.

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

How to use an enhanced for loop?

A

```java
for (int score : scores) {
System.out.println(score);
}
~~~
✅ Easier way to iterate, but cannot modify elements.

17
Q

Does array1 = array2; copy an array?

A

No! It only copies the reference, not the values.

18
Q

Three correct ways to copy an array?

A
  1. Using a loop

```java
for (int i = 0; i < sourceArray.length; i++) {
targetArray[i] = sourceArray[i];
}
~~~

  1. Using arraycopy()

```java
System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
~~~

  1. Using clone()

```java
int[] targetArray = sourceArray.clone();
~~~

✅ Creates a new array with the same values.

  1. Where:
    sourceArray → The array from which elements are copied.

srcPos (0) → The starting position (index) in sourceArray from where copying begins.

targetArray → The array into which elements are copied.

destPos (0) → The starting position (index) in targetArray where elements will be copied.

length (sourceArray.length) → The number of elements to copy.

19
Q

How are arrays passed to methods?

A

By reference, meaning changes inside the method affect the original array.

20
Q

Example of passing an array?

A

```java
public static void printArray(int[] arr) {
for (int num : arr) {
System.out.println(num);
}
}
printArray(new int[] {1, 2, 3, 4});
~~~
✅ Prints 1 2 3 4.

21
Q

How can a method return an array?

A

```java
public static int[] getNumbers() {
return new int[] {10, 20, 30};
}
int[] nums = getNumbers();
~~~
✅ The method returns an array reference.

22
Q

What is a variable-length argument list (varargs)?

A

Allows passing any number of arguments to a method.

23
Q

Example of varargs?

A

```java
public static void printMax(double… numbers) {
double max = numbers[0];
for (double num : numbers) {
if (num > max) max = num;
}
System.out.println(“Max: “ + max);
}
printMax(3.5, 7.2, 9.8, 1.4); // Output: Max: 9.8
~~~
✅ Accepts any number of double values.

24
Q

How does Java receive command-line arguments?

A

Through the args parameter in main().

25
Q

Example of reading arguments?

A

```java
public class ArgTest {
public static void main(String[] args) {
System.out.println(“First argument: “ + args[0]);
}
}

✅ Running `java ArgTest Hello` prints:

First argument: Hello
~~~

26
Q

Example of finding the maximum number in an array?

A

```java
int max = userVals[0];
for (int i = 1; i < userVals.length; i++) {
if (userVals[i] > max) max = userVals[i];
}
System.out.println(“Max: “ + max);
~~~
✅ Loops through the array and finds the largest number.