पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 20 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
नीचे दिए गए जावा प्रोग्राम में, क्लास Circle इंटरफ़ेस Shape को लागू करता है। यहाँ कौन सी बहुरूपता अवधारणा लागू की गई है? interface Shape { void area(); } class Circle implements Shape { public void area() { } }
In the java program given below, the class Circle implements the interface Shape. Which polymorphism concept has been applied here? interface Shape { void area(); } class Circle implements Shape { public void area() { } }
Aमेथड ओवरलोडिंगMethod overloading
Bमेथड ओवरराइडिंगMethod overriding
Cकोई बहुरूपता नहींNo polymorphism
D(A) और (B) दोनोंBoth (A)&(B)
सही उत्तर / Answer: (B)
हल / Solution
इंटरफ़ेस Shape में area() केवल घोषित है, उसका शरीर नहीं है। Circle उसे लागू करते समय area() का अपना शरीर लिखती है, यानी ऊपर वाली घोषणा की जगह अपना रूप दे देती है। यही मेथड ओवरराइडिंग है और यह रनटाइम बहुरूपता का उदाहरण बनता है। ओवरलोडिंग यहाँ है ही नहीं, क्योंकि area() का नाम एक बार ही आया है और उसके पैरामीटर भी नहीं बदले। इसलिए न तीसरा विकल्प ठीक है, न दोनों वाला चौथा।
In the interface Shape the method area() is only declared and has no body. When Circle implements the interface, it supplies its own body for area(), so it replaces the declaration above with a real form. That is method overriding, and it is an example of runtime polymorphism. There is no overloading here, because the name area() appears only once and its parameter list has not been changed. So neither the third option nor the fourth option can be correct.
प्रश्न 2उत्तर + हल
जावा में बहुरूपता (polymorphism) के अंतर्गत कौन सी अवधारणाएँ आती हैं?
What concepts come under polymorphism in Java?
Aमेथड ओवरलोडिंगMethod overloading
Bकंस्ट्रक्टर ओवरलोडिंगConstructor overloading
Cमेथड ओवरराइडिंगMethod overriding
Dउपरोक्त सभीAll the above
सही उत्तर / Answer: (D)
हल / Solution
बहुरूपता का मतलब है एक नाम और कई रूप। मेथड ओवरलोडिंग और कंस्ट्रक्टर ओवरलोडिंग, दोनों में एक ही नाम की कई इकाइयाँ अलग-अलग पैरामीटर के साथ रहती हैं और चुनाव compiler करता है, इसलिए वे संकलन-समय बहुरूपता हैं। मेथड ओवरराइडिंग में चुनाव चलते समय object देखकर होता है, इसलिए वह रनटाइम बहुरूपता है। तीनों इसी अवधारणा के हिस्से हैं, इसलिए सही उत्तर उपरोक्त सभी है, किसी एक को चुनना अधूरा रहेगा।
Polymorphism means one name with many forms. Method overloading and constructor overloading both keep several units with the same name but different parameter lists, and the compiler makes the choice, so they are compile-time polymorphism. Method overriding decides the version while the program runs, by looking at the object, so it is runtime polymorphism. All three belong to the same idea, which is why the correct answer is all the above, and picking any single one would be incomplete.
प्रश्न 3उत्तर + हल
इस प्रोग्राम का आउटपुट है: public class PolymorphismDemoClass2 { void m1(String x){ System.out.println("One"); } protected void m1(String x, String y){ System.out.println("Two"); } public static void main(String[] args){ PolymorphismDemoClass2 obj = new PolymorphismDemoClass2(); obj.m1("ABC"); obj.m1("PQR", "XYZ"); } }
What is the output of this program? public class PolymorphismDemoClass2 { void m1(String x){ System.out.println("One"); } protected void m1(String x, String y){ System.out.println("Two"); } public static void main(String[] args){ PolymorphismDemoClass2 obj = new PolymorphismDemoClass2(); obj.m1("ABC"); obj.m1("PQR", "XYZ"); } }
ATwo One
BOne Two
Cकम्पाइल टाइम एररCompile time error
Dरनटाइम एररRuntime error
सही उत्तर / Answer: (B)
हल / Solution
यहाँ m1 नाम की दो method हैं — एक में एक String पैरामीटर है, दूसरी में दो। पैरामीटर की संख्या अलग है, इसलिए यह ठीक ओवरलोडिंग है। access modifier अलग होने से कोई फ़र्क़ नहीं पड़ता, ओवरलोडिंग में उसे गिना ही नहीं जाता, इसलिए कोई एरर नहीं आता। main में पहले obj.m1("ABC") चला, जिससे One छपा, फिर obj.m1("PQR", "XYZ") चला, जिससे Two छपा। इसलिए क्रम से आउटपुट One Two बनता है।
There are two methods named m1 here, one taking a single String parameter and the other taking two. The number of parameters differs, so this is proper overloading. The different access modifier makes no difference, because a modifier is not a part of the signature, and no error is raised. In main the call obj.m1("ABC") runs first and prints One, then obj.m1("PQR", "XYZ") runs and prints Two. So the output, in order, is One Two.
प्रश्न 4उत्तर + हल
इस प्रोग्राम का आउटपुट है: public class PolymorphismDemoClass { void add(int x, int y){ System.out.println("1. Addition is: " +(x+y)); } void add(int y, int x){ System.out.println("2. Addition is: " +(x+y)); } public static void main(String[] args){ PolymorphismDemoClass obj = new PolymorphismDemoClass(); obj.add(20, 30); } }
What is the output of this program? public class PolymorphismDemoClass { void add(int x, int y){ System.out.println("1. Addition is: " +(x+y)); } void add(int y, int x){ System.out.println("2. Addition is: " +(x+y)); } public static void main(String[] args){ PolymorphismDemoClass obj = new PolymorphismDemoClass(); obj.add(20, 30); } }
A"2. Addition is: 50" छपेगाPrints 2. Addition is: 50
B"1. Addition is: 50" छपेगाPrints 1. Addition is: 50
Cकम्पाइल टाइम एररCompile time error
Dरनटाइम एररRuntime error
सही उत्तर / Answer: (C)
हल / Solution
देखने में लगता है कि दोनों add अलग हैं, क्योंकि पैरामीटर के नाम उलट दिए गए हैं। पर ओवरलोडिंग में नाम नहीं गिने जाते, केवल पैरामीटर की संख्या, प्रकार और क्रम गिने जाते हैं। दोनों method का signature एक ही है — दो int। इसलिए compiler इसे दोहराई हुई method मानकर वहीं रोक देता है और कोई आउटपुट बनता ही नहीं। यानी उत्तर कम्पाइल टाइम एरर है। रनटाइम एरर तब आता जब प्रोग्राम कम से कम चलने लायक बन जाता।
The two add methods seem different, because the names of the parameters have been swapped. But overloading never counts parameter names; it counts only their number, their types and their order. Both methods here have the same signature, two int values, so the compiler treats the second one as a duplicate method and stops right there. No output is ever produced, so the answer is a compile time error. A runtime error would need the program to compile first.
प्रश्न 5उत्तर + हल
क्या हम जावा में main() विधि को ओवरलोड कर सकते हैं?
Can we overload the main() method in Java?
Aनहीं, main() मेथड को ओवरलोड नहीं किया जा सकता।No, the main() method cannot be overloaded.
Bहाँ, main() मेथड को ओवरलोड किया जा सकता है।Yes, the main() method can be overloaded.
Cओवरलोडिंग के लिए केवल main() का return type बदला जा सकता है।Only the return type of the main() method can be changed for overloading.
Dओवरलोडिंग जावा की मेथड पर लागू होने वाली अवधारणा नहीं है।Overloading is not a concept applicable to Java methods.
सही उत्तर / Answer: (B)
हल / Solution
main() भी आख़िर एक साधारण method ही है, इसलिए उसे ओवरलोड किया जा सकता है। एक ही class में अलग-अलग पैरामीटर वाली कई main लिखी जा सकती हैं। पर याद रखिए, प्रोग्राम शुरू करते समय JVM हमेशा वही main ढूँढ़ती है जिसका पैरामीटर String का array हो; बाक़ी रूपों को आपको उसी के भीतर से बुलाना पड़ेगा। तीसरा विकल्प ग़लत है क्योंकि केवल return type बदलने से ओवरलोडिंग होती ही नहीं, और चौथा विकल्प तो सीधे-सीधे ग़लत है।
The main() method is an ordinary method in the end, so it can be overloaded. A class may hold several versions of main with different parameter lists. Remember, though, that while starting the program the JVM always looks for the version whose parameter is an array of String; the other versions have to be called from inside it. The third option is wrong because changing only the return type never creates overloading, and the fourth option is plainly false.
प्रश्न 6उत्तर + हल
ओवरराइड की गई विधि में सुपरक्लास की विधि को कॉल करने के लिए किस कीवर्ड का उपयोग किया जाता है?
Which keyword is used to call the superclass method in the overridden method?
Athis
Bsuper
Cextends
Dinstanceof
सही उत्तर / Answer: (B)
हल / Solution
सबक्लass में method ओवरराइड करने के बाद भी कई बार सुपरक्लास वाला पुराना काम चाहिए होता है। तब super कीवर्ड लगाकर उसे बुलाते हैं, जैसे super.move(); इससे पुराना बरताव भी चलता है और नया भी जुड़ जाता है। this अपनी ही वस्तु की ओर इशारा करता है, इसलिए उससे वही ओवरराइड वाली method दोबारा चलेगी और अनंत चक्र बन सकता है। extends केवल घोषणा में वंशानुक्रम जोड़ता है और instanceof सिर्फ़ प्रकार जाँचता है।
Even after a subclass overrides a method, the work of the superclass version is often still needed. The super keyword calls it, as in super.move(), so the old behaviour runs first and the new behaviour is added on top. The keyword this points to the current object, so it would call the overriding method again and may build an endless loop. The keyword extends only sets up inheritance in the declaration, and instanceof merely checks the type of an object.
प्रश्न 7उत्तर + हल
बहुरूपता में, सुपरक्लास का एक संदर्भ चर (reference variable) किन वर्गों की वस्तु को संदर्भित कर सकता है?
In polymorphism, a reference variable of the superclass can refer to the object of which classes?
Aकेवल सुपरक्लास कीOnly the superclass
Bकेवल सबक्लास कीOnly the subclass
Cकिसी भी क्लास कीAny class
Dसुपरक्लास या उसकी किसी भी सबक्लास कीThe superclass or any of its subclasses
सही उत्तर / Answer: (D)
हल / Solution
जावा में ऊपर की ओर casting अपने आप हो जाती है, इसलिए सुपरक्लास का संदर्भ चर अपनी class का object भी रख सकता है और अपनी किसी भी सबक्लास का भी। यही बात रनटाइम बहुरूपता को संभव बनाती है। पहला और दूसरा विकल्प आधा सच कहते हैं, क्योंकि दोनों में से एक हालत छूट जाती है। तीसरा विकल्प ज़्यादा कह देता है — जिस class का इस पदानुक्रम से नाता नहीं, उसका object रखने पर compiler रोक देता है।
In Java an upcast happens automatically, so a superclass reference variable may hold an object of its own class as well as an object of any of its subclasses. This is exactly what makes runtime polymorphism possible. The first two options are only half true, since each of them leaves out one of these cases. The third option claims too much, because an object of a class outside this hierarchy is rejected by the compiler.
प्रश्न 8उत्तर + हल
किस परिदृश्य में जावा बहुरूपता (polymorphism) की अनुमति नहीं देता है?
In which scenario does Java NOT allow polymorphism?
Aजब private मेथड को ओवरराइड किया जाएWhen overriding private methods
Bजब मेथड ओवरलोडिंग का उपयोग होWhen using the method overloading
Cजब interface की मेथड का उपयोग होWhen using interface methods
Dजब abstract मेथड का उपयोग होWhen using abstract methods
सही उत्तर / Answer: (A)
हल / Solution
private मेथड केवल अपनी class के भीतर दिखती है, सबक्लास तक पहुँचती ही नहीं। इसलिए सबक्लास में वही नाम लिखने पर वह ओवरराइड नहीं होती, एक नई अलग मेथड बन जाती है और चुनाव compile time पर हो जाता है। यानी वहाँ रनटाइम बहुरूपता बनती ही नहीं। बाक़ी तीनों में बहुरूपता चलती है — ओवरलोडिंग संकलन-समय वाली देती है, और interface तथा abstract मेथड तो बनी ही इसलिए हैं कि उन्हें लागू करने वाली class अपने ढंग से काम करे।
A private method is visible only inside its own class and never reaches a subclass. So writing the same name in a subclass does not override it; a fresh separate method is created and the call is settled at compile time. Runtime polymorphism therefore cannot happen there. The other three cases do allow polymorphism, because overloading gives the compile-time kind, while interface methods and abstract methods exist exactly so that each implementing class can supply its own behaviour.
प्रश्न 9उत्तर + हल
इनमें से कौन संकलन-समय बहुरूपता (compile-time polymorphism) की अनुमति देता है?
Which of these allows compile-time polymorphism?
Aमेथड ओवरलोडिंगMethod overloading
Bमेथड ओवरराइडिंगMethod overriding
CदोनोंBoth
Dइनमें से कोई नहींNone of the above
सही उत्तर / Answer: (A)
हल / Solution
संकलन-समय बहुरूपता का मतलब है कि कौन सी method चलेगी यह compiler ही तय कर दे। ओवरलोडिंग में एक ही नाम की कई method होती हैं और call में दिए तर्कों की संख्या तथा प्रकार देखकर compiler सही method जोड़ देता है, इसलिए वही इसका उदाहरण है। ओवरराइडिंग में यह फ़ैसला चलते समय object के असली प्रकार से होता है, इसलिए वह रनटाइम बहुरूपता है। दूसरा विकल्प ग़लत है, इसलिए दोनों वाला विकल्प भी टिकता नहीं।
Compile-time polymorphism means that the compiler itself settles which method will run. In overloading there are several methods with one name, and the compiler joins the call to the right one by reading the number and the types of the arguments, so overloading is the example of it. In overriding the decision is taken while the program runs, so that is runtime polymorphism. Since the second option is wrong, both and none cannot stand.
प्रश्न 10उत्तर + हल
जावा में, कौन सी अवधारणा हमें रनटाइम बहुरूपता (runtime polymorphism) को लागू करने की अनुमति देती है?
In Java, which concept allows us to implement runtime polymorphism?
Aमेथड ओवरलोडिंगMethod overloading
Bमेथड ओवरराइडिंगMethod overriding
Cकंस्ट्रक्टरConstructors
Dस्टैटिक मेथडStatic methods
सही उत्तर / Answer: (B)
हल / Solution
रनटाइम बहुरूपता तब बनती है जब सबक्लास सुपरक्लास की method को उसी नाम और उसी signature के साथ दोबारा लिखे। यही मेथड ओवरराइडिंग है। सुपरक्लास के संदर्भ में सबक्लास का object रखने पर JVM चलते समय object देखकर सही method चुनती है। ओवरलोडिंग का फ़ैसला compiler पहले ही कर देता है, इसलिए वह compile time polymorphism है। कंस्ट्रक्टर वंशानुक्रम में उतरते नहीं, और स्टैटिक मेथड ओवरराइड नहीं होतीं, वे सिर्फ़ छिपती हैं।
Runtime polymorphism appears when a subclass writes a method of its superclass again with the same name and the same signature, which is method overriding. If a subclass object is kept in a superclass reference, the JVM looks at the object while the program runs and picks the right method. Overloading is settled by the compiler beforehand, so it is compile time polymorphism. Constructors are not inherited, and static methods are never overridden, they are only hidden.
आगे के 20 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
निम्नलिखित में से कौन सा मेम्बर फ़ंक्शन ओवरराइडिंग का सबसे अच्छा वर्णन करता है?
Which of the following best describes member function overriding?
Aऐसे मेम्बर फ़ंक्शन जिनका नाम केवल derived class में एक जैसा होMember functions having the same name in derived class only
Bऐसे मेम्बर फ़ंक्शन जिनका नाम एक जैसा और signature अलग हो, main फ़ंक्शन के भीतरMember functions having the same name and different signature inside main function
Cऐसे मेम्बर फ़ंक्शन जिनका नाम base और derived दोनों class में एक जैसा होMember functions having the same name in base and derived classes
Dऐसे मेम्बर फ़ंक्शन जिनका नाम केवल base class में एक जैसा होMember functions having the same name in base class only
Aकिसी मेथड को दूसरी मेथड से कॉल करना।Calling a method from another method.
Bरनटाइम पर मेथड का नाम बदल देना।Renaming a method at runtime.
Cएक ही क्लास में एक ही नाम की कई मेथड रखना पर उनके पैरामीटर अलग-अलग होना।Having multiple methods with the same name but different parameters in a class.
Dसबक्लास में सुपरक्लास की मेथड को ओवरराइड करना।Overriding a superclass method in a subclass.
निम्नलिखित जावा कोड में, sum() विधि के लिए कौन सी कॉल उपयुक्त है? class Output { public static int sum(int ...x) { return 0; } static void main(String args[]) { sum(10); sum(10,20); sum(10,20,30); sum(10,20,30,40); } }
In the following Java code, which call to the sum() method is appropriate? class Output { public static int sum(int ...x) { return 0; } static void main(String args[]) { sum(10); sum(10,20); sum(10,20,30); sum(10,20,30,40); } }
Aकेवल sum(10)only sum(10)
Bकेवल sum(10,20)only sum(10,20)
Cकेवल sum(10) और sum(10,20)only sum(10) & sum(10,20)
निम्नलिखित जावा प्रोग्राम का आउटपुट क्या है? class Parent { String name = "parent"; String message() { return "from parent"; } } class Child extends Parent { String name = "child"; String message() { return "from child"; } } public class Main { public static void main(String[] args) { Parent p = new Child(); System.out.println(p.name + " " + p.message()); } }
What is the output of the following Java program? class Parent { String name = "parent"; String message() { return "from parent"; } } class Child extends Parent { String name = "child"; String message() { return "from child"; } } public class Main { public static void main(String[] args) { Parent p = new Child(); System.out.println(p.name + " " + p.message()); } }
निम्नलिखित जावा प्रोग्राम का आउटपुट क्या है? public class Vehicle { public void move() { System.out.println("The vehicle moves"); } } public class Car extends Vehicle { public void move() { System.out.println("The car moves"); } } public class Main { public static void main(String[] args) { Vehicle vehicle = new Car(); vehicle.move(); } }
What is the output of the following Java program? public class Vehicle { public void move() { System.out.println("The vehicle moves"); } } public class Car extends Vehicle { public void move() { System.out.println("The car moves"); } } public class Main { public static void main(String[] args) { Vehicle vehicle = new Car(); vehicle.move(); } }
जावा में, क्या स्थैतिक (static) विधि को ओवरराइड करना संभव है?
In Java, is it possible to override a static method?
Aहाँ, static मेथड को भी instance मेथड की तरह ओवरराइड किया जा सकता है।Yes, we can override a static method just like we do with instance methods.
Bनहीं, static मेथड ओवरराइड नहीं हो सकती क्योंकि वे class की होती हैं, object की नहीं।No, static methods cannot be overridden because they belong to the class, not the object.
Cयह इस बात पर निर्भर करता है कि static मेथड final घोषित है या नहीं।It depends on whether the static method is declared as final or not.
Dयह static मेथड के access modifier पर निर्भर करता है।It depends on the access modifier of the static method.
Aएक ही नाम की कई मेथड, पर signature अलग और पैरामीटर की संख्या या प्रकार अलगMore than one method with same name but different method signature and different number or type of parameters
Bएक ही नाम की कई मेथड, पर पैरामीटर की संख्या अलगMore than one method with same name but different number of parameters
Cएक ही नाम की कई मेथड, पैरामीटर की संख्या समान पर प्रकार अलगMore than one method with same name, same number of parameters but different type of parameters
Dएक ही नाम की कई मेथड, पैरामीटर की संख्या और प्रकार समान पर signature अलगMore than one method with same name, same number of parameters and type but different signature