0% found this document useful (0 votes)
1 views7 pages

Arrays in Java

The document provides an overview of arrays in Java, including their definition, memory representation, types of declaration, and common operations such as traversing, taking input, and printing. It also discusses advantages and disadvantages of arrays, common errors, and includes multiple-choice questions to test understanding. Key points include that arrays are fixed in size, stored in heap memory, and can store homogeneous data.

Uploaded by

nepekah233
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Arrays in Java

The document provides an overview of arrays in Java, including their definition, memory representation, types of declaration, and common operations such as traversing, taking input, and printing. It also discusses advantages and disadvantages of arrays, common errors, and includes multiple-choice questions to test understanding. Key points include that arrays are fixed in size, stored in heap memory, and can store homogeneous data.

Uploaded by

nepekah233
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

📦 Arrays in Java

📌 1. Definition
👉 An array is a collection of elements of the same data type, stored in contiguous memory
locations.

✅ Fixed size​
✅ Indexed (0-based)​
✅ Homogeneous data

🧠 2. Memory Representation
👉 Arrays are stored in heap memory in Java.
●​ Reference variable → stored in stack
●​ Actual array → stored in heap

📍 Example:
int[] arr = new int[5];

🧠 Memory:
arr → [0][0][0][0][0]
0 1 2 3 4 (indexes)

🛠️ 3. Types of Array Declaration


🔹 Method 1
int[] arr = new int[5];

🔹 Method 2
int arr[] = new int[5];

Soumik Karmakar - Object Oriented Programming with Java


🔹 Method 3 (Initialization)
int[] arr = {1, 2, 3, 4, 5};

🔹 Method 4 (Dynamic + values)


int[] arr = new int[]{10, 20, 30};

📏 4. Size of Array (length)


👉 Use .length property
int[] arr = {1,2,3,4};
[Link]([Link]); // 4

📌 Note:
●​ Not a method → no ()

🔁 5. Traversing an Array
🔹 Using for loop
for(int i = 0; i < [Link]; i++) {
[Link](arr[i]);
}

🔹 Enhanced for loop (for-each)


for(int num : arr) {
[Link](num);
}

👉 Use when index is NOT required 👍

📥 6. Taking Input
import [Link].*;

Scanner sc = new Scanner([Link]);


int n = [Link]();

Soumik Karmakar - Object Oriented Programming with Java


int[] arr = new int[n];

for(int i = 0; i < n; i++) {


arr[i] = [Link]();
}

📤 7. Printing Array
🔹 Normal
for(int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}

🔹 Using [Link]()
import [Link];

[Link]([Link](arr));

⚠️ 8. Common Errors in Arrays


❌ 1. ArrayIndexOutOfBoundsException

int[] arr = new int[3];
[Link](arr[5]); //

❌ 2. NullPointerException

int[] arr = null;
[Link]([Link]); //

❌ 3. NegativeArraySizeException
int[] arr = new int[-5]; // ❌
🔍 9. Accessing & Updating Elements
🔹 Access
[Link](arr[2]);

Soumik Karmakar - Object Oriented Programming with Java


🔹 Update
arr[2] = 100;

🧱 10. Array of Objects


class Student {
int id;
String name;
}

Student[] students = new Student[3];

students[0] = new Student();


students[0].id = 1;
students[0].name = "Soumik";

👉 Array stores references of objects 🔗

🔄 11. Passing Array to Methods


public static void printArray(int[] arr) {
for(int num : arr) {
[Link](num + " ");
}
}

Call:

printArray(arr);

👉 Arrays are passed by reference 🔁

🔙 12. Returning Array from Method


public static int[] createArray() {
int[] arr = {1,2,3};
return arr;
}

Soumik Karmakar - Object Oriented Programming with Java


✅ 13. Advantages
✨ Fast access (O(1))​
✨ Simple & easy​
✨ Memory efficient​
✨ Useful for fixed-size data

❌ 14. Disadvantages
🚫 Fixed size​
🚫 Insertion/Deletion costly​
🚫 Homogeneous only​
🚫 Memory wastage possible

🧠 16. MCQs on Arrays


❓1. What is default value of int array?
A. 1​
B. 0​
C. null​
D. garbage

✅ Answer: B​
🧠 Explanation: Primitive int → default = 0

❓2. Index of first element?


A. 1​
B. 0​
C. -1​
D. depends

Soumik Karmakar - Object Oriented Programming with Java


✅ Answer: B

❓3. Which is correct declaration?


A. int arr[5]​
B. int[] arr​
C. array int arr​
D. int arr()

✅ Answer: B

❓4. Array size is?


A. Dynamic​
B. Fixed​
C. Unlimited​
D. Random

✅ Answer: B

❓5. Which exception for invalid index?


A. NullPointerException​
B. ArrayIndexOutOfBoundsException​
C. ArithmeticException​
D. IOException

✅ Answer: B

❓6. .length is?


A. Method​
B. Variable​
C. Class​
D. Object

✅ Answer: B

Soumik Karmakar - Object Oriented Programming with Java


❓7. Array is stored in?
A. Stack​
B. Heap​
C. CPU​
D. Disk

✅ Answer: B

❓8. Which loop is best without index?


A. for​
B. while​
C. do-while​
D. for-each

✅ Answer: D

❓9. Can array store objects?


A. No​
B. Yes​
C. Only primitives​
D. Only strings

✅ Answer: B

❓10. What happens if array not initialized?


A. Compile error​
B. Runtime error​
C. Garbage value​
D. NullPointerException

✅ Answer: D

Soumik Karmakar - Object Oriented Programming with Java

You might also like