1 Answers
📚 Quick Study Guide: Java Arrays for AP CSA
- 📝 What is an Array? An array is an object that stores a fixed-size sequential collection of elements of the same data type. Think of it as a list of variables of the same type.
- ⚙️ Declaration: To declare an array, you specify the data type followed by `[]` and the array name. Example: `$int[] scores;$` or `$String[] names;$`
- 🚀 Initialization: After declaration, you must create the array object using the `new` keyword and specify its size. Example: `$scores = new int[10];$` This creates an array named `scores` that can hold 10 integer values.
- 🔢 Accessing Elements: Array elements are accessed using an index, which starts from `0` for the first element. The last element is at `length - 1`. Example: `$scores[0]$` accesses the first element, `$scores[9]$` accesses the last element of a 10-element array.
- 📏 Array Length: The number of elements an array can hold is found using the `.length` property. Example: `$scores.length$` would return `10` for the array above.
- 🔁 Iterating Arrays: You often use `for` loops to process arrays. A common loop looks like: `$for (int i = 0; i < scores.length; i++) { // access scores[i] }$`
- ❌ Common Error: An `ArrayIndexOutOfBoundsException` occurs if you try to access an index that is outside the valid range (e.g., negative index or an index greater than or equal to the array's length).
- 💡 Default Values: When an array is initialized, its elements are automatically given default values: `0` for numeric types, `false` for booleans, and `null` for object types (like `String`).
🧠 Practice Quiz: Test Your Array Knowledge
1. Which of the following correctly declares and initializes an array of 5 integers named `numbers`?
A. `int numbers[5];`
B. `int[] numbers = new int[5];`
C. `int numbers[] = {1, 2, 3, 4, 5};`
D. `numbers = new int[5];`
2. What is the index of the first element in any Java array?
A. 1
B. 0
C. -1
D. The array's length
3. Given the array `String[] names = {"Alice", "Bob", "Charlie"};`, what will `names[1]` return?
A. "Alice"
B. "Bob"
C. "Charlie"
D. An error
4. If an array is declared as `double[] prices = new double[7];`, what is the value of `prices.length`?
A. 6
B. 7
C. 8
D. Undefined
5. Which type of loop is most commonly used to iterate through all elements of an array from start to finish?
A. `while` loop
B. `do-while` loop
C. `for` loop (including enhanced for loop)
D. `if-else` statement
6. What happens if you try to access `myArray[myArray.length]`?
A. It returns the last element of the array.
B. It returns `null`.
C. It results in an `ArrayIndexOutOfBoundsException`.
D. It returns the size of the array.
7. What is the default value for elements in a `boolean[] flags = new boolean[3];` array?
A. `true`
B. `false`
C. `null`
D. Undefined
Click to see Answers
1. B
2. B
3. B
4. B
5. C
6. C
7. B
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀