Multi-Dimensional Arrays (2D) and Jagged Arrays in Java Notes in Hindi & English : 2D एरे और जैग्ड एरे (Jagged Arrays - असमान कॉलम साइज) और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Multi-Dimensional Arrays (2D) and Jagged Arrays in Java (2D एरे और जैग्ड एरे (Jagged Arrays - असमान कॉलम साइज)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Java implements 2D arrays not as single contiguous matrices, but as arrays of references pointing to separate 1D array objects in Heap memory. This design naturally enables jagged (ragged) arrays where each row holds a different number of columns.

अध्याय सार: जावा 2D एरे को एकल सन्निहित मैट्रिक्स के रूप में नहीं, बल्कि हीप मेमोरी में अलग-अलग 1D एरे ऑब्जेक्ट्स की ओर संकेत करने वाले रेफरेंस एरे के रूप में लागू करता है। यह डिज़ाइन स्वाभाविक रूप से जैग्ड एरे (Jagged Arrays) को संभव बनाता है जहाँ प्रत्येक पंक्ति में कॉलम्स की संख्या अलग हो सकती है।

Multi-Dimensional Arrays (2D) and Jagged Arrays in Java: Detailed Notes & Architecture (2D एरे और जैग्ड एरे (Jagged Arrays - असमान कॉलम साइज))

1. Array-of-Arrays Architecture in Java (जावा में एरे-ऑफ़-एरेज़ आर्किटेक्चर)

[EN] Unlike C or C++ where int mat[3][4] occupies one contiguous 12-element block, Java int[][] mat is an outer 1D array of type int[][] holding references to inner int[] objects. When instantiated via new int[3][4], the JVM creates one outer array of length 3 and three distinct inner 1D arrays of length 4 scattered anywhere on the Heap. Consequently, mat.length returns the number of rows (3), while mat[i].length returns the column count of row i. Valid declaration variants include int[][] a;, int a[][];, and int[] a[];.

[HI] C या C++ के विपरीत जहाँ int mat[3][4] एक साथ 12 तत्वों का सन्निहित ब्लॉक लेता है, जावा में int[][] mat एक बाहरी एरे है जो आंतरिक int[] ऑब्जेक्ट्स के रेफरेंस रखता है। new int[3][4] चलाने पर JVM 3 लंबाई का एक आउटर एरे और हीप पर कहीं भी 4 लंबाई के तीन अलग-अलग 1D एरे बनाता है। इसलिए mat.length पंक्तियों की संख्या (3) देता है जबकि mat[i].length i-वीं पंक्ति के कॉलमों की संख्या लौटाता है। वैध डिक्लेरेशन रूपों में int[][] a;, int a[][]; और int[] a[]; शामिल हैं।

2. Jagged (Ragged) Arrays and Memory Efficiency (जैग्ड (Ragged) एरे और मेमोरी दक्षता)

[EN] A jagged array is a multi-dimensional array whose inner rows have unequal column lengths, such as a triangular table or variable-length student marks per class. To construct a jagged array, specify only the outermost row dimension first: int[][] jag = new int[3][];. At this stage, every element jag[i] holds null until each row is individually instantiated with sizes like jag[0] = new int[2]; and jag[1] = new int[5];. Attempting new int[][4] without the primary row size triggers a compile-time error because the outer array size must be known first.

[HI] जैग्ड एरे वह मल्टी-डायमेंशनल एरे है जिसकी प्रत्येक पंक्ति में कॉलम्स की संख्या असमान होती है, जैसे त्रिकोणीय तालिका या प्रत्येक कक्षा में अलग-अलग छात्र अंक। जैग्ड एरे बनाने के लिए पहले केवल आउटर रो डायमेंशन दिया जाता है: int[][] jag = new int[3][];। इस चरण में प्रत्येक jag[i] का मान null होता है जब तक प्रत्येक पंक्ति को अलग से jag[0] = new int[2]; और jag[1] = new int[5]; द्वारा आवंटित न किया जाए। बिना पंक्ति संख्या दिए new int[][4] लिखना कंपाइल-टाइम एरर देता है क्योंकि आउटर एरे का साइज पहले ज्ञात होना अनिवार्य है।

3. Initialization and Traversal of 2D and Jagged Arrays (2D और जैग्ड एरे का इनिशियलाइज़ेशन व ट्रैवर्सल)

[EN] Inline literal initialization seamlessly constructs either rectangular or jagged 2D arrays, e.g., int[][] grid = {{1, 2}, {3, 4, 5}};. When traversing a jagged array with nested for loops, the inner loop bound must strictly use grid[i].length rather than a fixed constant or grid[0].length. Using grid[0].length on a jagged array either misses trailing elements or raises ArrayIndexOutOfBoundsException on shorter rows. Nested enhanced for-each loops (for (int[] row : grid) followed by for (int val : row)) safely adapt to variable row sizes automatically.

[HI] इनलाइन लिटरल इनिशियलाइज़ेशन आसानी से आयताकार या जैग्ड दोनों तरह के 2D एरे बना सकता है, जैसे int[][] grid = {{1, 2}, {3, 4, 5}};। नेस्टेड for लूप से जैग्ड एरे को ट्रैवर्स करते समय इनर लूप की सीमा किसी निश्चित संख्या या grid[0].length के बजाय grid[i].length होनी चाहिए। जैग्ड एरे पर grid[0].length का उपयोग करने से छोटी पंक्तियों पर ArrayIndexOutOfBoundsException आ सकता है या डेटा छूट सकता है। नेस्टेड for-each लूप (for (int[] row : grid) के भीतर for (int val : row)) स्वतः ही असमान पंक्ति आकारों के अनुकूल काम करता है।

4. Runtime Pitfalls: NullPointerException and Row Reassignment (रनटाइम त्रुटियां: NullPointerException और रो रीअसाइनमेंट)

[EN] If an outer array is created via int[][] a = new int[3][]; and you access a[0][0] before allocating a[0], the JVM throws NullPointerException because a[0] is null. Because each row is an independent 1D array object, you can swap two rows in O(1) time simply by exchanging references: int[] temp = a[0]; a[0] = a[1]; a[1] = temp;. Similarly, a single row can be replaced at runtime with a newly sized 1D array without reallocating the other rows. Keep in mind that calling a.clone() on a 2D array performs only a shallow copy of row references—inner 1D rows remain shared.

[HI] यदि int[][] a = new int[3][]; बनाकर बिना a[0] आवंटित किए a[0][0] एक्सेस किया जाए, तो a[0] के null होने से JVM NullPointerException फेंकता है। प्रत्येक पंक्ति एक स्वतंत्र 1D एरे ऑब्जेक्ट होती है, इसलिए केवल रेफरेंस बदलकर O(1) समय में दो पंक्तियों की अदला-बदली की जा सकती है। इसी प्रकार अन्य पंक्तियों को छेड़े बिना किसी एक पंक्ति को नए आकार के 1D एरे से बदला जा सकता है। ध्यान रखें कि 2D एरे पर a.clone() कॉल करने से केवल पंक्ति रेफरेंस की शैलो कॉपी बनती है—आंतरिक 1D पंक्तियां साझा रहती हैं।

Syntax, Formula & Technical Example (Multi-Dimensional Arrays (2D) and Jagged Arrays in Java)

int[][] jag = new int[3][];
jag[0] = new int[]{10, 20};
jag[1] = new int[]{30, 40, 50};
jag[2] = new int[]{60};
for (int i = 0; i < jag.length; i++) {
    for (int j = 0; j < jag[i].length; j++) {
        System.out.print(jag[i][j] + " ");
    }
}

Multi-Dimensional Arrays (2D) and Jagged Arrays in Java Quick Reference & Comparison Table

2D ConstructHeap BehaviorExam Fact
new int[3][4]1 outer array + 3 inner arrays of size 4Total 4 array objects created in Heap
new int[3][]1 outer array holding 3 null slotsAccessing a[0][0] throws NullPointerException
new int[][4]Invalid syntax; missing primary sizeFails at compile time with syntax error
a.clone() on 2DShallow copy of outer row referencesModifying copy[0][0] also mutates original[0][0]

Multi-Dimensional Arrays (2D) and Jagged Arrays in Java Top 20 Bilingual MCQs with Answers

Q1. How are 2D arrays actually represented in Java's Heap memory?

(जावा की हीप मेमोरी में 2D एरे वास्तव में किस रूप में संग्रहीत होते हैं?)

(A) As a single column-major contiguous block

(B) As a single row-major contiguous memory block

(C) As a doubly linked list of integers

(D) As an array of references to separate 1D array objects

Correct Answer: (D)

[EN] Java stores multi-dimensional arrays as arrays of arrays, where the outer array stores references to inner 1D arrays.

[HI] जावा मल्टी-डायमेंशनल एरे को एरे-ऑफ़-एरेज़ के रूप में रखता है जहाँ बाहरी एरे आंतरिक 1D एरे के रेफरेंस संभालता है।

Q2. How many separate array objects are allocated in Heap memory by executing int[][] m = new int[4][3];?

(कोड int[][] m = new int[4][3]; निष्पादित करने पर हीप मेमोरी में कुल कितने एरे ऑब्जेक्ट बनते हैं?)

(A) 1

(B) 5

(C) 12

(D) 4

Correct Answer: (B)

[EN] The JVM creates 1 outer int[][] array of length 4 plus 4 inner int[] arrays of length 3, totaling 5 array objects.

[HI] JVM लंबाई 4 का 1 बाहरी int[][] एरे और लंबाई 3 के 4 आंतरिक int[] एरे बनाता है, अर्थात कुल 5 ऑब्जेक्ट।

Q3. Which 2D array instantiation causes a compile-time error in Java?

(जावा में निम्न में से कौन-सा 2D एरे इंस्टैंशिएशन कंपाइल-टाइम एरर उत्पन्न करेगा?)

(A) int[] a[] = {{1}, {2, 3}};

(B) int[][] a = new int[5][5];

(C) int[][] a = new int[][5];

(D) int[][] a = new int[5][];

Correct Answer: (C)

[EN] The first (outermost) dimension must always be specified when instantiating with new; leaving it blank is illegal.

[HI] new से इंस्टैंशिएशन करते समय पहला (आउटर) डायमेंशन देना अनिवार्य है; इसे खाली छोड़ना कंपाइल एरर है।

Q4. What happens if you run: int[][] a = new int[2][]; System.out.println(a[0][0]);?

(कोड int[][] a = new int[2][]; System.out.println(a[0][0]); चलाने पर क्या होगा?)

(A) Compile-time error

(B) Throws NullPointerException at runtime

(C) Prints 0

(D) Throws ArrayIndexOutOfBoundsException

Correct Answer: (B)

[EN] Because the inner dimension was omitted, a[0] defaults to null; dereferencing a[0][0] throws NullPointerException.

[HI] आंतरिक डायमेंशन न देने से a[0] का डिफ़ॉल्ट मान null रहता है, और a[0][0] एक्सेस करने पर NullPointerException आता है।

Q5. What is a jagged (or ragged) array in Java?

(जावा में जैग्ड (Jagged या Ragged) एरे क्या होता है?)

(A) A 1D array with negative indices

(B) An array sorted in alternating ascending/descending order

(C) A multi-dimensional array whose rows have different lengths

(D) An array whose elements have mixed primitive types

Correct Answer: (C)

[EN] Jagged arrays allow each inner 1D row array to be instantiated with an independent column size.

[HI] जैग्ड एरे ऐसा बहु-आयामी एरे है जिसकी अलग-अलग पंक्तियों की लंबाई (कॉलम संख्या) भिन्न हो सकती है।

Q6. For int[][] a = {{1, 2}, {3, 4, 5}, {6}}; what is the value of a.length + a[1].length?

(यदि int[][] a = {{1, 2}, {3, 4, 5}, {6}}; हो, तो a.length + a[1].length का मान क्या होगा?)

(A) 5

(B) 6

(C) 9

(D) 7

Correct Answer: (B)

[EN] a.length equals 3 (number of rows) and a[1].length equals 3 (elements in second row), summing to 6.

[HI] a.length का मान 3 (पंक्तियां) और a[1].length का मान 3 (दूसरी पंक्ति के तत्व) है, जिनका योग 6 होता है।

Q7. After int[] x[], y; is declared, what are the data types of x and y?

(int[] x[], y; घोषित होने के बाद x और y के डेटा टाइप क्या होंगे?)

(A) Compile-time syntax error

(B) x is 2D and y is primitive int

(C) x is a 2D int[][] array and y is a 1D int[] array

(D) Both x and y are 2D int[][] arrays

Correct Answer: (C)

[EN] The int[] prefix applies to both x and y, while the trailing [] on x gives x one extra dimension.

[HI] प्रारंभिक int[] दोनों x और y पर लागू होता है, तथा x के बाद लगा [] x को 2D एरे बनाता है जबकि y 1D रहता है।

Q8. Why is calling grid[i].length essential in the inner loop condition when traversing a jagged 2D array?

(जैग्ड 2D एरे को ट्रैवर्स करते समय इनर लूप की कंडीशन में grid[i].length लिखना क्यों आवश्यक होता है?)

(A) grid[0].length always returns 0

(B) Java prohibits numeric literals inside inner loop headers

(C) Each row can have a different column count, preventing missed elements or bounds exceptions

(D) grid.length returns total element count instead of row count

Correct Answer: (C)

[EN] Relying on grid[0].length fails when later rows are longer or shorter than row 0; grid[i].length adapts per row.

[HI] बाद की पंक्तियाँ पहली पंक्ति से छोटी या बड़ी हो सकती हैं, इसलिए grid[i].length प्रत्येक पंक्ति के सटीक आकार के अनुसार चलता है।

Q9. What does int[][] a = new int[2][]; System.out.print(a[1]); print?

(कोड int[][] a = new int[2][]; System.out.print(a[1]); क्या प्रिंट करेगा?)

(A) [[I@...

(B) Throws NullPointerException

(C) null

(D) 0

Correct Answer: (C)

[EN] Unallocated inner row slots in new int[2][] hold null, and println prints null without dereferencing.

[HI] आवंटित न की गई पंक्तियां null रखती हैं और System.out.print(null) बिना किसी त्रुटि के null प्रिंट करता है।

Q10. If int[][] orig = {{1, 2}, {3, 4}}; int[][] copy = orig.clone(); copy[0][1] = 99; what is orig[0][1]?

(यदि int[][] orig = {{1, 2}, {3, 4}}; int[][] copy = orig.clone(); copy[0][1] = 99; चलाया जाए, तो orig[0][1] क्या होगा?)

(A) Throws CloneNotSupportedException

(B) 2

(C) 0

(D) 99

Correct Answer: (D)

[EN] clone() on a 2D array performs a shallow copy of the outer row references, so orig[0] and copy[0] point to the same 1D array.

[HI] 2D एरे पर clone() केवल पंक्ति रेफरेंस की शैलो कॉपी बनाता है, इसलिए orig[0] और copy[0] एक ही 1D एरे को दर्शाते हैं।

Q11. What is the time complexity of swapping two entire rows in a Java N x M 2D array using temp = a[r1]; a[r1] = a[r2]; a[r2] = temp;?

(जावा के N x M 2D एरे में temp = a[r1]; a[r1] = a[r2]; a[r2] = temp; द्वारा दो पंक्तियां बदलने की टाइम कॉम्प्लेक्सिटी क्या है?)

(A) O(N)

(B) O(N * M)

(C) O(M)

(D) O(1)

Correct Answer: (D)

[EN] Because a[r1] and a[r2] are merely references to 1D row arrays, swapping them moves two pointers in constant O(1) time.

[HI] चूंकि a[r1] और a[r2] केवल 1D पंक्ति एरे के रेफरेंस हैं, उन्हें स्वैप करने में केवल नियत O(1) समय लगता है।

Q12. Which nested enhanced for-each syntax correctly prints every int in a 2D array int[][] mat?

(2D एरे int[][] mat के सभी तत्व प्रिंट करने के लिए कौन-सा नेस्टेड for-each सिंटैक्स सही है?)

(A) for (int val : mat) { System.out.print(val); }

(B) for (int[] row : mat) { for (int val : row) { System.out.print(val); } }

(C) for (int[][] row : mat) { for (int val : row) {} }

(D) for (int val : mat[]) {}

Correct Answer: (B)

[EN] Each element of int[][] mat is an int[] reference, which is then traversed element-by-element as int.

[HI] int[][] mat का प्रत्येक तत्व एक int[] रेफरेंस होता है, जिसे अंदरूनी लूप में int के रूप में ट्रैवर्स किया जाता है।

Q13. What happens when you execute int[][] a = {{1, 2}, {3}}; System.out.println(a[1][1]);?

(कोड int[][] a = {{1, 2}, {3}}; System.out.println(a[1][1]); चलाने पर क्या होगा?)

(A) Prints null

(B) Prints 0

(C) Compile-time error

(D) Throws ArrayIndexOutOfBoundsException at runtime

Correct Answer: (D)

[EN] Row 1 has length 1 (only index 0 is valid), so accessing index 1 throws ArrayIndexOutOfBoundsException.

[HI] पंक्ति 1 की लंबाई 1 है (केवल इंडेक्स 0 वैध है), इसलिए इंडेक्स 1 एक्सेस करने पर ArrayIndexOutOfBoundsException आता है।

Q14. Which of the following is NOT true about Java 2D and jagged arrays?

(जावा 2D और जैग्ड एरे के बारे में निम्न में से कौन-सा कथन सत्य नहीं है?)

(A) All rows in a Java 2D array must reside contiguously in a single flat memory block

(B) Jagged arrays save memory when storing triangular matrices

(C) Individual rows can be replaced with new 1D arrays of different lengths

(D) Inner rows can be initialized to zero-length arrays like new int[0]

Correct Answer: (A)

[EN] Unlike C, Java allocates each row as a separate Heap object, so rows are neither required to be contiguous nor equal in length.

[HI] C के विपरीत, जावा में प्रत्येक पंक्ति हीप पर अलग ऑब्जेक्ट के रूप में बनती है, इसलिए पंक्तियों का सन्निहित या समान आकार का होना आवश्यक नहीं है।

Q15. How many total integer elements exist in int[][] a = {new int[2], new int[4], new int[0]};?

(int[][] a = {new int[2], new int[4], new int[0]}; में कुल कितने int तत्व मौजूद हैं?)

(A) 0

(B) 6

(C) 8

(D) Compile-time error due to new int[0]

Correct Answer: (B)

[EN] The three rows contain 2, 4, and 0 integers respectively, giving a total of 6 integer slots.

[HI] तीनों पंक्तियों में क्रमशः 2, 4 और 0 पूर्णांक हैं, इसलिए कुल 6 int तत्व मौजूद हैं।

Q16. Can an outer row of int[][] mat be assigned to a standalone 1D reference int[] r = mat[0];?

(क्या int[][] mat की किसी पंक्ति को सीधे 1D रेफरेंस int[] r = mat[0]; में असाइन किया जा सकता है?)

(A) Yes, because each element of int[][] is of type int[]

(B) Only if mat is rectangular

(C) No, 2D arrays cannot decompose into 1D arrays

(D) Requires explicit (int[]) casting

Correct Answer: (A)

[EN] Since mat is an array of int[] references, mat[0] already has exact static type int[].

[HI] चूंकि mat वास्तव में int[] रेफरेंस का एरे है, इसलिए mat[0] का टाइप पहले से ही int[] होता है।

Q17. Within Multi-Dimensional Arrays (2D) and Jagged Arrays in Java, which 2d construct directly maps to: "1 outer array + 3 inner arrays of size 4"?

(Multi-Dimensional Arrays (2D) and Jagged Arrays in Java में कौन सा 2D Construct सीधे "1 outer array + 3 inner arrays of size 4" से संबंधित है?)

(A) a.clone() on 2D

(B) new int[3][4]

(C) new int[][4]

(D) new int[3][]

Correct Answer: (B)

[EN] In Multi-Dimensional Arrays (2D) and Jagged Arrays in Java, 'new int[3][4]' handles '1 outer array + 3 inner arrays of size 4' (Total 4 array objects created in Heap).

[HI] Multi-Dimensional Arrays (2D) and Jagged Arrays in Java में 'new int[3][4]' का कार्य/विवरण '1 outer array + 3 inner arrays of size 4' (Total 4 array objects created in Heap) है।

Q18. Which option accurately describes 'new int[3][]' in Multi-Dimensional Arrays (2D) and Jagged Arrays in Java?

(Multi-Dimensional Arrays (2D) and Jagged Arrays in Java के संदर्भ में 'new int[3][]' की सही विशेषता कौन सी है?)

(A) 1 outer array + 3 inner arrays of size 4

(B) 1 outer array holding 3 null slots

(C) Shallow copy of outer row references

(D) Invalid syntax; missing primary size

Correct Answer: (B)

[EN] 'new int[3][]' provides '1 outer array holding 3 null slots' — key point: Accessing a[0][0] throws NullPointerException.

[HI] 'new int[3][]' का मुख्य विवरण '1 outer array holding 3 null slots' है (Accessing a[0][0] throws NullPointerException)।

Q19. In Multi-Dimensional Arrays (2D) and Jagged Arrays in Java, identify the correct parameter for the specification: "Invalid syntax; missing primary size".

(Multi-Dimensional Arrays (2D) and Jagged Arrays in Java में "Invalid syntax; missing primary size" विवरण किस विकल्प के लिए सही है?)

(A) a.clone() on 2D

(B) new int[3][]

(C) new int[][4]

(D) new int[3][4]

Correct Answer: (C)

[EN] 'new int[][4]' corresponds to 'Invalid syntax; missing primary size' (Fails at compile time with syntax error).

[HI] 'new int[][4]' का संबंध 'Invalid syntax; missing primary size' (Fails at compile time with syntax error) से है।

Q20. Considering Multi-Dimensional Arrays (2D) and Jagged Arrays in Java, what key behavior defines 'a.clone() on 2D'?

(Multi-Dimensional Arrays (2D) and Jagged Arrays in Java में 'a.clone() on 2D' की मुख्य भूमिका या मान क्या है?)

(A) Invalid syntax; missing primary size

(B) 1 outer array + 3 inner arrays of size 4

(C) Shallow copy of outer row references

(D) 1 outer array holding 3 null slots

Correct Answer: (C)

[EN] 'a.clone() on 2D' represents 'Shallow copy of outer row references' (Modifying copy[0][0] also mutates original[0][0]).

[HI] 'a.clone() on 2D' का अर्थ/मान 'Shallow copy of outer row references' है (Modifying copy[0][0] also mutates original[0][0])।

Multi-Dimensional Arrays (2D) and Jagged Arrays in Java — Exam FAQs

Q1: Can a Java 2D array store a zero-length row alongside normal rows?

क्या जावा के 2D एरे में सामान्य पंक्तियों के साथ शून्य-लंबाई वाली पंक्ति भी हो सकती है?

[EN] Yes. Setting a[1] = new int[0]; creates an empty non-null row whose a[1].length is 0, preventing NullPointerException during nested iteration.

[HI] हाँ। a[1] = new int[0]; लिखने से 0 लंबाई वाली वैध पंक्ति बनती है जो null नहीं होती, जिससे नेस्टेड लूप में NullPointerException नहीं आता।

Q2: How do you perform a deep copy of a 2D int[][] array in Java?

जावा में 2D int[][] एरे की डीप कॉपी (Deep Copy) कैसे की जाती है?

[EN] Allocate a new outer array and clone or Arrays.copyOf each individual row in a loop so inner 1D arrays are not shared.

[HI] पहले एक नया आउटर एरे बनाएं और फिर लूप चलाकर प्रत्येक पंक्ति पर clone() या Arrays.copyOf() लागू करें ताकि आंतरिक 1D एरे साझा न रहें।

Q3: Does Java support 3D or higher-dimensional arrays?

क्या जावा 3D या उससे उच्च आयामी एरे का समर्थन करता है?

[EN] Yes. Java supports arbitrary nesting like int[][][] cube = new int[2][3][4]; up to 255 array dimensions per the JVM specification.

[HI] हाँ। JVM स्पेसिफिकेशन के अनुसार जावा 255 आयामों तक int[][][] जैसे बहु-आयामी एरे का समर्थन करता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)