Object-Oriented Programming (OOP) in Python: Classes and Objects Notes in Hindi & English : पायथन OOP: क्लास निर्माण, ऑब्जेक्ट्स और एट्रिब्यूट्स और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

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

Overview: Python implements Object-Oriented Programming where every value—including integers, strings, functions, and classes themselves—is a first-class object created from a blueprint. A class bundles data attributes and methods into a single namespace, instantiating objects dynamically at runtime.

अध्याय सार: पायथन में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (OOP) के तहत प्रत्येक मान—जैसे इंटीजर, स्ट्रिंग, फंक्शन और स्वयं क्लास—एक ऑब्जेक्ट होता है। class कीवर्ड डेटा (एट्रिब्यूट्स) और व्यवहार (मेथड्स) को एक नेमस्पेस में बांधकर रनटाइम पर नए ऑब्जेक्ट (इंस्टेंस) बनाने का ब्लूप्रिंट प्रदान करता है।

Object-Oriented Programming (OOP) in Python: Classes and Objects: Detailed Notes & Architecture (पायथन OOP: क्लास निर्माण, ऑब्जेक्ट्स और एट्रिब्यूट्स)

1. Classes as Blueprints and First-Class Objects (क्लास ब्लूप्रिंट और फर्स्ट-क्लास ऑब्जेक्ट अवधारणा)

[EN] Defined with the class ClassName: statement, a Python class acts as a factory blueprint whose metaclass is type by default. Unlike static languages such as C++, Python class bodies execute at runtime to create a live class object bound in the enclosing namespace. Calling the class like a function (obj = Student()) allocates a distinct instance in memory, and even built-in primitives pass isinstance(5, object) as True because everything derives from object.

[HI] class ClassName: द्वारा परिभाषित क्लास एक ब्लूप्रिंट की तरह कार्य करती है जिसकी डिफ़ॉल्ट मेटाक्लास type होती है। C++ जैसी स्टैटिक भाषाओं के विपरीत, पायथन में क्लास बॉडी रनटाइम पर निष्पादित होकर एक जीवंत क्लास ऑब्जेक्ट बनाती है। क्लास को फंक्शन की तरह कॉल करने (obj = Student()) पर मेमोरी में नया इंस्टेंस बनता है और पायथन में सभी मान object से जुड़े होने के कारण isinstance(5, object) True होता है।

2. Attributes, __dict__, and Memory Optimization via __slots__ (एट्रिब्यूट्स, __dict__ और __slots__ से मेमोरी बचत)

[EN] Object attributes are read and mutated with dot notation or the built-in introspection helpers getattr(), setattr(), hasattr(), and delattr(). By default, each instance stores its attributes inside a per-object hash dictionary named obj.__dict__, allowing arbitrary attributes to be attached dynamically at runtime. Defining __slots__ = ('name', 'roll') inside the class replaces __dict__ with fixed C-level descriptors, cutting memory footprint significantly across millions of objects and blocking undeclared attributes.

[HI] ऑब्जेक्ट के एट्रिब्यूट्स को डॉट नोटेशन या getattr(), setattr(), hasattr() और delattr() बिल्ट-इन फंक्शंस से पढ़ा और बदला जा सकता है। डिफ़ॉल्ट रूप से प्रत्येक इंस्टेंस अपना डेटा obj.__dict__ डिक्शनरी में रखता है, जिससे रनटाइम पर नए एट्रिब्यूट जोड़े जा सकते हैं। क्लास में __slots__ = ('name', 'roll') घोषित करने पर __dict__ समाप्त हो जाता है, लाखों ऑब्जेक्ट्स में भारी मेमोरी बचत होती है और नए अनघोषित एट्रिब्यूट जुड़ना रुकता है।

3. Method Categories and Attribute Lookup Resolution (मेथड के प्रकार और एट्रिब्यूट खोज क्रम)

[EN] A Python class supports three distinct method forms: instance methods receiving self automatically, @classmethod functions receiving the class cls for factory patterns, and @staticmethod utilities receiving neither implicit argument. When accessing obj.attr, Python checks data descriptors on type(obj), then obj.__dict__, then class attributes and non-data descriptors along the Method Resolution Order. Calling dir(obj) returns a sorted list of valid attribute and method names available on that object.

[HI] पायथन क्लास में तीन मुख्य मेथड प्रकार होते हैं: इंस्टेंस मेथड (जिन्हें स्वतः self मिलता है), @classmethod (जिन्हें क्लास cls प्राप्त होती है) और @staticmethod (जिनमें कोई इम्प्लिसिट पैरामीटर नहीं होता)। obj.attr पढ़ते समय पायथन पहले डेटा डिस्क्रिप्टर्स, फिर obj.__dict__ और उसके बाद क्लास व पेरेंट क्लासेस को खोजता है। dir(obj) किसी ऑब्जेक्ट पर उपलब्ध सभी एट्रिब्यूट्स और मेथड्स की क्रमबद्ध सूची देता है।

4. Encapsulation Conventions and Name Mangling (एनकैप्सुलेशन नियम और नेम मैंगलिंग)

[EN] Python relies on naming conventions rather than strict compiler keywords like private or protected. A single leading underscore (_balance) signals an internal attribute intended for class and subclass use only, though external code can still access it directly. In contrast, a double leading underscore without trailing underscores (__pin) triggers automatic name mangling to _ClassName__pin so subclasses cannot accidentally overwrite parent attributes.

[HI] पायथन में एनकैप्सुलेशन के लिए private या protected जैसे कठोर कीवर्ड्स के बजाय नामकरण परंपरा का उपयोग होता है। शुरुआत में एक अंडरस्कोर (_balance) आंतरिक या प्रोटेक्टेड सदस्य का संकेत देता है, हालांकि बाहर से इसे पढ़ा जा सकता है। इसके विपरीत, दो शुरुआती अंडरस्कोर (__pin, बिना अंतिम अंडरस्कोर के) देने पर नेम मैंगलिंग होती है और पायथन इसे अंदरूनी तौर पर _ClassName__pin में बदल देता है ताकि सबक्लास से टकराव न हो।

Syntax, Formula & Technical Example (Object-Oriented Programming (OOP) in Python: Classes and Objects)

class Student:
    board = 'RSMSSB'
    def __init__(self, name, roll):
        self.name = name
        self.__roll = roll
    def info(self):
        return f'{self.name}:{self.board}'

s = Student('Aarav', 101)
print(s.info(), s._Student__roll)

Object-Oriented Programming (OOP) in Python: Classes and Objects Quick Reference & Comparison Table

OOP ConstructTechnical BehaviorExam Key Point
obj.__dict__Per-instance dictionary holding attributesRemoved when __slots__ is declared
_attr (single)Convention marking protected/internal memberRemains directly accessible outside class
__attr (double)Name-mangled internally to _ClassName__attrPrevents accidental subclass collisions
__slots__Tuple fixing allowed instance attributesReduces memory use; forbids dynamic attrs

Object-Oriented Programming (OOP) in Python: Classes and Objects Top 20 Bilingual MCQs with Answers

Q1. What is the result of evaluating type(Student) for a user-defined class Student in Python 3?

(पायथन 3 में यूजर-डिफाइंड क्लास Student के लिए type(Student) का परिणाम क्या होता है?)

(A) <class 'type'>

(B) <class 'Student'>

(C) <class 'class'>

(D) <class 'object'>

Correct Answer: (A)

[EN] In Python, every class object is itself an instance of the default metaclass 'type'.

[HI] पायथन में प्रत्येक क्लास ऑब्जेक्ट स्वयं डिफ़ॉल्ट मेटाक्लास 'type' का एक इंस्टेंस होती है।

Q2. How does Python internally mangle an attribute named __pin inside class Account?

(पायथन में class Account के भीतर __pin नाम के एट्रिब्यूट की नेम मैंगलिंग किस रूप में होती है?)

(A) _pin_Account

(B) Account.__private_pin

(C) _Account__pin

(D) __Account_pin

Correct Answer: (C)

[EN] Double-leading-underscore identifiers without trailing underscores are mangled to _ClassName__attr.

[HI] शुरुआत में दो अंडरस्कोर वाले नामों (अंत में अंडरस्कोर रहित) को _ClassName__attr में बदला जाता है।

Q3. Which special class attribute eliminates per-instance __dict__ creation to save RAM?

(कौन-सा विशेष क्लास एट्रिब्यूट मेमोरी बचाने के लिए प्रत्येक इंस्टेंस में बनने वाली __dict__ डिक्शनरी को समाप्त करता है?)

(A) __fixed__

(B) __slots__

(C) __compact__

(D) __struct__

Correct Answer: (B)

[EN] Defining __slots__ replaces the dynamic __dict__ dictionary with fixed descriptor slots per instance.

[HI] __slots__ परिभाषित करने से इंस्टेंस की डायनामिक __dict__ समाप्त हो जाती है और मेमोरी की बचत होती है।

Q4. Which built-in function returns True if an object has a named attribute and False otherwise?

(कौन-सा बिल्ट-इन फंक्शन यह जांचता है कि किसी ऑब्जेक्ट में दिया गया एट्रिब्यूट मौजूद है या नहीं (True/False)?)

(A) isattr()

(B) checkattr()

(C) hasattr()

(D) findattr()

Correct Answer: (C)

[EN] hasattr(obj, 'name') safely checks attribute existence without raising AttributeError.

[HI] hasattr(obj, 'name') बिना AttributeError दिए ऑब्जेक्ट में एट्रिब्यूट की उपस्थिति की जांच करता है।

Q5. Which keyword allows defining an empty placeholder class in Python without a syntax error?

(पायथन में बिना सिंटैक्स त्रुटि के एक खाली प्लेसहोल्डर क्लास बनाने के लिए किस कीवर्ड का उपयोग होता है?)

(A) pass

(B) empty

(C) void

(D) skip

Correct Answer: (A)

[EN] Because Python requires an indented suite after class Name:, the pass statement acts as a null placeholder.

[HI] class Name: के बाद बॉडी आवश्यक होने के कारण रिक्त क्लास बनाने हेतु pass स्टेटमेंट का उपयोग किया जाता है।

Q6. What does isinstance(42, object) evaluate to in Python 3?

(पायथन 3 में isinstance(42, object) का मान क्या प्राप्त होता है?)

(A) None

(B) True

(C) TypeError

(D) False

Correct Answer: (B)

[EN] Every Python type—including int, str, functions, and modules—inherits from object.

[HI] पायथन में int, str, फंक्शन और मॉड्यूल सहित प्रत्येक डेटा टाइप object से इनहेरिट होता है।

Q7. Which decorator creates a method on a class that receives neither self nor cls implicitly?

(कौन-सा डेकोरेटर क्लास में ऐसा मेथड बनाता है जिसे न तो self और न ही cls स्वतः पास होता है?)

(A) @classmethod

(B) @globalmethod

(C) @staticmethod

(D) @nomethod

Correct Answer: (C)

[EN] @staticmethod behaves like a plain function namespaced inside the class without implicit first arguments.

[HI] @staticmethod क्लास के नेमस्पेस में सामान्य फंक्शन की तरह कार्य करता है जिसे कोई इम्प्लिसिट पैरामीटर नहीं मिलता।

Q8. What happens if you attach an attribute dynamically via obj.city = 'Jaipur' on a normal Python object?

(एक सामान्य पायथन ऑब्जेक्ट पर obj.city = 'Jaipur' लिखकर डायनामिक रूप से नया एट्रिब्यूट जोड़ने पर क्या होगा?)

(A) It overwrites the class name with 'Jaipur'

(B) 'city': 'Jaipur' is added to obj.__dict__ for that instance only

(C) CompileError occurs because city was not declared in the class

(D) city is added automatically to every existing instance

Correct Answer: (B)

[EN] Normal Python instances store attributes in a mutable __dict__, so dynamic per-instance assignment works immediately.

[HI] सामान्य ऑब्जेक्ट अपने एट्रिब्यूट __dict__ में रखते हैं, इसलिए केवल उसी ऑब्जेक्ट में 'city' जुड़ जाता है।

Q9. Which built-in function retrieves an attribute value by string name and allows specifying a fallback default?

(कौन-सा बिल्ट-इन फंक्शन स्ट्रिंग नाम से एट्रिब्यूट का मान प्राप्त करता है और न मिलने पर डिफ़ॉल्ट मान भी दे सकता है?)

(A) readattr()

(B) fetchattr()

(C) loadattr()

(D) getattr()

Correct Answer: (D)

[EN] getattr(obj, 'attr', default) returns obj.attr if present, otherwise default instead of raising AttributeError.

[HI] getattr(obj, 'attr', default) एट्रिब्यूट न मिलने पर AttributeError देने के बजाय डिफ़ॉल्ट मान लौटा सकता है।

Q10. What is the output of the Student snippet above?

(ऊपर दिए गए Student कोड स्निपेट का आउटपुट क्या होगा?)

(A) Aarav:RSMSSB 101

(B) Student:RSMSSB 101

(C) Aarav:None 101

(D) AttributeError on __roll

Correct Answer: (A)

[EN] s.info() returns 'Aarav:RSMSSB' and s._Student__roll accesses the name-mangled __roll value 101.

[HI] s.info() से 'Aarav:RSMSSB' और नाम-मैंगल्ड सन्दर्भ s._Student__roll से 101 प्रिंट होता है।

Q11. What is the difference between @classmethod and instance methods regarding the first parameter?

(पहले पैरामीटर के संदर्भ में @classmethod और सामान्य इंस्टेंस मेथड में क्या अंतर है?)

(A) There is no difference; both receive self

(B) @classmethod receives nothing whereas instance method receives cls

(C) @classmethod receives the class (cls) whereas an instance method receives the object (self)

(D) @classmethod can only be called on instances, never on classes

Correct Answer: (C)

[EN] @classmethod binds the class object itself as cls, allowing factory constructors and class-state updates.

[HI] @classmethod को पहला पैरामीटर क्लास (cls) मिलता है जबकि इंस्टेंस मेथड को ऑब्जेक्ट (self) मिलता है।

Q12. Why is an attribute like __init__ NOT name-mangled by Python?

(पायथन द्वारा __init__ जैसे नामों की नेम मैंगलिंग क्यों नहीं की जाती?)

(A) Only integer attributes are mangled by the interpreter

(B) Because __init__ starts with an underscore followed by lowercase i

(C) Name mangling applies only inside @staticmethod

(D) Identifiers with both leading and trailing double underscores (dunders) are exempt from mangling

Correct Answer: (D)

[EN] Name mangling applies only to names with at least two leading underscores and at most one trailing underscore.

[HI] नेम मैंगलिंग केवल उन्हीं नामों पर लागू होती है जिनके आगे कम से कम दो और अंत में अधिक से अधिक एक अंडरस्कोर हो।

Q13. Which built-in function returns a list of attribute and method names available on an object?

(कौन-सा बिल्ट-इन फंक्शन किसी ऑब्जेक्ट पर उपलब्ध एट्रिब्यूट्स और मेथड्स के नामों की सूची देता है?)

(A) vars_list()

(B) dir()

(C) attrs()

(D) inspect_all()

Correct Answer: (B)

[EN] dir(obj) returns an alphabetically sorted list of valid attribute names found on obj and its class hierarchy.

[HI] dir(obj) किसी ऑब्जेक्ट और उसकी क्लास में उपलब्ध सभी वैध एट्रिब्यूट नामों की वर्णमाला क्रम सूची देता है।

Q14. What happens when you try to add an undeclared attribute to an object whose class defines __slots__ = ('x',)?

(जिस क्लास में __slots__ = ('x',) परिभाषित हो, उसके ऑब्जेक्ट पर नया अनघोषित एट्रिब्यूट जोड़ने पर क्या होगा?)

(A) The attribute is silently ignored

(B) AttributeError is raised

(C) SyntaxError is raised

(D) Python automatically creates __dict__ as fallback

Correct Answer: (B)

[EN] Without __dict__ (unless '__dict__' is listed in __slots__), assigning any attribute outside __slots__ raises AttributeError.

[HI] __dict__ के अभाव में __slots__ से बाहर कोई भी नया एट्रिब्यूट असाइन करने पर AttributeError उत्पन्न होता है।

Q15. Which statement about Python OOP is NOT true?

(पायथन OOP के विषय में निम्नलिखित में से कौन-सा कथन सत्य नहीं है?)

(A) Double-underscore attributes are strictly unbreakable private fields inaccessible from outside

(B) Classes are created dynamically at runtime when the class block executes

(C) Functions defined inside a class act as descriptors creating bound methods on instances

(D) delattr(obj, 'x') removes an instance attribute dynamically

Correct Answer: (A)

[EN] Python has no truly unbreakable private access modifier; __var is simply mangled to _ClassName__var and remains accessible.

[HI] पायथन में पूर्णतः अलंघ्य private कीवर्ड नहीं होता; __var केवल _ClassName__var में मैंगल होता है और एक्सेस किया जा सकता है।

Q16. What does vars(obj) return for a standard Python class instance?

(एक सामान्य पायथन क्लास इंस्टेंस के लिए vars(obj) क्या लौटाता है?)

(A) The list of all global variables in the module

(B) The memory address in hexadecimal

(C) The obj.__dict__ attribute dictionary

(D) A tuple of all methods in the class

Correct Answer: (C)

[EN] When called with an object having __dict__, vars(obj) returns obj.__dict__ directly.

[HI] जिस ऑब्जेक्ट में __dict__ होता है, उसके लिए vars(obj) सीधे obj.__dict__ डिक्शनरी लौटाता है।

Q17. Within Object-Oriented Programming (OOP) in Python: Classes and Objects, which oop construct directly maps to: "Per-instance dictionary holding attributes"?

(Object-Oriented Programming (OOP) in Python: Classes and Objects में कौन सा OOP Construct सीधे "Per-instance dictionary holding attributes" से संबंधित है?)

(A) obj.__dict__

(B) _attr (single)

(C) __slots__

(D) __attr (double)

Correct Answer: (A)

[EN] In Object-Oriented Programming (OOP) in Python: Classes and Objects, 'obj.__dict__' handles 'Per-instance dictionary holding attributes' (Removed when __slots__ is declared).

[HI] Object-Oriented Programming (OOP) in Python: Classes and Objects में 'obj.__dict__' का कार्य/विवरण 'Per-instance dictionary holding attributes' (Removed when __slots__ is declared) है।

Q18. Which option accurately describes '_attr (single)' in Object-Oriented Programming (OOP) in Python: Classes and Objects?

(Object-Oriented Programming (OOP) in Python: Classes and Objects के संदर्भ में '_attr (single)' की सही विशेषता कौन सी है?)

(A) Per-instance dictionary holding attributes

(B) Tuple fixing allowed instance attributes

(C) Name-mangled internally to _ClassName__attr

(D) Convention marking protected/internal member

Correct Answer: (D)

[EN] '_attr (single)' provides 'Convention marking protected/internal member' — key point: Remains directly accessible outside class.

[HI] '_attr (single)' का मुख्य विवरण 'Convention marking protected/internal member' है (Remains directly accessible outside class)।

Q19. In Object-Oriented Programming (OOP) in Python: Classes and Objects, identify the correct parameter for the specification: "Name-mangled internally to _ClassName__attr".

(Object-Oriented Programming (OOP) in Python: Classes and Objects में "Name-mangled internally to _ClassName__attr" विवरण किस विकल्प के लिए सही है?)

(A) obj.__dict__

(B) __slots__

(C) _attr (single)

(D) __attr (double)

Correct Answer: (D)

[EN] '__attr (double)' corresponds to 'Name-mangled internally to _ClassName__attr' (Prevents accidental subclass collisions).

[HI] '__attr (double)' का संबंध 'Name-mangled internally to _ClassName__attr' (Prevents accidental subclass collisions) से है।

Q20. Considering Object-Oriented Programming (OOP) in Python: Classes and Objects, what key behavior defines '__slots__'?

(Object-Oriented Programming (OOP) in Python: Classes and Objects में '__slots__' की मुख्य भूमिका या मान क्या है?)

(A) Convention marking protected/internal member

(B) Name-mangled internally to _ClassName__attr

(C) Tuple fixing allowed instance attributes

(D) Per-instance dictionary holding attributes

Correct Answer: (C)

[EN] '__slots__' represents 'Tuple fixing allowed instance attributes' (Reduces memory use; forbids dynamic attrs).

[HI] '__slots__' का अर्थ/मान 'Tuple fixing allowed instance attributes' है (Reduces memory use; forbids dynamic attrs)।

Object-Oriented Programming (OOP) in Python: Classes and Objects — Exam FAQs

Q1: Does Python have true private variables like Java or C++?

क्या पायथन में जावा या C++ की तरह वास्तविक private वेरिएबल होते हैं?

[EN] No. Python enforces encapsulation by convention ('we are all consenting adults'): single underscore _x signals internal use, while double underscore __x mangles the name to _ClassName__x to prevent accidental subclass clashes rather than blocking deliberate access.

[HI] नहीं। पायथन में सख्त private एक्सेस मॉडिफायर के बजाय परंपरा का पालन किया जाता है: एक अंडरस्कोर (_x) आंतरिक उपयोग दर्शाता है और दो अंडरस्कोर (__x) सबक्लास नाम-टकराव रोकने हेतु नेम मैंगलिंग करते हैं।

Q2: When should __slots__ be used in a Python class?

पायथन क्लास में __slots__ का उपयोग कब करना चाहिए?

[EN] Use __slots__ when creating huge numbers (thousands or millions) of small objects with a fixed set of attributes, as omitting per-instance __dict__ saves significant RAM and speeds up attribute access.

[HI] जब किसी निश्चित एट्रिब्यूट सेट वाले हजारों या लाखों छोटे ऑब्जेक्ट बनाने हों, तब __slots__ का उपयोग करने से प्रत्येक ऑब्जेक्ट की __dict__ न बनने के कारण काफी मेमोरी बचती है।

Q3: What is the difference between a class and an object in Python?

पायथन में क्लास और ऑब्जेक्ट के बीच क्या अंतर है?

[EN] A class is the blueprint (itself an instance of type) that defines attributes and methods once, whereas an object (instance) is a concrete runtime entity created from that blueprint with its own state in memory.

[HI] क्लास एक ब्लूप्रिंट है जो एट्रिब्यूट्स और मेथड्स की संरचना एक बार तय करता है, जबकि ऑब्जेक्ट (इंस्टेंस) उस क्लास से मेमोरी में निर्मित वास्तविक इकाई है जो अपना अलग डेटा रखती है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)