Python Frozenset: Immutable Sets and Dictionary Keys Notes in Hindi & English : फ्रोज़नसेट (Frozenset): अपरिवर्तनीय सेट्स और हैशेबल प्रकृति और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Frozenset: Immutable Sets and Dictionary Keys (फ्रोज़नसेट (Frozenset): अपरिवर्तनीय सेट्स और हैशेबल प्रकृति) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: A frozenset in Python is an immutable, unordered collection of unique hashable elements created using the frozenset() constructor. Because its contents cannot change after creation, a frozenset has a constant hash value and can serve as a dictionary key or an element inside another set.

अध्याय सार: पायथन में frozenset अद्वितीय (unique) और हैशेबल तत्वों का एक अपरिवर्तनीय (immutable) एवं अनियमित संग्रह है जिसे frozenset() कन्स्ट्रक्टर द्वारा बनाया जाता है। निर्माण के बाद इसके तत्व बदले नहीं जा सकते, इसलिए इसका हैश मान स्थिर रहता है और इसे डिक्शनरी की (key) या किसी अन्य सेट के तत्व के रूप में उपयोग किया जा सकता है।

Python Frozenset: Immutable Sets and Dictionary Keys: Detailed Notes & Architecture (फ्रोज़नसेट (Frozenset): अपरिवर्तनीय सेट्स और हैशेबल प्रकृति)

1. Creating Frozenset and Strict Immutability (Frozenset का निर्माण और पूर्ण अपरिवर्तनीयता)

[EN] Python provides no literal syntax for frozenset; empty curly braces {} create an empty dict and set() creates a mutable set, so you must call frozenset() or frozenset(iterable). Duplicates in the input iterable are automatically discarded while preserving only unique hashable items. Once instantiated, calling mutating methods such as add(), remove(), discard(), pop(), or clear() raises AttributeError because those methods do not exist on frozenset objects. Attempting to include an unhashable element like a list or dict inside frozenset() immediately raises TypeError.

[HI] पायथन में frozenset के लिए कोई लिटरल सिंटैक्स नहीं होता; खाली {} एक डिक्शनरी बनाता है और set() एक म्यूटेबल सेट बनाता है, इसलिए इसे हमेशा frozenset() या frozenset(iterable) से बनाया जाता है। इनपुट इटरेबल से डुप्लीकेट मान स्वतः हट जाते हैं और केवल अद्वितीय हैशेबल तत्व सुरक्षित रहते हैं। निर्माण के बाद add(), remove(), discard(), pop() या clear() जैसे म्यूटेशन मेथड कॉल करने पर AttributeError उत्पन्न होता है क्योंकि वे frozenset में मौजूद ही नहीं होते। यदि इसमें लिस्ट या डिक्शनरी जैसा अनहैशेबल तत्व डाला जाए, तो तुरंत TypeError आता है।

2. Hashability, Dictionary Keys, and Nested Sets (हैशेबिलिटी, डिक्शनरी कीज़ और नेस्टेड सेट्स)

[EN] A standard Python set is mutable and sets __hash__ to None, meaning using a set as a dictionary key or inside another set raises TypeError: unhashable type: 'set'. By contrast, frozenset implements __hash__() by combining the hashes of its members in an order-independent manner, guaranteeing that frozenset({1, 2}) == frozenset({2, 1}) and both produce the exact same hash. This allows programmers to represent sets of sets, composite cache keys, and undirected graph edges cleanly as dictionary keys.

[HI] मानक पायथन set म्यूटेबल होता है और इसका __hash__ मान None होता है, इसलिए इसे डिक्शनरी की या किसी अन्य सेट के भीतर रखने पर TypeError: unhashable type: 'set' आता है। इसके विपरीत, frozenset अपने तत्वों के हैश को क्रम-निरपेक्ष (order-independent) तरीके से मिलाकर __hash__() लागू करता है, जिससे frozenset({1, 2}) == frozenset({2, 1}) सत्य होता है और दोनों समान हैश देते हैं। इसी कारण सेट के अंदर सेट, कम्पोज़िट कैश कीज़ और अनडायरेक्टेड ग्राफ़ एज को डिक्शनरी की के रूप में आसानी से उपयोग किया जा सकता है।

3. Non-Mutating Set Operations and Type Rules (नॉन-म्यूटेटिंग सेट ऑपरेशंस और टाइप नियम)

[EN] All read-only relational and mathematical set operations work on frozenset, including union (|), intersection (&), difference (-), symmetric difference (^), issubset(), issuperset(), and isdisjoint(). When combining a set and a frozenset with a binary operator, the returned object inherits the type of the left-hand operand: set_a | frozen_b returns a set, whereas frozen_b | set_a returns a frozenset. Calling fs.copy() typically returns the exact same object reference (fs.copy() is fs evaluates to True in CPython) because immutable objects require no separate copy.

[HI] सभी रीड-ओनली और गणितीय सेट ऑपरेशंस जैसे union (|), intersection (&), difference (-), symmetric difference (^), issubset(), issuperset() और isdisjoint() frozenset पर पूरी तरह काम करते हैं। जब किसी set और frozenset को बाइनरी ऑपरेटर से जोड़ा जाता है, तो परिणाम का डेटा टाइप बाएँ (left-hand) ऑपरेंड के अनुसार होता है: set_a | frozen_b से set और frozen_b | set_a से frozenset मिलता है। fs.copy() कॉल करने पर सामान्यतः वही ऑब्जेक्ट रेफरेंस लौटता है (fs.copy() is fs सत्य होता है) क्योंकि इम्यूटेबल ऑब्जेक्ट की अलग कॉपी बनाने की आवश्यकता नहीं होती।

4. Performance, Rebinding Operators, and Practical Scenarios (परफॉरमेंस, री-बाइंडिंग ऑपरेटर्स और व्यावहारिक उपयोग)

[EN] Like set, frozenset uses an open-addressing hash table under the hood, providing average O(1) membership testing via the in operator and O(min(len(s1), len(s2))) complexity for intersection checks. Note that an augmented assignment like fs |= {4} does not mutate the frozenset in place; instead, it creates a brand new frozenset and rebinds the variable name fs while leaving any aliases unchanged. Common exam and production scenarios include storing role-permission sets as dictionary keys, memoizing functions with order-independent parameter sets, and defining fixed read-only configuration flags.

[HI] set की तरह frozenset भी आंतरिक रूप से ओपन-एड्रेसिंग हैश टेबल का उपयोग करता है, जिससे in ऑपरेटर द्वारा औसत O(1) समय में सदस्यता जाँच संभव होती है। ध्यान दें कि fs |= {4} जैसा ऑगमेंटेड असाइनमेंट frozenset को इन-प्लेस नहीं बदलता, बल्कि एक नया frozenset बनाकर वेरिएबल fs को उससे री-बाइंड करता है और पुराने एलियास अपरिवर्तित रहते हैं। मुख्य परीक्षा व व्यावहारिक उपयोगों में रोल-परमिशन सेट को डिक्शनरी की बनाना, क्रम-निरपेक्ष पैरामीटर कैशिंग और स्थिर रीड-ओनली कॉन्फ़िगरेशन फ्लैग्स संग्रहीत करना शामिल है।

Syntax, Formula & Technical Example (Python Frozenset: Immutable Sets and Dictionary Keys)

edge_ab = frozenset({'A', 'B'})
edge_ba = frozenset(['B', 'A'])
weights = {edge_ab: 12}
print(edge_ab == edge_ba, weights[edge_ba])  # Output: True 12
s = {1, 2}
fs = frozenset({2, 3})
print(type(s & fs).__name__, type(fs & s).__name__)  # Output: set frozenset
print(fs.copy() is fs)  # Output: True

Python Frozenset: Immutable Sets and Dictionary Keys Quick Reference & Comparison Table

Operation / FeatureBehavior in frozensetKey Exam Note
frozenset()Creates empty immutable setNo literal syntax exists like {}
hash(frozenset({1, 2}))Returns consistent integer hashUsable as dict key or set element
fs.add(5)Raises AttributeErrorMutating methods are completely absent
fs_a | set_bReturns new frozenset objectResult type follows left-hand operand

Python Frozenset: Immutable Sets and Dictionary Keys Top 20 Bilingual MCQs with Answers

Q1. How do you create an empty frozenset in Python?

(पायथन में खाली frozenset कैसे बनाया जाता है?)

(A) set()

(B) frozenset({}) if only mutable

(C) frozenset()

(D) {}

Correct Answer: (C)

[EN] Calling frozenset() with no arguments creates an empty frozenset; {} creates an empty dict and set() creates a mutable set.

[HI] बिना आर्ग्युमेंट के frozenset() कॉल करने पर खाली frozenset बनता है; {} से डिक्शनरी और set() से म्यूटेबल सेट बनता है।

Q2. Which exception is raised when calling fs.add(10) on a frozenset object?

(frozenset ऑब्जेक्ट पर fs.add(10) कॉल करने से कौन-सा अपवाद आता है?)

(A) AttributeError

(B) KeyError

(C) TypeError

(D) ValueError

Correct Answer: (A)

[EN] frozenset has no mutating methods like add(), remove(), or pop(), so accessing add raises AttributeError.

[HI] frozenset में add(), remove() या pop() जैसे म्यूटेशन मेथड नहीं होते, इसलिए AttributeError उत्पन्न होता है।

Q3. Which of the following is valid as a Python dictionary key?

(निम्नलिखित में से कौन पायथन डिक्शनरी की (key) के रूप में मान्य है?)

(A) frozenset({1, 2, 3})

(B) {1, 2, 3}

(C) [1, 2, 3]

(D) {1: 'a'}

Correct Answer: (A)

[EN] Only hashable objects can be dictionary keys; frozenset is immutable and hashable, whereas set, list, and dict are unhashable.

[HI] केवल हैशेबल ऑब्जेक्ट ही डिक्शनरी की बन सकते हैं; frozenset इम्यूटेबल व हैशेबल है जबकि set, list और dict अनहैशेबल हैं।

Q4. What happens when you execute frozenset([1, [2, 3]])?

(frozenset([1, [2, 3]]) निष्पादित करने पर क्या होता है?)

(A) Raises TypeError: unhashable type: 'list'

(B) Raises ValueError

(C) Converts inner list to tuple automatically

(D) Creates frozenset({1, [2, 3]})

Correct Answer: (A)

[EN] Every element stored inside a set or frozenset must itself be hashable; a nested list triggers TypeError.

[HI] set या frozenset के भीतर रखा जाने वाला प्रत्येक तत्व स्वयं हैशेबल होना चाहिए; नेस्टेड लिस्ट TypeError उत्पन्न करती है।

Q5. What is the output of: frozenset({1, 2}) == frozenset({2, 1})?

(frozenset({1, 2}) == frozenset({2, 1}) का आउटपुट क्या होगा?)

(A) True

(B) TypeError

(C) False

(D) None

Correct Answer: (A)

[EN] Sets and frozensets are unordered collections, so equality checks element membership regardless of insertion order.

[HI] set और frozenset अनियमित संग्रह हैं, इसलिए समानता की जाँच क्रम के बजाय केवल तत्वों की उपस्थिति पर आधारित होती है।

Q6. If s = {1, 2} and fs = frozenset({2, 3}), what is type(fs | s)?

(यदि s = {1, 2} और fs = frozenset({2, 3}) है, तो type(fs | s) क्या होगा?)

(A) <class 'tuple'>

(B) <class 'set'>

(C) <class 'frozenset'>

(D) Raises TypeError

Correct Answer: (C)

[EN] When combining set and frozenset using binary set operators, the result takes the type of the left-hand operand (fs).

[HI] set और frozenset पर बाइनरी सेट ऑपरेटर लगाने पर परिणाम बाएँ ऑपरेंड (fs) के टाइप का प्राप्त होता है।

Q7. If s = {1, 2} and fs = frozenset({2, 3}), what is type(s & fs)?

(यदि s = {1, 2} और fs = frozenset({2, 3}) है, तो type(s & fs) क्या होगा?)

(A) <class 'frozenset'>

(B) Raises TypeError

(C) <class 'dict'>

(D) <class 'set'>

Correct Answer: (D)

[EN] Because the left operand s is a mutable set, s & fs returns a mutable set object.

[HI] चूँकि बायाँ ऑपरेंड s एक म्यूटेबल set है, इसलिए s & fs एक म्यूटेबल set ऑब्जेक्ट लौटाता है।

Q8. What does len(frozenset('mississippi')) return?

(len(frozenset('mississippi')) क्या लौटाता है?)

(A) 5

(B) 11

(C) 4

(D) 1

Correct Answer: (C)

[EN] frozenset('mississippi') keeps only unique characters {'m', 'i', 's', 'p'}, whose length is 4.

[HI] frozenset('mississippi') केवल अद्वितीय अक्षर {'m', 'i', 's', 'p'} रखता है, जिनकी संख्या 4 है।

Q9. In CPython, what does fs.copy() is fs evaluate to for a non-empty frozenset fs?

(CPython में गैर-खाली frozenset fs के लिए fs.copy() is fs का मान क्या होता है?)

(A) AttributeError

(B) None

(C) True

(D) False

Correct Answer: (C)

[EN] Because frozenset is immutable, copy() optimizes by returning a reference to the same object rather than allocating a duplicate.

[HI] frozenset अपरिवर्तनीय है, इसलिए copy() नई कॉपी बनाने के बजाय उसी ऑब्जेक्ट का रेफरेंस लौटाता है।

Q10. What is the average time complexity of checking x in fs for a frozenset of size n?

(n आकार के frozenset में x in fs जाँचने की औसत समय जटिलता क्या है?)

(A) O(n)

(B) O(n log n)

(C) O(1)

(D) O(log n)

Correct Answer: (C)

[EN] frozenset stores elements in an open-addressing hash table, giving O(1) average lookup time.

[HI] frozenset तत्वों को ओपन-एड्रेसिंग हैश टेबल में रखता है, जिससे औसत खोज समय O(1) होता है।

Q11. What happens when you execute: a = frozenset({1}); b = a; a |= {2}?

(a = frozenset({1}); b = a; a |= {2} चलाने पर क्या होता है?)

(A) Raises AttributeError

(B) a becomes frozenset({1, 2}) and b remains frozenset({1})

(C) Both a and b become frozenset({1, 2})

(D) Raises TypeError on |=

Correct Answer: (B)

[EN] |= on a frozenset creates a new frozenset object and rebinds a, leaving alias b pointing to the original frozenset({1}).

[HI] frozenset पर |= नया frozenset बनाकर वेरिएबल a को री-बाइंड करता है, जिससे एलियास b पुराने frozenset({1}) पर ही रहता है।

Q12. Which data structure is best to represent undirected graph edges so {u, v} and {v, u} map to the same dictionary key?

(अनडायरेक्टेड ग्राफ़ एज को दर्शाने के लिए कौन-सा डेटा स्ट्रक्चर उपयुक्त है ताकि {u, v} और {v, u} एक ही डिक्शनरी की बनें?)

(A) [u, v]

(B) {u, v}

(C) frozenset({u, v})

(D) (u, v)

Correct Answer: (C)

[EN] Tuples are order-sensitive so (u, v) != (v, u), lists and sets are unhashable, whereas frozenset({u, v}) == frozenset({v, u}) and is hashable.

[HI] टपल क्रम-संवेदनशील है, लिस्ट और सेट अनहैशेबल हैं, जबकि frozenset({u, v}) क्रम-निरपेक्ष व हैशेबल दोनों है।

Q13. Can a frozenset be stored as an element inside a standard mutable set?

(क्या एक मानक म्यूटेबल set के भीतर frozenset को तत्व के रूप में रखा जा सकता है?)

(A) No, sets cannot hold any set types

(B) Yes, because frozenset is hashable

(C) Only in Python 2

(D) Only if the frozenset is empty

Correct Answer: (B)

[EN] Since frozenset is immutable and implements __hash__(), it can be placed inside a mutable set without TypeError.

[HI] चूँकि frozenset अपरिवर्तनीय और हैशेबल है, इसलिए इसे बिना किसी त्रुटि के म्यूटेबल set में रखा जा सकता है।

Q14. What does frozenset({1, 2}).isdisjoint(frozenset({3, 4})) return?

(frozenset({1, 2}).isdisjoint(frozenset({3, 4})) क्या लौटाता है?)

(A) True

(B) frozenset()

(C) False

(D) None

Correct Answer: (A)

[EN] isdisjoint() returns True when two sets share zero common elements.

[HI] दो सेटों में कोई भी उभयनिष्ठ (common) तत्व न होने पर isdisjoint() True लौटाता है।

Q15. Which of the following operations raises an error on fs = frozenset({1, 2, 3})?

(fs = frozenset({1, 2, 3}) पर निम्नलिखित में से कौन-सा ऑपरेशन त्रुटि देगा?)

(A) 2 in fs

(B) fs.discard(2)

(C) fs.issubset({1, 2, 3, 4})

(D) fs - {2}

Correct Answer: (B)

[EN] discard() is a mutating method absent from frozenset and raises AttributeError; membership, issubset, and - return new values cleanly.

[HI] discard() एक म्यूटेटिंग मेथड है जो frozenset में नहीं होता और AttributeError देता है; बाकी तीनों वैध हैं।

Q16. Which statement is NOT true about Python's frozenset?

(पायथन के frozenset के बारे में कौन-सा कथन सत्य नहीं है?)

(A) frozenset can act as a dictionary key

(B) frozenset removes duplicate items

(C) frozenset has no literal syntax

(D) frozenset supports indexing like fs[0]

Correct Answer: (D)

[EN] Like set, frozenset is unordered and does not support integer indexing or slicing; fs[0] raises TypeError.

[HI] set की तरह frozenset भी अनियमित होता है और इसमें इंडेक्सिंग (fs[0]) या स्लाइसिंग नहीं होती; fs[0] से TypeError आता है।

Q17. Within Python Frozenset: Immutable Sets and Dictionary Keys, which operation / feature directly maps to: "Creates empty immutable set"?

(Python Frozenset: Immutable Sets and Dictionary Keys में कौन सा Operation / Feature सीधे "Creates empty immutable set" से संबंधित है?)

(A) fs.add(5)

(B) hash(frozenset({1, 2}))

(C) frozenset()

(D) fs_a | set_b

Correct Answer: (C)

[EN] In Python Frozenset: Immutable Sets and Dictionary Keys, 'frozenset()' handles 'Creates empty immutable set' (No literal syntax exists like {}).

[HI] Python Frozenset: Immutable Sets and Dictionary Keys में 'frozenset()' का कार्य/विवरण 'Creates empty immutable set' (No literal syntax exists like {}) है।

Q18. Which option accurately describes 'hash(frozenset({1, 2}))' in Python Frozenset: Immutable Sets and Dictionary Keys?

(Python Frozenset: Immutable Sets and Dictionary Keys के संदर्भ में 'hash(frozenset({1, 2}))' की सही विशेषता कौन सी है?)

(A) Raises AttributeError

(B) Returns new frozenset object

(C) Creates empty immutable set

(D) Returns consistent integer hash

Correct Answer: (D)

[EN] 'hash(frozenset({1, 2}))' provides 'Returns consistent integer hash' — key point: Usable as dict key or set element.

[HI] 'hash(frozenset({1, 2}))' का मुख्य विवरण 'Returns consistent integer hash' है (Usable as dict key or set element)।

Q19. In Python Frozenset: Immutable Sets and Dictionary Keys, identify the correct parameter for the specification: "Raises AttributeError".

(Python Frozenset: Immutable Sets and Dictionary Keys में "Raises AttributeError" विवरण किस विकल्प के लिए सही है?)

(A) frozenset()

(B) fs.add(5)

(C) hash(frozenset({1, 2}))

(D) fs_a | set_b

Correct Answer: (B)

[EN] 'fs.add(5)' corresponds to 'Raises AttributeError' (Mutating methods are completely absent).

[HI] 'fs.add(5)' का संबंध 'Raises AttributeError' (Mutating methods are completely absent) से है।

Q20. Considering Python Frozenset: Immutable Sets and Dictionary Keys, what key behavior defines 'fs_a | set_b'?

(Python Frozenset: Immutable Sets and Dictionary Keys में 'fs_a | set_b' की मुख्य भूमिका या मान क्या है?)

(A) Creates empty immutable set

(B) Returns new frozenset object

(C) Returns consistent integer hash

(D) Raises AttributeError

Correct Answer: (B)

[EN] 'fs_a | set_b' represents 'Returns new frozenset object' (Result type follows left-hand operand).

[HI] 'fs_a | set_b' का अर्थ/मान 'Returns new frozenset object' है (Result type follows left-hand operand)।

Python Frozenset: Immutable Sets and Dictionary Keys — Exam FAQs

Q1: Why does Python not have a literal syntax for frozenset?

पायथन में frozenset के लिए कोई लिटरल सिंटैक्स क्यों नहीं है?

[EN] Curly braces {} are already claimed by dict and non-empty set literals, and frozenset is used far less frequently than dict or set, so Python keeps the explicit frozenset() constructor for clarity.

[HI] {} का उपयोग पहले से डिक्शनरी और सेट लिटरल्स के लिए आरक्षित है, और frozenset का उपयोग अपेक्षाकृत कम होने के कारण पायथन ने स्पष्टता हेतु केवल frozenset() कन्स्ट्रक्टर रखा है।

Q2: What happens if you pass a dictionary to frozenset(d)?

यदि frozenset(d) में कोई डिक्शनरी पास की जाए तो क्या होता है?

[EN] Iterating over a dictionary yields only its keys, so frozenset({'a': 1, 'b': 2}) creates frozenset({'a', 'b'}) containing just the dictionary keys.

[HI] डिक्शनरी पर इटरेट करने से केवल उसकी कीज़ मिलती हैं, इसलिए frozenset({'a': 1, 'b': 2}) केवल कीज़ वाला frozenset({'a', 'b'}) बनाता है।

Q3: What is the return type of set({1, 2}) & frozenset({2, 3})?

set({1, 2}) & frozenset({2, 3}) का रिटर्न टाइप क्या होता है?

[EN] It returns a mutable set ({2}) because Python binary set operators always adopt the type of the left-hand operand.

[HI] यह एक म्यूटेबल set ({2}) लौटाता है क्योंकि पायथन के बाइनरी सेट ऑपरेटर हमेशा बाएँ ऑपरेंड का टाइप अपनाते हैं।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)