PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings (नॉन-प्रिमिटिव डेटा प्रकार: क्लास, इंटरफ़ेस, एरे और रेफरेंस) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Non-primitive (reference) data types in Java—comprising Classes, Interfaces, Arrays, and Strings—store memory references pointing to heap-allocated objects rather than holding raw values directly. All reference types share a default value of null, inherit capabilities from java.lang.Object, and support method invocation and polymorphic behavior.
अध्याय सार: जावा में नॉन-प्रिमिटिव (रेफरेंस) डेटा प्रकार—जिनमें Classes, Interfaces, Arrays और Strings शामिल हैं—सीधे मान रखने के बजाय हीप पर आवंटित ऑब्जेक्ट्स के मेमोरी रेफरेंस संग्रहीत करते हैं। सभी रेफरेंस प्रकारों का डिफ़ॉल्ट मान null होता है, वे java.lang.Object से गुण प्राप्त करते हैं और मेथड कॉल तथा पॉलीमॉर्फ़िज़्म का समर्थन करते हैं।
Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings: Detailed Notes & Architecture (नॉन-प्रिमिटिव डेटा प्रकार: क्लास, इंटरफ़ेस, एरे और रेफरेंस)
1. Reference Semantics vs Primitive Storage (रेफरेंस सेमेंटिक्स बनाम प्रिमिटिव स्टोरेज)
[EN] Unlike primitive variables that directly hold binary values in stack slots or object fields, non-primitive variables hold references (object handles/addresses) pointing to objects created in the JVM Heap. When one reference variable is assigned to another (Student s2 = s1;), Java copies the reference rather than cloning the object, meaning both variables alias the exact same heap instance. All uninitialized reference fields default to null, and attempting to call a method or access a field through a null reference throws NullPointerException at runtime. Furthermore, comparing two references with '==' compares their memory identity, whereas '.equals()' checks logical equality when overridden.
[HI] प्रिमिटिव वेरिएबल्स के विपरीत जो सीधे बाइनरी मान रखते हैं, नॉन-प्रिमिटिव वेरिएबल्स JVM Heap में बने ऑब्जेक्ट का मेमोरी रेफरेंस रखते हैं। एक रेफरेंस वेरिएबल को दूसरे को सौंपने पर (Student s2 = s1;) ऑब्जेक्ट की नई प्रति बनने के बजाय केवल रेफरेंस कॉपी होता है, अर्थात दोनों वेरिएबल एक ही हीप ऑब्जेक्ट की ओर संकेत करते हैं। सभी बिना-इनिशियलाइज़ किए गए रेफरेंस फ़ील्ड्स का डिफ़ॉल्ट मान null होता है, और null रेफरेंस पर मेथड कॉल करने से रनटाइम पर NullPointerException आता है। दो रेफरेंस की '==' से तुलना उनके मेमोरी पते की जांच करती है, जबकि सामग्री की तार्किक तुलना के लिए '.equals()' मेथड प्रयुक्त होता है।
2. Classes and Interfaces as User-Defined Types (यूज़र-डिफ़ाइंड प्रकार के रूप में Classes और Interfaces)
[EN] A Class serves as a blueprint encapsulating fields (state) and methods (behavior), instantiated at runtime via the 'new' operator (or reflection/deserialization). Every class in Java implicitly extends java.lang.Object at the root of the inheritance tree, inheriting methods like toString(), equals(), hashCode(), and getClass(). An Interface defines a behavioral contract using implicitly public abstract methods (plus static, default, and private methods in Java 8/9+) and implicitly 'public static final' constants. Because a class can implement multiple interfaces simultaneously, interfaces provide polymorphism and multiple inheritance of type without the diamond ambiguity of multiple class inheritance.
[HI] Class डेटा फ़ील्ड्स और मेथड्स को समाहित करने वाला एक ब्लूप्रिंट है, जिसका ऑब्जेक्ट रनटाइम पर 'new' ऑपरेटर द्वारा बनाया जाता है। जावा में प्रत्येक क्लास प्रत्यक्ष या अप्रत्यक्ष रूप से java.lang.Object से इनहेरिट करती है, जिससे उसे toString(), equals(), hashCode() और getClass() जैसे मेथड मिलते हैं। Interface एक अनुबंध (contract) परिभाषित करता है जिसमें स्वतः public abstract मेथड (तथा Java 8/9+ में default, static और private मेथड) और स्वतः 'public static final' स्थिरांक होते हैं। एक क्लास एक साथ कई इंटरफ़ेस लागू कर सकती है, जिससे जावा में डायमंड समस्या के बिना टाइप का मल्टीपल इनहेरिटेंस संभव होता है।
3. Arrays as Fixed-Length Container Objects (फिक्स्ड-लेंथ कंटेनर ऑब्जेक्ट्स के रूप में Arrays)
[EN] In Java, arrays are genuine heap objects—even an array of primitives such as int[]—and directly inherit from java.lang.Object while implementing Cloneable and java.io.Serializable. Arrays store homogeneous elements in contiguous memory indexed from 0 to length - 1, exposing their fixed capacity through the final field 'arr.length' (note: a field, not a method like String.length()). Once an array is constructed with 'new int[5]', its length cannot grow or shrink, and accessing an index outside [0, length - 1] throws ArrayIndexOutOfBoundsException at runtime. Java also supports multidimensional jagged arrays where each row in 'int[][] grid = new int[3][];' can reference inner arrays of different lengths.
[HI] जावा में एरे वास्तविक हीप ऑब्जेक्ट्स होते हैं—यहाँ तक कि int[] जैसा प्रिमिटिव एरे भी java.lang.Object का उपप्रकार है तथा Cloneable और Serializable इंटरफ़ेस लागू करता है। एरे समरूप तत्वों को 0 से length - 1 इंडेक्स तक संग्रहीत करते हैं और अपनी निश्चित क्षमता 'arr.length' फ़ील्ड (ध्यान दें: मेथड नहीं) द्वारा दिखाते हैं। 'new int[5]' से बनने के बाद एरे का आकार बदला नहीं जा सकता, और वैध सीमा के बाहर इंडेक्स एक्सेस करने पर ArrayIndexOutOfBoundsException उत्पन्न होता है। जावा में जैग्ड (jagged) बहु-आयामी एरे भी समर्थित हैं जहाँ प्रत्येक पंक्ति में अलग-अलग लंबाई के आंतरिक एरे हो सकते हैं।
4. Strings, Immutability, and the String Constant Pool (Strings, इम्यूटेबिलिटी और String Constant Pool)
[EN] java.lang.String is a final non-primitive class representing immutable character sequences—any modification such as concat() or replace() creates a brand-new String object rather than altering the original instance. String literals (e.g., String s = "Java";) are interned inside the String Constant Pool (residing inside the Heap since Java 7) so identical literals share a single object to conserve memory. Conversely, invoking 'new String("Java")' forces allocation of a separate String object in the regular Heap outside the pool reference, making s1 == s2 false even though s1.equals(s2) is true. Since Java 9 (Compact Strings), String stores characters internally in a byte[] alongside a 1-byte coder flag (LATIN1 or UTF16) instead of always using 2-byte char[], cutting memory usage nearly in half for ASCII text.
[HI] java.lang.String एक final नॉन-प्रिमिटिव क्लास है जो इम्यूटेबल (अपरिवर्तनीय) कैरेक्टर अनुक्रम दर्शाती है—concat() या replace() जैसी कोई भी संक्रिया मूल ऑब्जेक्ट को बदलने के बजाय नया String ऑब्जेक्ट बनाती है। स्ट्रिंग लिटरल (जैसे String s = "Java";) String Constant Pool (Java 7 से Heap के भीतर) में संग्रहीत होते हैं ताकि समान लिटरल मेमोरी बचाने के लिए एक ही ऑब्जेक्ट साझा करें। इसके विपरीत 'new String("Java")' नियमित हीप में एक अलग ऑब्जेक्ट बनाता है, जिससे s1 == s2 false होता है जबकि s1.equals(s2) true होता है। Java 9 (Compact Strings) से String आंतरिक रूप से हमेशा char[] के बजाय कोडर फ़्लैग के साथ byte[] उपयोग करता है, जिससे ASCII टेक्स्ट की मेमोरी खपत आधी हो जाती है।
Syntax, Formula & Technical Example (Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings)
interface Printable { void show(); }
class Book implements Printable { public void show() { System.out.println("Book implements Printable"); } }
public class NonPrimitiveDemo {
public static void main(String[] args) {
Printable p = new Book(); p.show();
int[] scores = {90, 85, 95}; String s1 = "JVM", s2 = new String("JVM");
System.out.println("Len=" + scores.length + ", ==:" + (s1 == s2) + ", eq:" + s1.equals(s2));
}
}
Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings Quick Reference & Comparison Table
| Non-Primitive Type | Key Memory & Language Property | Key Exam Fact |
|---|---|---|
| Class | User-defined blueprint instantiated on Heap via new | Implicitly inherits java.lang.Object; supports single inheritance |
| Interface | Contract with abstract/default/static methods and constants | Cannot be instantiated directly; enables multiple type inheritance |
| Array | Fixed-size homogeneous container inheriting Object | Uses arr.length field (not length() method); zero-indexed |
| String | Final immutable class backed by String Constant Pool | Compare content with .equals(), never ==; pool moved to Heap in Java 7 |
Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings Top 20 Bilingual MCQs with Answers
Q1. What is the default value of any uninitialized non-primitive (reference) class field in Java?
(जावा में किसी भी बिना इनिशियलाइज़ किए गए नॉन-प्रिमिटिव (रेफरेंस) क्लास फ़ील्ड का डिफ़ॉल्ट मान क्या होता है?)
(A) null
(B) void
(C) 0
(D) ""
Correct Answer: (A)
[EN] All reference variables (Class, Interface, Array, String) default to null when declared as static or instance fields without explicit initialization.
[HI] बिना स्पष्ट इनिशियलाइज़ेशन के क्लास या इंस्टेंस फ़ील्ड के रूप में घोषित सभी रेफरेंस वेरिएबल्स (Class, Interface, Array, String) का डिफ़ॉल्ट मान null होता है।
Q2. Which class sits at the root of every non-primitive class hierarchy in Java?
(जावा में प्रत्येक नॉन-प्रिमिटिव क्लास पदानुक्रम के शीर्ष (root) पर कौन सी क्लास स्थित है?)
(A) java.lang.Class
(B) java.lang.String
(C) java.lang.System
(D) java.lang.Object
Correct Answer: (D)
[EN] Every class (and every array type) in Java implicitly extends java.lang.Object unless it explicitly extends another class that already descends from Object.
[HI] जावा में प्रत्येक क्लास (और प्रत्येक एरे) प्रत्यक्ष या अप्रत्यक्ष रूप से java.lang.Object को एक्सटेंड करती है।
Q3. Given: String a = "Java"; String b = new String("Java"); What does (a == b) evaluate to?
(दिया गया है: String a = "Java"; String b = new String("Java"); तब (a == b) का परिणाम क्या होगा?)
(A) true
(B) false
(C) Throws NullPointerException
(D) Compile-time error
Correct Answer: (B)
[EN] 'a' references the pooled String literal, whereas 'b' allocates a distinct object via 'new'; '==' compares object references and returns false, while a.equals(b) returns true.
[HI] 'a' स्ट्रिंग पूल वाले लिटरल को संदर्भित करता है जबकि 'b' 'new' द्वारा हीप में एक अलग ऑब्जेक्ट बनाता है; '==' रेफरेंस पते की तुलना करता है इसलिए false देता है।
Q4. How do you correctly obtain the number of elements in an array 'arr' versus characters in a String 'str'?
(किसी एरे 'arr' में तत्वों की संख्या और String 'str' में अक्षरों की संख्या सही ढंग से कैसे प्राप्त की जाती है?)
(A) arr.length and str.length()
(B) arr.size() and str.size()
(C) arr.length() and str.length
(D) arr.length() and str.length()
Correct Answer: (A)
[EN] Arrays expose a public final field named 'length', whereas String exposes a method named 'length()'.
[HI] एरे में 'length' एक फ़ील्ड होता है, जबकि String में 'length()' एक मेथड होता है।
Q5. Are arrays considered primitive or non-primitive (reference) types in Java when storing int values (int[])?
(जब किसी एरे में int मान संग्रहीत किए जाते हैं (int[]), तो क्या उसे जावा में प्रिमिटिव या नॉन-प्रिमिटिव माना जाता है?)
(A) Neither primitive nor object
(B) Primitive type stored purely on stack
(C) Converted automatically to ArrayList<Integer>
(D) Non-primitive object (inherits from java.lang.Object)
Correct Answer: (D)
[EN] Every array in Java—including primitive arrays like int[]—is a heap-allocated object whose class implicitly extends java.lang.Object.
[HI] जावा में प्रत्येक एरे—यहाँ तक कि int[] जैसा प्रिमिटिव एरे भी—हीप पर बना एक नॉन-प्रिमिटिव ऑब्जेक्ट होता है जो java.lang.Object से इनहेरिट करता है।
Q6. What modifiers are implicitly applied to every variable declared inside a Java interface?
(जावा इंटरफ़ेस के भीतर घोषित प्रत्येक वेरिएबल पर कौन से मॉडिफ़ायर स्वतः लागू होते हैं?)
(A) public volatile
(B) private static final
(C) protected final
(D) public static final
Correct Answer: (D)
[EN] Any field declared inside an interface is automatically a compile-time or interface constant with modifiers public, static, and final.
[HI] इंटरफ़ेस के भीतर घोषित कोई भी फ़ील्ड स्वतः public, static और final मॉडिफ़ायर वाला स्थिरांक बन जाता है।
Q7. Why is the java.lang.String class declared immutable (and final) in Java?
(जावा में java.lang.String क्लास को इम्यूटेबल (और final) क्यों बनाया गया है?)
(A) To enable safe String Pool sharing, thread safety, hashCode caching, and security in classloading/networking
(B) To allow String literals to behave like primitive int values
(C) So String objects can be stored without using the Heap
(D) Because Java cannot modify arrays inside any class
Correct Answer: (A)
[EN] Immutability lets multiple variables safely share pooled literals, prevents tampering with URLs/class names, caches hashCode(), and ensures thread safety without synchronization.
[HI] इम्यूटेबिलिटी से स्ट्रिंग पूल साझाकरण सुरक्षित होता है, थ्रेड-सेफ्टी मिलती है, hashCode कैशिंग संभव होती है और क्लासलोडिंग/नेटवर्किंग में सुरक्षा बनी रहती है।
Q8. What happens when you execute: String s = "Hi"; s.concat("!"); System.out.println(s);
(String s = "Hi"; s.concat("!"); System.out.println(s); निष्पादित करने पर क्या प्रिंट होगा?)
(A) Hi!
(B) Compile-time error
(C) Hi
(D) null
Correct Answer: (C)
[EN] Because String is immutable, s.concat("!") returns a new "Hi!" String object that is discarded unless assigned back to s, leaving s unchanged as "Hi".
[HI] चूंकि String इम्यूटेबल है, इसलिए s.concat("!") एक नया "Hi!" ऑब्जेक्ट लौटाता है जिसे s में वापस असाइन न करने पर वह नष्ट हो जाता है और s का मान "Hi" ही रहता है।
Q9. Which exception is thrown if you access arr[5] on an array declared as 'int[] arr = new int[5];'?
('int[] arr = new int[5];' के रूप में घोषित एरे पर arr[5] एक्सेस करने पर कौन सा अपवाद (exception) आता है?)
(A) MemoryAccessException
(B) NullPointerException
(C) ArrayIndexOutOfBoundsException
(D) IndexOutOfRangeError
Correct Answer: (C)
[EN] Valid indices for a 5-element array run from 0 to 4; index 5 triggers an unchecked java.lang.ArrayIndexOutOfBoundsException at runtime.
[HI] 5 तत्वों वाले एरे के वैध इंडेक्स 0 से 4 तक होते हैं; इंडेक्स 5 एक्सेस करने पर रनटाइम पर ArrayIndexOutOfBoundsException आता है।
Q10. Which two interfaces are automatically implemented by every Java array object?
(प्रत्येक जावा एरे ऑब्जेक्ट द्वारा कौन से दो इंटरफ़ेस स्वतः लागू किए जाते हैं?)
(A) Collection and List
(B) Cloneable and java.io.Serializable
(C) Comparable and Iterable
(D) AutoCloseable and Runnable
Correct Answer: (B)
[EN] Per the Java Language Specification, every array type implements both java.lang.Cloneable and java.io.Serializable.
[HI] Java Language Specification के अनुसार, प्रत्येक एरे प्रकार java.lang.Cloneable और java.io.Serializable दोनों इंटरफ़ेस लागू करता है।
Q11. In which JVM memory area has the String Constant Pool been located since Java 7?
(Java 7 से String Constant Pool किस JVM मेमोरी क्षेत्र में स्थित है?)
(A) JVM Stack
(B) Heap Area
(C) PC Register
(D) PermGen
Correct Answer: (B)
[EN] In Java 6, the String Pool lived in PermGen (causing OutOfMemoryError easily); Java 7 relocated it into the main garbage-collected Heap.
[HI] Java 6 में स्ट्रिंग पूल PermGen में था, जिसे Java 7 में गार्बेज-कलेक्टेड मुख्य Heap मेमोरी में स्थानांतरित कर दिया गया।
Q12. Which class should be used instead of String when performing heavy loop-based string concatenation in a single thread?
(एकल थ्रेड में लूप के भीतर भारी स्ट्रिंग संयोजन (concatenation) करते समय String के बजाय किस क्लास का उपयोग करना चाहिए?)
(A) StringJoiner
(B) StringPool
(C) StringBuilder
(D) StringTokenizer
Correct Answer: (C)
[EN] StringBuilder is mutable and non-synchronized, performing O(1) amortized appends without creating discarded intermediate String objects.
[HI] StringBuilder म्यूटेबल और नॉन-सिंक्रोनाइज्ड है, जो बीच के अनावश्यक String ऑब्जेक्ट बनाए बिना तेज़ी से टेक्स्ट जोड़ता है।
Q13. What happens if you try to instantiate an interface directly using 'new Runnable()'? (without an anonymous class body)
(यदि आप बिना किसी एनॉनिमस क्लास बॉडी के सीधे 'new Runnable()' लिखकर इंटरफ़ेस का ऑब्जेक्ट बनाने का प्रयास करें तो क्या होगा?)
(A) Creates an empty object at runtime
(B) Returns null
(C) Compile-time error: Runnable is abstract and cannot be instantiated
(D) Throws InstantiationException at runtime
Correct Answer: (C)
[EN] Interfaces cannot be directly instantiated; you must either instantiate a concrete implementing class, an anonymous class 'new Runnable() { ... }', or use a lambda expression.
[HI] इंटरफ़ेस का सीधे ऑब्जेक्ट नहीं बनाया जा सकता; इसके लिए इम्प्लीमेंट करने वाली कंक्रीट क्लास, एनॉनिमस क्लास या लैम्ब्डा एक्सप्रेशन की आवश्यकता होती है।
Q14. If 'int[] a = {1, 2}; int[] b = a; b[0] = 99;', what is the value of a[0]?
(यदि 'int[] a = {1, 2}; int[] b = a; b[0] = 99;' हो, तो a[0] का मान क्या होगा?)
(A) Compile-time error
(B) 99
(C) 1
(D) 0
Correct Answer: (B)
[EN] Assigning b = a copies the heap reference rather than the array contents, so mutating b[0] mutates the shared array visible through a[0].
[HI] b = a लिखने से एरे की कॉपी नहीं बल्कि हीप रेफरेंस कॉपी होता है, इसलिए b[0] बदलने पर साझा एरे का a[0] भी 99 हो जाता है।
Q15. Since Java 8 and Java 9, which non-abstract method types are allowed inside an interface alongside abstract methods?
(Java 8 और Java 9 से इंटरफ़ेस के भीतर abstract मेथड्स के साथ और कौन से नॉन-abstract मेथड अनुमत हैं?)
(A) final and synchronized methods
(B) abstract and native methods only
(C) default, static, and private methods
(D) protected and constructor methods
Correct Answer: (C)
[EN] Java 8 introduced default and static interface methods for backward-compatible API evolution, and Java 9 added private helper methods inside interfaces.
[HI] Java 8 ने इंटरफ़ेस में default और static मेथड जोड़े, जबकि Java 9 ने आंतरिक कोड साझाकरण के लिए private मेथड की अनुमति दी।
Q16. Which statement comparing Primitive and Non-Primitive data types in Java is NOT true?
(जावा में प्रिमिटिव और नॉन-प्रिमिटिव डेटा प्रकारों की तुलना करने वाला निम्नलिखित में से कौन सा कथन सत्य नहीं है?)
(A) Every non-primitive type in Java has a fixed identical memory footprint across all classes
(B) Primitives cannot be null, whereas non-primitive variables can hold null
(C) Non-primitive types always begin with uppercase names by Java naming convention (except array syntax)
(D) Primitives cannot invoke methods directly, whereas non-primitives can
Correct Answer: (A)
[EN] Non-primitive object sizes vary wildly depending on the number and types of fields inside each class or the length of an array.
[HI] नॉन-प्रिमिटिव ऑब्जेक्ट्स का मेमोरी आकार स्थिर नहीं होता; यह प्रत्येक क्लास के फ़ील्ड्स या एरे की लंबाई के अनुसार अलग-अलग होता है।
Q17. Within Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings, which non-primitive type directly maps to: "User-defined blueprint instantiated on Heap via new"?
(Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings में कौन सा Non-Primitive Type सीधे "User-defined blueprint instantiated on Heap via new" से संबंधित है?)
(A) Class
(B) String
(C) Interface
(D) Array
Correct Answer: (A)
[EN] In Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings, 'Class' handles 'User-defined blueprint instantiated on Heap via new' (Implicitly inherits java.lang.Object; supports single inheritance).
[HI] Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings में 'Class' का कार्य/विवरण 'User-defined blueprint instantiated on Heap via new' (Implicitly inherits java.lang.Object; supports single inheritance) है।
Q18. Which option accurately describes 'Interface' in Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings?
(Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings के संदर्भ में 'Interface' की सही विशेषता कौन सी है?)
(A) Contract with abstract/default/static methods and constants
(B) Fixed-size homogeneous container inheriting Object
(C) User-defined blueprint instantiated on Heap via new
(D) Final immutable class backed by String Constant Pool
Correct Answer: (A)
[EN] 'Interface' provides 'Contract with abstract/default/static methods and constants' — key point: Cannot be instantiated directly; enables multiple type inheritance.
[HI] 'Interface' का मुख्य विवरण 'Contract with abstract/default/static methods and constants' है (Cannot be instantiated directly; enables multiple type inheritance)।
Q19. In Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings, identify the correct parameter for the specification: "Fixed-size homogeneous container inheriting Object".
(Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings में "Fixed-size homogeneous container inheriting Object" विवरण किस विकल्प के लिए सही है?)
(A) Interface
(B) String
(C) Array
(D) Class
Correct Answer: (C)
[EN] 'Array' corresponds to 'Fixed-size homogeneous container inheriting Object' (Uses arr.length field (not length() method); zero-indexed).
[HI] 'Array' का संबंध 'Fixed-size homogeneous container inheriting Object' (Uses arr.length field (not length() method); zero-indexed) से है।
Q20. Considering Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings, what key behavior defines 'String'?
(Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings में 'String' की मुख्य भूमिका या मान क्या है?)
(A) Contract with abstract/default/static methods and constants
(B) User-defined blueprint instantiated on Heap via new
(C) Fixed-size homogeneous container inheriting Object
(D) Final immutable class backed by String Constant Pool
Correct Answer: (D)
[EN] 'String' represents 'Final immutable class backed by String Constant Pool' (Compare content with .equals(), never ==; pool moved to Heap in Java 7).
[HI] 'String' का अर्थ/मान 'Final immutable class backed by String Constant Pool' है (Compare content with .equals(), never ==; pool moved to Heap in Java 7)।
Non-Primitive Data Types: Classes, Interfaces, Arrays, Strings — Exam FAQs
Q1: Are Java Strings stored on the Stack or the Heap?
क्या जावा Strings स्टैक पर संग्रहीत होती हैं या हीप पर?
[EN] All String objects—whether created as literals in the String Constant Pool or via 'new String()'—are stored in the JVM Heap (since Java 7). Only the local reference variable pointing to the String resides on the thread's Stack.
[HI] सभी String ऑब्जेक्ट्स—चाहे वे String Constant Pool के लिटरल हों या 'new String()' द्वारा बने हों—Java 7 से JVM Heap में ही संग्रहीत होते हैं; स्टैक पर केवल उनका रेफरेंस वेरिएबल रहता है।
Q2: What is the difference between String, StringBuilder, and StringBuffer?
String, StringBuilder और StringBuffer में क्या अंतर है?
[EN] String is immutable (every edit allocates a new object). StringBuilder and StringBuffer are mutable; StringBuffer is synchronized (thread-safe but slightly slower), whereas StringBuilder is non-synchronized (faster for single-threaded usage).
[HI] String इम्यूटेबल है। StringBuilder और StringBuffer दोनों म्यूटेबल हैं; StringBuffer के मेथड synchronized (थ्रेड-सेफ) होते हैं जबकि StringBuilder नॉन-सिंक्रोनाइज्ड होने के कारण एकल-थ्रेड में अधिक तेज़ चलता है।
Q3: Why can't a Java interface have constructors?
जावा इंटरफ़ेस में कंस्ट्रक्टर क्यों नहीं हो सकते?
[EN] A constructor initializes instance fields of an instantiable class. Because interfaces cannot have mutable instance state (all fields are static final constants) and cannot be instantiated directly, constructors have no purpose in interfaces.
[HI] कंस्ट्रक्टर ऑब्जेक्ट के इंस्टेंस फ़ील्ड्स को इनिशियलाइज़ करता है। चूंकि इंटरफ़ेस में कोई इंस्टेंस फ़ील्ड नहीं होता और उसका सीधा ऑब्जेक्ट नहीं बनाया जा सकता, इसलिए इंटरफ़ेस में कंस्ट्रक्टर की अनुमति नहीं होती।
Post a Comment
Post a Comment