Dएक्सेसर और म्यूटेटर method (Accessor and mutator methods)Accessor and mutator methods
सही उत्तर / Answer: (C)
हल / Solution
access modifier यानी private, default, protected और public तय करते हैं कि class का कौन-सा गुण या method कहाँ से दिख सकता है। इन्हीं से class अपने गुणों और method, दोनों पर पूरा नियंत्रण रखती है। getter-setter भी मदद करते हैं, पर वे सिर्फ़ गुणों तक पहुँच देते हैं। वे भी तभी काम आते हैं जब गुण private हों, यानी वे भी access modifier पर टिके हैं। inheritance और polymorphism का पहुँच-नियंत्रण से सीधा संबंध नहीं है।
Access modifiers, namely private, default, protected and public, decide where each attribute or method of a class can be seen. With them, a class controls both its attributes and its methods. Getters and setters help too, but they only cover attributes, and they work only because the attributes are made private by an access modifier. Inheritance and polymorphism are not directly about access control. So option 3 is correct.
प्रश्न 2उत्तर + हल
निम्नलिखित में से कौन-सा encapsulation का लाभ नहीं है?
Which of the following is NOT an advantage of encapsulation?
Aयह संवेदनशील डेटा तक अनधिकृत पहुँच रोककर सुरक्षा बढ़ाता है।It enhances security by preventing unauthorized access to sensitive data.
Bयह implementation का ब्योरा छिपाकर रखरखाव आसान बनाता है, जिससे प्रोग्राम के दूसरे हिस्सों पर असर डाले बिना बदलाव किए जा सकते हैं।It improves maintainability by hiding implementation details, allowing changes to be made without affecting other parts of the program.
Cयह मौजूदा class को उपयोग करना और आगे बढ़ाना आसान बनाकर पुनः उपयोग (reusability) को बढ़ावा देता है।It promotes reusability by making it easier to use and extend existing classes.
Dयह object के अंदरूनी ब्योरे बाहरी class के सामने खोलकर जटिलता बढ़ाता है।It increases complexity by exposing internal details of objects to external classes.
सही उत्तर / Answer: (D)
हल / Solution
encapsulation के मुख्य लाभ हैं: private डेटा से सुरक्षा, छिपे हुए implementation से आसान रखरखाव, और साफ़ public interface से पुनः उपयोग। इसलिए पहले तीन विकल्प सच्चे लाभ हैं। चौथा विकल्प कहता है कि यह अंदरूनी ब्योरे खोलकर जटिलता बढ़ाता है। यह encapsulation का काम नहीं, उसका उलटा है। encapsulation ब्योरे छिपाता है और जटिलता घटाता है। इसलिए 'लाभ नहीं' वाला सही उत्तर चौथा विकल्प है।
The main benefits of encapsulation are security through private data, easier maintenance because the implementation is hidden, and reusability through a clean public interface. So the first three options are real benefits. Option 4 says it increases complexity by exposing internal details. That is the opposite of what encapsulation does, because it hides details and lowers complexity. So option 4 is the correct answer to this NOT question.
प्रश्न 3उत्तर + हल
Java में encapsulation के लिए, जो method class के बाहर से उपयोग के लिए बने हों, उन पर कौन-सा access modifier लगाना चाहिए?
In Java, which access modifier should be used for methods that are intended to be accessible from outside the class to achieve encapsulation?
Aprivate
Bpublic
Cprotected
Ddefault
सही उत्तर / Answer: (B)
हल / Solution
encapsulation में fields private रखे जाते हैं, पर उन तक पहुँचने वाले getter-setter जैसे method बाहर से बुलाए जाने चाहिए। कोई भी दूसरी class, किसी भी package से, इन्हें तभी बुला सकती है जब ये public हों। private method सिर्फ़ उसी class में दिखता है। protected सिर्फ़ उसी package और subclass तक सीमित है। default यानी बिना modifier वाला method सिर्फ़ उसी package में दिखता है। इसलिए सही उत्तर public है।
In encapsulation, fields are private, but methods such as getters and setters must be callable from outside. Any class in any package can call them only if they are public. A private method is visible only inside its own class. A protected method is limited to the same package and subclasses. A default method, with no modifier, is visible only inside its own package. So the correct answer is public.
प्रश्न 4उत्तर + हल
निम्नलिखित में से कौन-सी encapsulation की एक विशेषता है?
Which of the following is a characteristic of encapsulation?
Aयह class के बाहर से object के सभी गुणों तक सीधी पहुँच देता है।It allows direct access to all attributes of an object from outside the class.
Bयह प्रोग्राम के अलग-अलग हिस्सों के बीच loose coupling (ढीले जुड़ाव) को बढ़ावा देता है।It promotes loose coupling between different parts of a program.
Cयह global variables के उपयोग को बढ़ावा देता है।It encourages the use of global variables.
Dइसमें object के सारे अंदरूनी implementation ब्योरे खोल दिए जाते हैं।It involves exposing all internal implementation details of an object.
सही उत्तर / Answer: (B)
हल / Solution
encapsulation में हर class अपना डेटा private रखती है और बाहर सिर्फ़ public method देती है। दूसरी class इन्हीं method पर निर्भर रहती हैं, अंदर के ब्योरे पर नहीं। इसलिए एक class का अंदरूनी कोड बदलने पर बाक़ी हिस्से नहीं टूटते। इसे loose coupling कहते हैं। सीधी पहुँच देना और सारे ब्योरे खोल देना encapsulation का उलटा है। global variables डेटा को सबके लिए खोल देते हैं, इसलिए वे भी इसके ख़िलाफ़ हैं।
In encapsulation, each class keeps its data private and offers only public methods. Other classes depend on these methods, not on the inner details, so changing one class's internal code does not break the rest of the program. This is called loose coupling. Direct access to all attributes and exposing all internal details are the opposite of encapsulation. Global variables open data to everyone, so they work against it too.
प्रश्न 5उत्तर + हल
encapsulation में डेटा छिपाने (data hiding) की अवधारणा को निम्नलिखित में से कौन-सा कथन सबसे अच्छी तरह बताता है?
Which of the following statements best describes the concept of data hiding in encapsulation?
Adata hiding का मतलब है object का सारा अंदरूनी डेटा बाहरी class के सामने खोल देना।Data hiding refers to the process of exposing all internal data of an object to external classes.
Bdata hiding का मतलब है class के कुछ डेटा तक पहुँच सीमित करना, ताकि सिर्फ़ अधिकृत method ही उसे पढ़ या बदल सकें।Data hiding refers to the process of restricting access to certain data within a class, allowing only authorized methods to access or modify it.
Cdata hiding का मतलब है class के सभी गुण public करना, ताकि बाहर से आसानी से पहुँच हो।Data hiding refers to the process of making all attributes of a class public for easy access from outside the class.
Ddata hiding का मतलब है class के सभी गुण private करना, ताकि बाहर से कोई भी पहुँच न हो सके।Data hiding refers to the process of declaring all attributes of a class as private, preventing any access from outside the class.
सही उत्तर / Answer: (B)
हल / Solution
data hiding में डेटा पर पूरा ताला नहीं लगाया जाता, बल्कि उस तक पहुँचने का रास्ता तय किया जाता है। डेटा private रहता है और सिर्फ़ class के अपने अधिकृत method उसे पढ़ते या बदलते हैं। इसलिए दूसरा कथन सही है। पहला और तीसरा कथन डेटा खोल देते हैं, जो इसका उलटा है। चौथा कथन कहता है कि कोई भी पहुँच न हो। पर getter-setter से नियंत्रित पहुँच मिलती है, इसलिए वह भी ग़लत है।
Data hiding does not lock data away completely; it controls the path to it. The data stays private, and only the class's own authorized methods read or change it. So statement 2 is correct. Statements 1 and 3 expose the data, which is the opposite of hiding. Statement 4 says no access at all is possible, but getters and setters still give controlled access, so it is also wrong.
प्रश्न 6उत्तर + हल
यदि आप encapsulation को दरकिनार करके किसी object के गुणों (attributes) को सीधे पढ़ें या बदलें, तो क्या होता है?
What happens if you bypass encapsulation and directly access or modify attributes of an object?
Aयह encapsulation के सिद्धांतों को तोड़ता है और अनचाहा व्यवहार पैदा कर सकता है।It violates encapsulation principles and can lead to unexpected behavior.
Bयह प्रोग्राम की सुरक्षा बढ़ाता है।It enhances the security of the program.
Cयह debugging की प्रक्रिया को आसान बनाता है।It simplifies the debugging process.
Dयह प्रोग्राम का प्रदर्शन (performance) सुधारता है।It improves the performance of the program.
सही उत्तर / Answer: (A)
हल / Solution
encapsulation में गुणों को सिर्फ़ method से बदला जाता है, ताकि हर बदलाव की जाँच हो सके। अगर कोई कोड सीधे गुण बदल दे, तो ये जाँचें छूट जाती हैं। जैसे खाते का balance ऋणात्मक हो सकता है। इससे object की स्थिति बिगड़ती है और अनचाहा व्यवहार होता है। सुरक्षा बढ़ती नहीं, घटती है। ग़लती कहाँ से आई, यह ढूँढ़ना भी कठिन होता है। प्रदर्शन में मिलने वाला फ़र्क़ न के बराबर होता है।
In encapsulation, attributes are changed only through methods, so every change can be checked. If code changes an attribute directly, these checks are skipped. For example, an account balance could become negative. This breaks the object's state and causes unexpected behavior. Security gets weaker, not stronger. Finding the source of a bug also becomes harder, and any performance gain is negligible. So option 1 is correct.
प्रश्न 7उत्तर + हल
निम्नलिखित में से कौन-सा Java में encapsulation का हिस्सा नहीं है?
Which of the following is NOT a part of encapsulation in Java?
Dprivate गुण (Private attributes)Private attributes
सही उत्तर / Answer: (C)
हल / Solution
encapsulation के तीन मुख्य हिस्से हैं। पहला, private गुण, जिससे डेटा छिपता है। दूसरा, accessor यानी getter method, जिनसे मान पढ़ा जाता है। तीसरा, mutator यानी setter method, जिनसे मान नियंत्रित ढंग से बदला जाता है। class inheritance एक अलग OOP सिद्धांत है। इसमें एक class दूसरी class के गुण और method विरासत में लेती है। यह encapsulation का हिस्सा नहीं है, इसलिए सही उत्तर तीसरा विकल्प है।
Encapsulation has three main parts. Private attributes hide the data. Accessor methods, or getters, let outside code read values. Mutator methods, or setters, let values change in a controlled way. Class inheritance is a separate OOP principle in which one class receives the attributes and methods of another class. It is not a part of encapsulation, so option 3 is the correct answer to this NOT question.
प्रश्न 8उत्तर + हल
encapsulation में गुणों (attributes) को private घोषित करने की सलाह क्यों दी जाती है?
In encapsulation, why is it recommended to declare attributes as private?
Aclass के बाहर से सीधी पहुँच देने के लिए।To allow direct access from outside the class.
Bडेटा छिपाने (data hiding) को बढ़ावा देने और class के बाहर से सीधे बदलाव रोकने के लिए।To promote data hiding and prevent direct manipulation from outside the class.
Csubclass में गुणों की विरासत (inheritance) पक्की करने के लिए।To enforce inheritance of attributes to subclasses.
Dगुणों की दृश्यता (visibility) बढ़ाने के लिए।To increase the visibility of attributes.
सही उत्तर / Answer: (B)
हल / Solution
private field को सिर्फ़ उसी class के अंदर से पढ़ा या बदला जा सकता है। बाहर का कोड उसे सीधे नहीं छू सकता। उसे getter या setter method से होकर जाना पड़ता है, जहाँ जाँच लगाई जा सकती है। इससे डेटा छिपा रहता है और ग़लत बदलाव रुकते हैं। पहला और चौथा विकल्प private का उलटा असर बताते हैं। private सदस्य subclass को सीधे मिलते भी नहीं, इसलिए तीसरा विकल्प भी ग़लत है।
A private field can be read or changed only inside its own class. Outside code cannot touch it directly and must go through getter or setter methods, where checks can be added. This keeps the data hidden and blocks wrong changes. Options 1 and 4 describe the opposite effect of private. Private members are not even directly accessible in subclasses, so option 3 is also wrong. Hence option 2 is correct.
प्रश्न 9उत्तर + हल
Java में encapsulation कैसे प्राप्त किया जाता है?
In Java, how is encapsulation achieved?
Aसभी गुणों (attributes) को public घोषित करके।By declaring all attributes as public.
Bobject की स्थिति पढ़ने और बदलने के लिए public setter और getter method देकर।By providing public setter and getter methods to access and modify the state of an object.
Cसभी गुणों के लिए protected access modifier लगाकर।By using the protected access modifier for all attributes.
Dसभी method के लिए static keyword लगाकर।By using the static keyword for all methods.
सही उत्तर / Answer: (B)
हल / Solution
Java में encapsulation दो क़दमों में होता है। पहला, fields को private रखा जाता है। दूसरा, उन्हें पढ़ने और बदलने के लिए public getter और setter method दिए जाते हैं। setter में जाँच लगाकर ग़लत मान रोका जा सकता है। विकल्पों में सिर्फ़ दूसरा इसी तरीके को बताता है। सब कुछ public करना डेटा को खोल देता है। protected से subclass और package को सीधी पहुँच मिलती है। static का encapsulation से कोई संबंध नहीं है।
Encapsulation in Java is done in two steps. First, fields are declared private. Second, public getter and setter methods are provided to read and change them, and a setter can reject bad values. Among the options, only option 2 describes this approach. Making attributes public exposes the data. Protected still gives direct access to subclasses and the package. The static keyword has nothing to do with encapsulation.
प्रश्न 10उत्तर + हल
encapsulation के बारे में निम्नलिखित में से कौन-सा कथन सही है?
Which of the following is true regarding encapsulation?
Aencapsulation class के बाहर से object की अंदरूनी स्थिति (state) तक सीधी पहुँच देता है।Encapsulation allows direct access to the internal state of an object from outside the class.
Bencapsulation डेटा छिपाने (data hiding) को बढ़ावा देता है और प्रोग्राम के अलग-अलग हिस्सों के बीच coupling घटाता है।Encapsulation promotes data hiding and reduces coupling between different parts of a program.
Cencapsulation class के सभी गुण और method public बना देता है।Encapsulation makes all attributes and methods of a class publicly accessible.
Dencapsulation केवल static method और गुणों पर लागू होता है।Encapsulation is only applicable to static methods and attributes.
सही उत्तर / Answer: (B)
हल / Solution
encapsulation में fields को private रखा जाता है और उन तक पहुँच सिर्फ़ method के ज़रिए होती है। इससे डेटा छिपा रहता है। दूसरी class सिर्फ़ public method पर निर्भर रहती हैं, अंदर की बनावट पर नहीं। इसलिए हिस्सों के बीच coupling (आपसी निर्भरता) घटती है। सीधी पहुँच देना या सब कुछ public करना encapsulation का उलटा है। यह सिद्धांत हर तरह के field और method पर लागू होता है, सिर्फ़ static पर नहीं।
In encapsulation, fields are kept private and are reached only through methods, so the data stays hidden. Other classes depend only on the public methods, not on the inner structure, which reduces coupling between parts of the program. Giving direct access to internal state or making everything public is the opposite of encapsulation. The idea applies to all fields and methods, not only static ones. So option 2 is correct.
आगे के 20 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
Aparent class से गुण और method विरासत में लेने की क्षमता।The ability to inherit attributes and methods from a parent class.
Bobject के अंदरूनी implementation का ब्योरा छिपाकर सिर्फ़ ज़रूरी काम बाहर दिखाने की प्रक्रिया।The process of hiding the internal implementation details of an object and only exposing necessary functionality.
Cएक ही नाम पर अलग-अलग काम वाले कई method बनाने की प्रक्रिया।The process of defining multiple methods with the same name but different implementations.
Dएक class के कई object (instance) बनाने की प्रक्रिया।The process of creating multiple instances of a class.
Java में एब्स्ट्रैक्शन के उद्देश्य को निम्नलिखित में से कौन-सा सबसे अच्छी तरह बताता है?
Which of the following best describes the purpose of abstraction in Java?
Aप्रोग्राम की जटिलता बढ़ाना।To increase program complexity.
Bobject के implementation का ब्योरा छिपाकर सिर्फ़ ज़रूरी गुण दिखाना।To hide the implementation details and only show the necessary features of an object.
Cएक ही नाम पर अलग-अलग काम वाले कई method बनाना।To define multiple methods with the same name but different implementations.
Dप्रोग्राम को छोटी, सँभालने लायक इकाइयों में तोड़ना।To break down a program into smaller, manageable units.
Aएक ही नाम पर अलग-अलग काम वाले कई method बनाने की प्रक्रिया।The process of defining multiple methods with the same name but different implementations.
Bकिसी object के implementation का ब्योरा छिपाकर सिर्फ़ ज़रूरी गुण दिखाने की प्रक्रिया।The process of hiding the implementation details and showing only essential features of an object.
Cप्रोग्राम को छोटी, सँभालने लायक इकाइयों में तोड़ने की प्रक्रिया।The process of breaking down a program into smaller, manageable units.
Dक्लास से object बनाने की प्रक्रिया।The process of creating objects from classes.
एक फ़ोन कई घटकों से बना होता है, जैसे मदरबोर्ड, कैमरा, सेंसर आदि। यदि प्रोसेसर फ़ोन के सभी कामकाज का प्रतिनिधित्व करता है, डिस्प्ले केवल डिस्प्ले दिखाता है, और फ़ोन को संपूर्ण रूप में दर्शाया जाता है, तो निम्नलिखित में से किसमें एब्स्ट्रैक्शन का स्तर उच्चतम है?
A phone is made up of many components like motherboard, camera, sensors etc. If the processor represents all the functioning of the phone, the display shows the display only, and the phone is represented as a whole, which among the following has the highest level of abstraction?
Aयह कई data members और उन पर काम करने वाली member functions को एक इकाई में जोड़ने का तरीक़ा हैIt is a way of combining various data members and member functions that operate on those data members into a single unit
Bयह data members और member functions को एक इकाई में जोड़ने का तरीक़ा है, जो किसी भी डेटा पर काम कर सकती हैIt is a way of combining various data members and member functions into a single unit which can operate on any data
Cयह कई data members को एक इकाई में जोड़ने का तरीक़ा हैIt is a way of combining various data members into a single unit
Dयह कई member functions को एक इकाई में जोड़ने का तरीक़ा हैIt is a way of combining various member functions into a single unit