PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Java Arrays: 1D Array Declaration, Instantiation and Initialization (1D एरे: डिक्लेरेशन, new कीवर्ड, लेंथ (.length) प्रॉपर्टी) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: A 1D array in Java is a fixed-size, dynamically allocated object stored in Heap memory holding elements of a homogeneous type. Every array exposes a public final length property and triggers automatic JVM runtime bounds verification.
अध्याय सार: जावा में 1D एरे हीप मेमोरी में डायनामिक रूप से आवंटित एक निश्चित-आकार का ऑब्जेक्ट है जो समान डेटा टाइप के तत्वों को संग्रहीत करता है। प्रत्येक एरे एक public final length प्रॉपर्टी प्रदान करता है और JVM द्वारा स्वचालित बाउंड्स जांच का समर्थन करता है।
Java Arrays: 1D Array Declaration, Instantiation and Initialization: Detailed Notes & Architecture (1D एरे: डिक्लेरेशन, new कीवर्ड, लेंथ (.length) प्रॉपर्टी)
1. Array Declaration Syntax and Compile-Time Rules (एरे डिक्लेरेशन सिंटैक्स और कंपाइल-टाइम नियम)
[EN] In Java, a 1D array reference can be declared either as int[] arr; (recommended) or C-style int arr[];. Declaring an array creates only a reference variable on the stack; no memory for elements is allocated yet. Specifying the array size inside brackets during declaration—such as int[5] arr;—produces a compile-time syntax error. When declaring multiple variables together like int[] a, b;, both a and b are arrays, whereas in int a[], b;, only a is an array and b is a plain int.
[HI] जावा में 1D एरे रेफरेंस को int[] arr; (अनुशंसित) या C-स्टाइल int arr[]; दोनों तरह से घोषित किया जा सकता है। डिक्लेरेशन से केवल स्टैक पर रेफरेंस वेरिएबल बनता है; तत्वों के लिए मेमोरी आवंटित नहीं होती। डिक्लेरेशन में साइज लिखना जैसे int[5] arr; कंपाइल-टाइम सिंटैक्स एरर उत्पन्न करता है। int[] a, b; में a और b दोनों एरे होते हैं, जबकि int a[], b; में केवल a एरे और b सामान्य int होता है।
2. Instantiation with new and Default Element Values (new कीवर्ड से इंस्टैंशिएशन और डिफ़ॉल्ट मान)
[EN] Memory allocation happens on the Heap using the new operator: arr = new int[5];. Upon instantiation, the JVM automatically initializes every slot to its type default: 0 for integers, 0.0 for floating-point, false for boolean, '\u0000' for char, and null for object references. Creating a zero-length array via new int[0] is completely legal and produces a valid non-null object whose length is 0. Passing a negative integer size compiles cleanly if held in a variable, but throws NegativeArraySizeException at runtime.
[HI] मेमोरी आवंटन हीप मेमोरी में new ऑपरेटर द्वारा होता है: arr = new int[5];। इंस्टैंशिएशन के समय JVM प्रत्येक स्लॉट को डिफ़ॉल्ट मान देता है: पूर्णांकों के लिए 0, फ्लोट/डबल के लिए 0.0, boolean के लिए false, char के लिए '\u0000' और ऑब्जेक्ट्स के लिए null। new int[0] द्वारा शून्य-लंबाई का एरे बनाना पूर्णतः वैध है और यह 0 लंबाई वाला वैध ऑब्जेक्ट बनाता है। वेरिएबल के माध्यम से ऋणात्मक साइज देने पर कोड कंपाइल तो हो जाता है लेकिन रनटाइम पर NegativeArraySizeException फेंकता है।
3. Inline Initialization and Anonymous Arrays (इनलाइन इनिशियलाइज़ेशन और एनोनिमस एरे)
[EN] When elements are known ahead of time, shorthand initialization such as int[] nums = {10, 20, 30}; declares, allocates, and populates the array in one line. However, the brace-only initializer syntax cannot be separated from declaration; writing nums = {10, 20}; later fails to compile. For reassignment or passing an array inline to a method, Java provides anonymous arrays: new int[]{10, 20, 30}. You must never specify a numeric size alongside an initializer list, so new int[3]{1, 2, 3} is illegal.
[HI] यदि तत्व पहले से ज्ञात हों, तो int[] nums = {10, 20, 30}; जैसा शॉर्टहैंड एक ही पंक्ति में घोषणा, आवंटन और मान भराव कर देता है। लेकिन केवल कर्ली ब्रेसेस सिंटैक्स को डिक्लेरेशन से अलग नहीं किया जा सकता; बाद में nums = {10, 20}; लिखने पर कंपाइल एरर आती है। पुनः असाइनमेंट या मेथड को सीधे एरे पास करने के लिए एनोनिमस एरे new int[]{10, 20, 30} का उपयोग होता है। इनिशियलाइज़र लिस्ट के साथ कभी भी संख्यात्मक साइज नहीं लिखा जाता, इसलिए new int[3]{1, 2, 3} अमान्य है।
4. The .length Property and Array Object Internals (.length प्रॉपर्टी और एरे ऑब्जेक्ट की आंतरिक संरचना)
[EN] Every Java array is a first-class object whose runtime class directly extends java.lang.Object and implements both Cloneable and java.io.Serializable. Array size is exposed via the public final field .length (without parentheses), whereas String uses the .length() method. Valid 0-based indices span from 0 to arr.length - 1, and array capacity is constrained by 32-bit int indexing (up to roughly Integer.MAX_VALUE). Because .length is final, assigning arr.length = 10 causes a compile-time error—an array cannot grow or shrink after creation.
[HI] जावा में प्रत्येक एरे एक प्रथम श्रेणी का ऑब्जेक्ट है जिसकी रनटाइम क्लास सीधे java.lang.Object को एक्सटेंड करती है तथा Cloneable और java.io.Serializable को इम्प्लीमेंट करती है। एरे का आकार बिना कोष्ठक वाली public final प्रॉपर्टी .length से मिलता है, जबकि String में .length() मेथड होता है। वैध 0-आधारित इंडेक्स 0 से arr.length - 1 तक होते हैं और अधिकतम क्षमता 32-बिट int इंडेक्स (लगभग Integer.MAX_VALUE) से सीमित होती है। .length के final होने के कारण arr.length = 10 लिखने पर कंपाइल एरर आती है—निर्माण के बाद एरे का आकार बदला नहीं जा सकता।
Syntax, Formula & Technical Example (Java Arrays: 1D Array Declaration, Instantiation and Initialization)
int[] marks = new int[4];
marks[0] = 85;
marks[1] = 90;
String[] labs = new String[]{"Java", "DBMS"};
System.out.println(marks.length);
System.out.println(marks[3]);
System.out.println(labs[1]);
Java Arrays: 1D Array Declaration, Instantiation and Initialization Quick Reference & Comparison Table
| Array Aspect | Java Specification | Exam Fact |
|---|---|---|
| Size Access | arr.length public final int field | No parentheses; String uses length() method instead |
| Default Value (int) | 0 assigned automatically in Heap | boolean gets false; objects get null; char gets NUL |
| Negative Size | Compiles via variable; runtime exception | Throws NegativeArraySizeException at instantiation |
| Superclass & Interfaces | Extends Object; Cloneable & Serializable | Supports clone() directly without CloneNotSupportedException |
Java Arrays: 1D Array Declaration, Instantiation and Initialization Top 20 Bilingual MCQs with Answers
Q1. How do you correctly obtain the number of elements in a Java array named arr?
(जावा में arr नामक एरे में तत्वों की संख्या सही तरीके से कैसे प्राप्त की जाती है?)
(A) sizeof(arr)
(B) arr.size()
(C) arr.length
(D) arr.length()
Correct Answer: (C)
[EN] Java arrays expose a public final field named length (without parentheses), unlike String.length() or List.size().
[HI] जावा एरे में बिना कोष्ठक वाली public final प्रॉपर्टी length होती है, जो String.length() और List.size() से भिन्न है।
Q2. What is the default value assigned to elements of new boolean[3] in Java?
(जावा में new boolean[3] एरे के तत्वों को डिफ़ॉल्ट रूप से क्या मान मिलता है?)
(A) null
(B) true
(C) 0
(D) false
Correct Answer: (D)
[EN] Boolean array elements allocated on the Heap are automatically initialized to false by the JVM.
[HI] हीप पर बने boolean एरे के सभी तत्व JVM द्वारा स्वचालित रूप से false से इनिशियलाइज़ होते हैं।
Q3. Which statement causes a compile-time error in Java?
(जावा में निम्न में से कौन-सा स्टेटमेंट कंपाइल-टाइम एरर उत्पन्न करेगा?)
(A) int a[] = new int[0];
(B) int[] a = {1, 2, 3};
(C) int[] a = new int[3]{1, 2, 3};
(D) int[] a = new int[]{1, 2, 3};
Correct Answer: (C)
[EN] You cannot specify an array dimension size when providing an explicit curly-brace initializer list.
[HI] इनिशियलाइज़र लिस्ट के साथ एरे का साइज संख्यात्मक रूप से निर्दिष्ट करने पर कंपाइल एरर आती है।
Q4. What happens when executing int n = -4; int[] a = new int[n];?
(कोड int n = -4; int[] a = new int[n]; चलाने पर क्या होता है?)
(A) Fails compilation due to negative constant
(B) Compiles cleanly and throws NegativeArraySizeException at runtime
(C) Throws ArrayIndexOutOfBoundsException
(D) Creates an empty array of length 0
Correct Answer: (B)
[EN] javac accepts any int expression for size, so negative size is caught by the JVM at runtime with NegativeArraySizeException.
[HI] javac साइज के लिए किसी भी int एक्सप्रेशन को स्वीकार करता है, लेकिन रनटाइम पर JVM NegativeArraySizeException फेंकता है।
Q5. After executing int a[], b;, what are the types of a and b?
(int a[], b; घोषित करने के बाद a और b के डेटा टाइप क्या होंगे?)
(A) Both a and b are int[] arrays
(B) Compile-time syntax error
(C) a is an int[] array and b is a primitive int
(D) a is primitive int and b is int[]
Correct Answer: (C)
[EN] When brackets follow the variable name, they apply only to that variable; putting brackets after int applies to both.
[HI] वेरिएबल नाम के बाद कोष्ठक लगाने पर वह केवल उसी वेरिएबल पर लागू होता है, इसलिए a एरे और b सामान्य int है।
Q6. Which interfaces does every Java array automatically implement?
(जावा में प्रत्येक एरे स्वचालित रूप से किन इंटरफ़ेस को इम्प्लीमेंट करता है?)
(A) Comparable and Iterable
(B) Cloneable and java.io.Serializable
(C) AutoCloseable and Runnable
(D) List and RandomAccess
Correct Answer: (B)
[EN] Per the Java Language Specification, every array type implements Cloneable and java.io.Serializable.
[HI] जावा लैंग्वेज स्पेसिफिकेशन के अनुसार प्रत्येक एरे टाइप Cloneable और java.io.Serializable को इम्प्लीमेंट करता है।
Q7. Where are Java array elements and the array object itself stored at runtime?
(रनटाइम पर जावा एरे ऑब्जेक्ट और उसके तत्व किस मेमोरी क्षेत्र में संग्रहीत होते हैं?)
(A) Heap memory
(B) JVM Call Stack only
(C) Method Area Constant Pool
(D) CPU Register File
Correct Answer: (A)
[EN] All arrays in Java are objects dynamically allocated in Heap memory, while the reference variable may live on the stack.
[HI] जावा में सभी एरे ऑब्जेक्ट होते हैं जो डायनामिक रूप से हीप मेमोरी में आवंटित होते हैं।
Q8. What is the default value of elements in new String[5]?
(new String[5] के तत्वों का डिफ़ॉल्ट मान क्या होता है?)
(A) null
(B) ""
(C) "null"
(D) Compile-time uninitialized error
Correct Answer: (A)
[EN] All object reference arrays initialize each element slot to null until assigned an instance.
[HI] सभी ऑब्जेक्ट रेफरेंस एरे के प्रत्येक स्लॉट का डिफ़ॉल्ट मान null होता है।
Q9. What is the valid data type for specifying an array's size inside new int[size]?
(new int[size] के भीतर एरे का साइज बताने के लिए कौन-सा डेटा टाइप मान्य है?)
(A) boolean and int
(B) long and double
(C) float only
(D) byte, short, char, or int (implicitly widened to int)
Correct Answer: (D)
[EN] Array dimension sizes must be promotable to int; long, float, and double cause a lossy conversion compile error.
[HI] एरे का साइज int या उससे छोटे टाइप (byte, short, char) का ही हो सकता है; long या double से कंपाइल एरर आती है।
Q10. What does System.out.println(new int[0].length); print?
(System.out.println(new int[0].length); क्या प्रिंट करेगा?)
(A) null
(B) 0
(C) Throws NegativeArraySizeException
(D) Compile-time error
Correct Answer: (B)
[EN] A zero-length array is completely legal in Java and has a valid .length equal to 0.
[HI] जावा में शून्य-लंबाई वाला एरे बनाना वैध है और इसकी .length प्रॉपर्टी 0 होती है।
Q11. Which statement assigns an anonymous array to an existing variable int[] x;?
(पहले से घोषित वेरिएबल int[] x; को एनोनिमस एरे असाइन करने का सही तरीका कौन-सा है?)
(A) x = new int[3]{4, 5, 6};
(B) x = {4, 5, 6};
(C) x = new int[]{4, 5, 6};
(D) x = int[]{4, 5, 6};
Correct Answer: (C)
[EN] Standalone brace initializers work only during declaration; later reassignment requires new int[]{...}.
[HI] केवल ब्रेसेस वाला इनिशियलाइज़र डिक्लेरेशन के समय ही चलता है; बाद में असाइनमेंट के लिए new int[]{...} आवश्यक है।
Q12. What is the maximum index you can legally access in an array declared as double[] d = new double[10];?
(double[] d = new double[10]; में आप अधिकतम किस वैध इंडेक्स को एक्सेस कर सकते हैं?)
(A) 9
(B) 10
(C) 8
(D) 11
Correct Answer: (A)
[EN] Java arrays are 0-indexed, so valid indices for size 10 span from 0 through 9.
[HI] जावा एरे 0-आधारित इंडेक्स का उपयोग करते हैं, इसलिए 10 आकार के एरे के वैध इंडेक्स 0 से 9 तक होते हैं।
Q13. What happens if you write int[5] arr; in a Java method?
(किसी जावा मेथड में int[5] arr; लिखने पर क्या होता है?)
(A) Allocates 5 integers on heap
(B) Compile-time syntax error because size cannot appear in declaration
(C) Initializes arr with 5
(D) Allocates 5 integers on stack
Correct Answer: (B)
[EN] Java separates reference declaration from memory allocation; size may only be given during new instantiation.
[HI] जावा डिक्लेरेशन में साइज की अनुमति नहीं देता; साइज केवल new द्वारा इंस्टैंशिएशन के समय ही दिया जा सकता है।
Q14. Which of the following is NOT true about 1D arrays in Java?
(जावा में 1D एरे के बारे में निम्न में से कौन-सा कथन सत्य नहीं है?)
(A) An array's size can be dynamically increased later by updating arr.length
(B) Uninitialized array elements receive deterministic default values
(C) Arrays are homogeneous containers storing elements of compatible types
(D) Arrays inherit methods from java.lang.Object
Correct Answer: (A)
[EN] Because length is a final field, an array's size is immutable once allocated; resizing requires creating a new array.
[HI] length एक final फ़ील्ड है, इसलिए एक बार आवंटित होने के बाद एरे का आकार बदला नहीं जा सकता।
Q15. What default value is stored in elements of a newly created char[3] array?
(नए बनाए गए char[3] एरे के तत्वों में कौन-सा डिफ़ॉल्ट मान संग्रहीत होता है?)
(A) Unicode NUL character ('\u0000', numeric code 0)
(B) Space character (' ')
(C) Character '0'
(D) null reference
Correct Answer: (A)
[EN] Primitive char arrays default to the 16-bit Unicode null character '\u0000' whose integer value is 0.
[HI] प्रिमिटिव char एरे का डिफ़ॉल्ट मान 16-बिट यूनिकोड NUL कैरेक्टर '\u0000' (संख्यात्मक मान 0) होता है।
Q16. If Object x = new int[3]; is executed, is the assignment valid in Java?
(यदि Object x = new int[3]; लिखा जाए, तो क्या यह असाइनमेंट जावा में वैध है?)
(A) Compiles but throws ClassCastException
(B) No, only String[] extends Object
(C) No, primitive arrays are not objects
(D) Yes, because every primitive array is an instance of java.lang.Object
Correct Answer: (D)
[EN] Every Java array—whether primitive or reference type—is an object whose direct superclass is java.lang.Object.
[HI] जावा में प्रत्येक एरे—चाहे प्रिमिटिव हो या रेफरेंस—एक ऑब्जेक्ट है जिसकी सुपरक्लास java.lang.Object होती है।
Q17. Within Java Arrays: 1D Array Declaration, Instantiation and Initialization, which array aspect directly maps to: "arr.length public final int field"?
(Java Arrays: 1D Array Declaration, Instantiation and Initialization में कौन सा Array Aspect सीधे "arr.length public final int field" से संबंधित है?)
(A) Default Value (int)
(B) Size Access
(C) Negative Size
(D) Superclass & Interfaces
Correct Answer: (B)
[EN] In Java Arrays: 1D Array Declaration, Instantiation and Initialization, 'Size Access' handles 'arr.length public final int field' (No parentheses; String uses length() method instead).
[HI] Java Arrays: 1D Array Declaration, Instantiation and Initialization में 'Size Access' का कार्य/विवरण 'arr.length public final int field' (No parentheses; String uses length() method instead) है।
Q18. Which option accurately describes 'Default Value (int)' in Java Arrays: 1D Array Declaration, Instantiation and Initialization?
(Java Arrays: 1D Array Declaration, Instantiation and Initialization के संदर्भ में 'Default Value (int)' की सही विशेषता कौन सी है?)
(A) Compiles via variable; runtime exception
(B) Extends Object; Cloneable & Serializable
(C) arr.length public final int field
(D) 0 assigned automatically in Heap
Correct Answer: (D)
[EN] 'Default Value (int)' provides '0 assigned automatically in Heap' — key point: boolean gets false; objects get null; char gets NUL.
[HI] 'Default Value (int)' का मुख्य विवरण '0 assigned automatically in Heap' है (boolean gets false; objects get null; char gets NUL)।
Q19. In Java Arrays: 1D Array Declaration, Instantiation and Initialization, identify the correct parameter for the specification: "Compiles via variable; runtime exception".
(Java Arrays: 1D Array Declaration, Instantiation and Initialization में "Compiles via variable; runtime exception" विवरण किस विकल्प के लिए सही है?)
(A) Negative Size
(B) Default Value (int)
(C) Superclass & Interfaces
(D) Size Access
Correct Answer: (A)
[EN] 'Negative Size' corresponds to 'Compiles via variable; runtime exception' (Throws NegativeArraySizeException at instantiation).
[HI] 'Negative Size' का संबंध 'Compiles via variable; runtime exception' (Throws NegativeArraySizeException at instantiation) से है।
Q20. Considering Java Arrays: 1D Array Declaration, Instantiation and Initialization, what key behavior defines 'Superclass & Interfaces'?
(Java Arrays: 1D Array Declaration, Instantiation and Initialization में 'Superclass & Interfaces' की मुख्य भूमिका या मान क्या है?)
(A) arr.length public final int field
(B) Compiles via variable; runtime exception
(C) 0 assigned automatically in Heap
(D) Extends Object; Cloneable & Serializable
Correct Answer: (D)
[EN] 'Superclass & Interfaces' represents 'Extends Object; Cloneable & Serializable' (Supports clone() directly without CloneNotSupportedException).
[HI] 'Superclass & Interfaces' का अर्थ/मान 'Extends Object; Cloneable & Serializable' है (Supports clone() directly without CloneNotSupportedException)।
Java Arrays: 1D Array Declaration, Instantiation and Initialization — Exam FAQs
Q1: Why is int[] arr preferred over int arr[] in Java?
जावा में int arr[] की तुलना में int[] arr को प्राथमिकता क्यों दी जाती है?
[EN] Writing int[] places the type together clearly as 'array of int', prevents confusion in multi-variable declarations like int[] a, b, and aligns with Java coding conventions.
[HI] int[] लिखने से डेटा टाइप 'array of int' स्पष्ट दिखता है, एक साथ कई वेरिएबल घोषित करने पर भ्रम नहीं होता और यह जावा कोडिंग मानकों के अनुरूप है।
Q2: Can a Java array hold both int and String values in the same array?
क्या जावा का एक ही एरे int और String दोनों प्रकार के मान एक साथ रख सकता है?
[EN] Only if declared as Object[] (using autoboxing for Integer), because all elements must be assignment-compatible with the array's declared component type.
[HI] केवल Object[] के रूप में घोषित करने पर (जहाँ int ऑटोबॉक्स होकर Integer बनता है), अन्यथा एरे केवल अपने घोषित टाइप के संगत तत्व ही रख सकता है।
Q3: Why does Java use 0-based indexing for arrays?
जावा एरे के लिए 0-आधारित इंडेक्सिंग का उपयोग क्यों करता है?
[EN] Index represents the offset distance from the starting base address of the array (address = base + index * elementSize), making element 0 have zero offset.
[HI] इंडेक्स एरे के शुरुआती बेस एड्रेस से ऑफ़सेट दूरी को दर्शाता है (address = base + index * size), जहाँ पहले तत्व का ऑफ़सेट 0 होता है।
Post a Comment
Post a Comment