पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 20 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
क्या marker interface में methods हो सकते हैं?
Can a marker interface have methods?
Aहाँ, पर implement करने वाली क्लासों को उन्हें implement करना होगा।Yes, but they must be implemented by the implementing classes.
Bनहीं, marker interfaces में कोई method नहीं होता।No, marker interfaces do not have any methods.
Cहाँ, पर वे abstract methods होने चाहिए।Yes, but they must be abstract methods.
Dनहीं, marker interfaces के सभी methods अपने-आप final होते हैं।No, all methods in marker interfaces are implicitly final.
सही उत्तर / Answer: (B)
हल / Solution
Marker interface की पहचान ही यह है कि वह खाली होता है; उसमें न method होता है, न field। उसका काम सिर्फ़ क्लास पर निशान लगाना है। अगर उसमें एक भी method जोड़ दें तो वह साधारण इंटरफ़ेस बन जाता है, marker नहीं रहता; इसलिए पहला और तीसरा विकल्प ग़लत हैं। चौथा विकल्प अपने-आप में उलटा है, क्योंकि जिसमें method है ही नहीं उसके methods final कैसे होंगे। साथ ही इंटरफ़ेस के methods final हो भी नहीं सकते।
The defining feature of a marker interface is that it is empty, with no methods and no fields. Its only job is to tag a class. If you add even one method, it becomes an ordinary interface and is no longer a marker, so options 1 and 3 are wrong. Option 4 contradicts itself: an interface with no methods cannot have final methods, and interface methods can never be final anyway.
प्रश्न 2उत्तर + हल
निम्नलिखित में से कौन-सा marker interface बताता है कि ऑब्जेक्ट की copy बनाई जा सकती है?
Which of the following interfaces is a marker interface that indicates that objects can be copied?
ACloneable
BComparable
CSerializable
DIterable
सही उत्तर / Answer: (A)
हल / Solution
Cloneable (java.lang) एक खाली marker interface है। Object क्लास का clone() method पहले देखता है कि क्लास ने Cloneable implement किया है या नहीं; न किया हो तो CloneNotSupportedException आता है। इसलिए copy बनाने की अनुमति यही देता है। Comparable में compareTo() method है और यह ऑब्जेक्टों की तुलना के लिए है। Serializable ऑब्जेक्ट को byte stream में बदलने के लिए है। Iterable में iterator() method है, जिससे for-each loop चलता है।
Cloneable in java.lang is an empty marker interface. The clone() method of Object first checks whether the class implements Cloneable; if not, it throws CloneNotSupportedException. So this is the interface that allows copying. Comparable has compareTo() and is for comparing objects. Serializable is for turning an object into a byte stream. Iterable has iterator(), which powers the for-each loop.
प्रश्न 3उत्तर + हल
जब कोई क्लास marker interface को implement करती है, तो इसका क्या अर्थ होता है?
When a class implements a marker interface, what does it signify?
Aक्लास को इंटरफ़ेस के सभी methods अपने-आप मिल जाते हैं।The class automatically inherits all methods from the interface.
Bक्लास को इंटरफ़ेस के सभी methods override करने पड़ते हैं।The class must override all methods from the interface.
Cक्लास compiler या JVM को बताती है कि उसमें कुछ ख़ास गुण हैं या उससे अलग तरह से बर्ताव किया जाए।The class indicates to the compiler or JVM that it possesses certain characteristics or should be treated differently.
Dउस क्लास का ऑब्जेक्ट नहीं बनाया जा सकता।The class cannot be instantiated.
सही उत्तर / Answer: (C)
हल / Solution
Marker interface खाली होता है, इसलिए उसे implement करना सिर्फ़ एक घोषणा है कि इस क्लास में कोई ख़ास गुण है। compiler इसे type के रूप में जाँच सकता है, और JVM या libraries instanceof से देखकर अलग बर्ताव करती हैं, जैसे Serializable वाली क्लास ही serialize होती है। इंटरफ़ेस में कोई method है ही नहीं, इसलिए न कुछ inherit होता है, न override करना पड़ता है। क्लास का ऑब्जेक्ट बनना भी इससे नहीं रुकता।
A marker interface is empty, so implementing it is only a declaration that the class has some special property. The compiler can check it as a type, and the JVM or libraries test it with instanceof and act differently, for example only Serializable classes get serialized. The interface has no methods, so nothing is inherited and nothing must be overridden. It also does not stop the class from being instantiated.
प्रश्न 4उत्तर + हल
निम्नलिखित में से कौन-सा Java में marker interface नहीं है?
Which of the following is NOT a marker interface in Java?
ASerializable
BRunnable
CCloneable
DRemote
सही उत्तर / Answer: (B)
हल / Solution
Marker interface में कोई method नहीं होता। Runnable में एक abstract method run() है, जिसे thread चलाने वाली क्लास लिखती है, इसलिए यह marker नहीं बल्कि functional interface है। Serializable (java.io) खाली है और ऑब्जेक्ट को serialize करने की अनुमति का निशान है। Cloneable (java.lang) खाली है और clone() की अनुमति देता है। Remote (java.rmi) भी खाली है और RMI में दूर से बुलाए जा सकने वाले ऑब्जेक्ट को चिह्नित करता है।
A marker interface has no methods. Runnable has one abstract method, run(), which a thread class must write, so it is a functional interface, not a marker. Serializable in java.io is empty and tags objects that may be serialized. Cloneable in java.lang is empty and permits clone(). Remote in java.rmi is also empty and tags objects whose methods can be called remotely through RMI.
प्रश्न 5उत्तर + हल
Marker interfaces को और किस नाम से जाना जाता है?
Marker interfaces are also known as:
AFunctional interfaces
BEmpty interfaces (खाली इंटरफ़ेस)Empty interfaces
CAbstract interfaces
DTyped interfaces
सही उत्तर / Answer: (B)
हल / Solution
Marker interface में न कोई method होता है, न कोई field; वह बिल्कुल खाली होता है। इसलिए इसे empty interface (खाली इंटरफ़ेस) कहते हैं, और tagging interface भी। Serializable, Cloneable और Remote इसके उदाहरण हैं। Functional interface में ठीक एक abstract method होता है, जैसे Runnable, इसलिए वह marker नहीं है। Abstract interface और typed interface Java में कोई अलग प्रकार नहीं हैं; हर इंटरफ़ेस वैसे भी abstract ही होता है।
A marker interface has no methods and no fields; it is completely empty. That is why it is called an empty interface, and also a tagging interface. Serializable, Cloneable and Remote are examples. A functional interface has exactly one abstract method, like Runnable, so it is not a marker. Abstract interface and typed interface are not separate kinds in Java; every interface is abstract anyway.
प्रश्न 6उत्तर + हल
Java में इंटरफ़ेस variables को मान (initialize) कैसे दिया जाता है?
In Java, how are interface variables initialized?
Aघोषणा के समय ही उन्हें स्पष्ट मान देना ज़रूरी है।They must be explicitly initialized when declared.
Bउन्हें अपने-आप default मान मिल जाता है।They are automatically initialized to their default values.
Cजब implement करने वाली क्लास मान देती है तब वे initialize होते हैं।They are initialized when the implementing class provides a value.
Dइंटरफ़ेस variables को मान दिया ही नहीं जा सकता।Interface variables cannot be initialized.
सही उत्तर / Answer: (A)
हल / Solution
इंटरफ़ेस का हर variable final होता है, और उसे बाद में मान देने के लिए न constructor है, न initializer block। इसलिए घोषणा के समय ही मान देना पड़ता है, जैसे int MAX = 10;। सिर्फ़ int MAX; लिखने पर compile error आता है। इसलिए default मान (0 या null) अपने-आप नहीं मिलता। implement करने वाली क्लास final स्थिरांक का मान नहीं बदल सकती। चौथा विकल्प भी ग़लत है, क्योंकि मान देना तो ज़रूरी है।
Every interface variable is final, and an interface has no constructor or initializer block to set it later. So it must get its value right where it is declared, like int MAX = 10;. Writing just int MAX; gives a compile error. It never gets an automatic default value like 0 or null. An implementing class cannot set a final constant. Option 4 is wrong because initialization is actually required.
प्रश्न 7उत्तर + हल
Java में इंटरफ़ेस variables के बारे में निम्नलिखित में से कौन-सा कथन सत्य है?
Which of the following statements regarding interface variables in Java is true?
Aइंटरफ़ेस variables अपने-आप static और final होते हैं।Interface variables are by default static and final.
Bimplement करने वाली क्लासें इंटरफ़ेस variables को सीधे पढ़ और बदल सकती हैं।Interface variables can be directly accessed and modified by implementing classes.
Cइंटरफ़ेस variables तक सिर्फ़ इंटरफ़ेस में लिखे static methods से पहुँचा जा सकता है।Interface variables can only be accessed through static methods defined in the interface.
Dइंटरफ़ेस variables पर कोई access modifier नहीं लग सकता।Interface variables cannot have access modifiers.
सही उत्तर / Answer: (A)
हल / Solution
इंटरफ़ेस में लिखा हर variable अपने-आप public, static और final होता है, यानी वह एक स्थिरांक है। final होने के कारण कोई क्लास उसका मान बदल नहीं सकती, इसलिए दूसरा कथन ग़लत है। उसे इंटरफ़ेस के नाम से या implement करने वाली क्लास में सीधे नाम से पढ़ा जा सकता है, इसलिए तीसरा ग़लत है। public लिखने की छूट है, भले वह ज़रूरी न हो, इसलिए चौथा भी ग़लत है।
Every variable declared in an interface is automatically public, static and final, so it is a constant. Because it is final, no class can change its value, so option 2 is wrong. It can be read through the interface name or directly by name inside an implementing class, so option 3 is wrong. Writing public is allowed, though redundant, so option 4 is also wrong.
प्रश्न 8उत्तर + हल
Java में क्लास और इंटरफ़ेस के संबंध का सही वर्णन कौन-सा है?
Which of the following accurately describes the relationship between a class and an interface in Java?
Aक्लास extends कीवर्ड से इंटरफ़ेस को extend कर सकती है।A class can extend an interface using the extends keyword.
Bक्लास implements कीवर्ड से इंटरफ़ेस को implement कर सकती है।A class can implement an interface using the implements keyword.
Cक्लास इंटरफ़ेस से state और behavior पा सकती है।A class can inherit state and behavior from an interface.
Dक्लास सीधे इंटरफ़ेस का ऑब्जेक्ट बना सकती है।A class can instantiate an interface directly.
सही उत्तर / Answer: (B)
हल / Solution
क्लास इंटरफ़ेस को implements कीवर्ड से अपनाती है, जैसे class Dog implements Animal। extends कीवर्ड क्लास से क्लास या इंटरफ़ेस से इंटरफ़ेस के बीच लगता है; क्लास इंटरफ़ेस को extend नहीं करती। इंटरफ़ेस में instance fields नहीं होते, इसलिए क्लास को उससे state नहीं मिलती। इंटरफ़ेस अधूरा होता है, इसलिए new लिखकर उसका सीधा ऑब्जेक्ट नहीं बनता। इसलिए दूसरा विकल्प सही है।
A class adopts an interface with the implements keyword, like class Dog implements Animal. The extends keyword is used between two classes or between two interfaces; a class never extends an interface. An interface has no instance fields, so a class gets no state from it. An interface is incomplete, so you cannot create its object directly with new. So option 2 is correct.
प्रश्न 9उत्तर + हल
इंटरफ़ेस के बारे में निम्नलिखित में से कौन-सा कथन सत्य है?
Which of the following statements is true regarding interfaces?
Aइंटरफ़ेस में constructor हो सकते हैं।Interfaces can contain constructors.
Bइंटरफ़ेस में access modifiers वाले instance variables हो सकते हैं।Interfaces can contain instance variables with access modifiers.
Cइंटरफ़ेस से behavior का contract तय किया जा सकता है।Interfaces can be used to define contracts for behavior.
Dइंटरफ़ेस दूसरी क्लासों के protected members तक पहुँच सकते हैं।Interfaces can access protected members of other classes.
सही उत्तर / Answer: (C)
हल / Solution
इंटरफ़ेस एक contract है: वह बताता है कि implement करने वाली हर क्लास को कौन-से methods देने ही होंगे। इसका ऑब्जेक्ट नहीं बनता, इसलिए constructor नहीं होता। इसमें instance variables नहीं होते; हर variable अपने-आप public static final स्थिरांक होता है। protected members inheritance और पैकेज से जुड़े हैं; इंटरफ़ेस को दूसरी क्लासों के protected members तक कोई ख़ास पहुँच नहीं मिलती। इसलिए तीसरा कथन ही सही है।
An interface is a contract: it states which methods every implementing class must provide. It cannot be instantiated, so it has no constructors. It has no instance variables; every variable is automatically a public static final constant. Protected members are tied to inheritance and packages, and an interface gets no special access to the protected members of other classes. So only the third statement is true.
प्रश्न 10उत्तर + हल
Java में एक क्लास implement कर सकती है:
In Java, a class can implement:
Aकई क्लासें।Multiple classes.
Bकई इंटरफ़ेस।Multiple interfaces.
Cकई क्लासें और इंटरफ़ेस दोनों।Both multiple classes and interfaces.
Dन कई क्लासें, न कई इंटरफ़ेस।Neither multiple classes nor interfaces.
सही उत्तर / Answer: (B)
हल / Solution
implements कीवर्ड सिर्फ़ इंटरफ़ेस के साथ लगता है, और एक क्लास comma लगाकर कई इंटरफ़ेस implement कर सकती है, जैसे class A implements X, Y। क्लास को implement नहीं किया जाता, उसे extends किया जाता है। Java में एक क्लास सिर्फ़ एक ही क्लास को extends कर सकती है, ताकि diamond problem न हो। इसलिए पहला और तीसरा विकल्प ग़लत हैं। चौथा भी ग़लत है, क्योंकि कई इंटरफ़ेस की छूट है।
The implements keyword works only with interfaces, and a class can implement many of them separated by commas, like class A implements X, Y. A class is never implemented; it is extended. Java lets a class extend only one class, which avoids the diamond problem. So options 1 and 3 are wrong, and option 4 is wrong too because multiple interfaces are allowed.
आगे के 20 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
Java में इंटरफ़ेस की तुलना में inheritance (extends) का सही वर्णन कौन-सा कथन करता है?
Which statement accurately describes inheritance (extends) in Java compared to interfaces?
Ainheritance से क्लास दूसरी क्लास की state और behavior दोनों पाती है, जबकि इंटरफ़ेस सिर्फ़ behavior का contract तय करते हैं।Inheritance allows a class to inherit both state and behavior from another class, while interfaces only define contracts for behavior.
Bइंटरफ़ेस से क्लास दूसरी क्लास की state और behavior दोनों पाती है, जबकि inheritance (extends) सिर्फ़ behavior का contract तय करता है।Interfaces allow a class to inherit both state and behavior from another class, while inheritance (extends) only defines contracts for behavior.
Cinheritance और इंटरफ़ेस दोनों से क्लास दूसरी क्लास की state और behavior पाती है।Inheritance and interfaces both allow a class to inherit state and behavior from another class.
DJava में inheritance (extends) और इंटरफ़ेस एक-दूसरे की जगह इस्तेमाल होने वाली अवधारणाएँ हैं।Inheritance (extends) and interfaces are interchangeable concepts in Java.