पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 20 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
निम्नलिखित में से कौन वंशानुक्रम (inheritance) का सबसे अच्छा वर्णन करता है?
Which among the following best describes Inheritance?
Aआधार क्लास के डेटा और function को व्युत्पन्न भाग (derived segment) में इस्तेमाल करनाUsing the data and functions into derived segment
Bकिसी programming language में पहले से बने function इस्तेमाल करनाUsing already defined functions in a programming language
Cएक बार लिखे हुए कोड को इस्तेमाल करनाUsing the code already written once
Dपहले लिखे हुए कोड की नक़ल कर लेनाCopying the code already written
सही उत्तर / Answer: (A)
हल / Solution
वंशानुक्रम (inheritance) का मतलब है आधार क्लास (base class) के डेटा और function को व्युत्पन्न क्लास में आगे बढ़ाकर इस्तेमाल करना — इसलिए पहला विकल्प सबसे सटीक है। पहले से बने function बुलाना तो library का साधारण उपयोग है, उसमें क्लास के बीच कोई नया संबंध नहीं बनता। एक बार लिखा कोड इस्तेमाल करना बहुत ढीली बात है, वह हर method बुलाने पर भी लागू होगी। नक़ल करना तो उलटा है, क्योंकि वहाँ कोड दो जगह हो जाता है।
Inheritance means the data and functions of a base class are carried into a derived class and used there, so the first option describes it best. Calling functions that already exist is just ordinary library use and creates no new relationship between classes. Saying you use code written once is far too loose, since that is true of any method call. Copying code is the opposite, because the code then lives in two places.
प्रश्न 2उत्तर + हल
आधार क्लास (base class) के किस प्रकार के सदस्य उसकी व्युत्पन्न क्लास (derived class) में पहुँच से बाहर रहते हैं?
Which type of members can't be accessed in the derived classes of a base class?
Aसभी तक पहुँचा जा सकता हैAll can be accessed
BprotectedProtected
CprivatePrivate
DpublicPublic
सही उत्तर / Answer: (C)
हल / Solution
private सदस्य सिर्फ़ उसी क्लास के भीतर दिखते हैं जिसमें वे लिखे गए हैं। उप-क्लास (subclass) उन्हें सीधे छू नहीं सकती, इसलिए तीसरा विकल्प सही है। वे वस्तु में मौजूद तो रहते हैं, पर उन तक पहुँचने का रास्ता आधार क्लास के public या protected method ही देते हैं। protected सदस्य व्युत्पन्न क्लास में मिलते हैं, यही उनका मक़सद है, और public सदस्य हर जगह मिलते हैं। इसलिए पहला विकल्प भी ग़लत है।
A private member is visible only inside the class where it is written. A subclass cannot touch it directly, so the third option is correct. The member still exists inside the object; the only way to reach it is through a public or protected method of the base class. protected members are reachable in a derived class, which is exactly their purpose, and public members are reachable everywhere, so the first option is wrong.
प्रश्न 3उत्तर + हल
OOP की कौन सी विशेषता नेस्टेड क्लास (nested class) के उपयोग को घटाती है?
Which feature of OOP reduces the use of nested classes?
Aवंशानुक्रम (Inheritance)Inheritance
Bबाइंडिंग (Binding)Binding
Cएब्स्ट्रैक्शन (Abstraction)Abstraction
Dएनकैप्सुलेशन (Encapsulation)Encapsulation
सही उत्तर / Answer: (A)
हल / Solution
वंशानुक्रम (inheritance) में साझा कोड आधार क्लास (base class) में रख दिया जाता है और नई क्लास उसे बढ़ाकर काम चला लेती है। इसलिए एक क्लास के भीतर दूसरी क्लास, यानी नेस्टेड क्लास लिखने की ज़रूरत घट जाती है। बाइंडिंग (binding) तो method के बुलावे को उसके कोड से जोड़ने की प्रक्रिया है। एब्स्ट्रैक्शन (abstraction) ब्योरा छिपाता है और एनकैप्सुलेशन (encapsulation) डेटा को बाँधता है; दोनों का क्लास के ढाँचे से यह नाता नहीं है।
With inheritance the shared code is kept in a base class and a new class simply extends it, so there is far less need to write one class inside another as a nested class. Binding is only the act of linking a method call to the code that will run. Abstraction hides detail and encapsulation wraps data together with its methods; neither of them removes the need for nesting.
प्रश्न 4उत्तर + हल
OOP की कौन सी विशेषता कोड के पुनः प्रयोग (code reusability) को दर्शाती है?
Which feature of OOP indicates code reusability?
Aएब्स्ट्रैक्शन (Abstraction)Abstraction
Bपॉलीमॉर्फिज़्म (Polymorphism)Polymorphism
Cएनकैप्सुलेशन (Encapsulation)Encapsulation
Dवंशानुक्रम (Inheritance)Inheritance
सही उत्तर / Answer: (D)
हल / Solution
वंशानुक्रम (inheritance) में एक क्लास दूसरी क्लास के गुण और method अपने आप पा जाती है, इसलिए वही कोड दोबारा लिखना नहीं पड़ता — कोड का पुनः प्रयोग यहीं से आता है। एब्स्ट्रैक्शन (abstraction) सिर्फ़ ज़रूरी बात दिखाकर ब्योरा छिपाता है। पॉलीमॉर्फिज़्म (polymorphism) एक ही नाम को कई रूपों में चलाता है। एनकैप्सुलेशन (encapsulation) डेटा और method को एक इकाई में बाँधकर सुरक्षित रखता है। इसलिए चौथा विकल्प सही है।
In inheritance a class automatically receives the fields and methods of another class, so the same code need not be written again, and that is where reuse comes from. Abstraction only shows what matters and hides the details. Polymorphism lets one name behave in several forms. Encapsulation binds data and methods into one unit and keeps the data safe. So the fourth option is the right answer.
प्रश्न 5उत्तर + हल
किसी class को दूसरी class द्वारा inherit होने से रोकने के लिए किस कीवर्ड का उपयोग किया जाता है?
Which keyword is used to prevent a class from being inherited by other classes?
Ainherit
Bsealed
Cprevent
Dfinal
सही उत्तर / Answer: (D)
हल / Solution
class के आगे final लिख दीजिए, फिर कोई उसे extends नहीं कर सकता — compiler वहीं रोक देता है। String, Integer और बाक़ी wrapper class इसी तरह final रखी गई हैं ताकि उनका व्यवहार सुरक्षित रहे। sealed जावा में बहुत बाद में आया और वह रोकता नहीं, बल्कि तय करता है कि कौन-कौन extends कर सकता है। inherit तथा prevent जावा के कीवर्ड हैं ही नहीं।
Write final in front of a class and nobody can extend it; the compiler stops any such attempt. String, Integer and the other wrapper classes are kept final in just this way so that their behaviour stays safe. The word sealed arrived in Java much later and does not block inheritance at all; it names the classes that are allowed to extend. Neither inherit nor prevent is a Java keyword.
प्रश्न 6उत्तर + हल
class और interface के बारे में सही कथन है/हैं
Correct statement about a class and interface is/are
Aएक interface कई interface को extends कर सकता हैAn interface can extend multiple interfaces
Bएक class कई interface को implements कर सकती हैA class can implement multiple interfaces
Cजावा interface की मदद से multiple inheritance का समर्थन करती हैJava supports multiple inheritance using interfaces
Dउपरोक्त सभीAll of the above
सही उत्तर / Answer: (D)
हल / Solution
तीनों कथन सही हैं, इसलिए उत्तर उपरोक्त सभी है। एक interface कई interface को एक साथ extends कर सकता है, जो class के लिए संभव नहीं। एक class भी कई interface को एक साथ implements कर सकती है। इन्हीं दो छूटों के बल पर जावा में multiple inheritance का काम interface से निकल आता है, जबकि class से class पर वह रोक बनी रहती है। इसीलिए कोई एक विकल्प चुनना अधूरा होगा।
All three statements are true, so the answer is all of the above. One interface may extend several interfaces at once, which a class cannot do. A class may also implement several interfaces at the same time. These two allowances are exactly how Java obtains the effect of multiple inheritance through interfaces, while the bar on class to class inheritance stays in place. Choosing any single option would therefore be incomplete.
प्रश्न 7उत्तर + हल
जावा में extends और implements में क्या अंतर है?
What is the difference between "extends" and "implements" in Java?
Aदोनों class की inheritance के लिए काम आते हैंBoth are used for class inheritance
Bextends class की inheritance के लिए है, जबकि implements interface लागू करने के लिए है"Extends" is used for class inheritance, while "implements" is used for interface implementation
Cदोनों interface लागू करने के लिए काम आते हैंBoth are used for interface implementation
Dimplements class की inheritance के लिए है, जबकि extends interface लागू करने के लिए है"Implements" is used for class inheritance, while "extends" is used for interface implementation
सही उत्तर / Answer: (B)
हल / Solution
class को class से जोड़ने पर extends लगता है, और class जब किसी interface का काम पूरा करने का वादा करती है तब implements। इसलिए दूसरा विकल्प सही है। चौथे विकल्प में दोनों की भूमिकाएँ आपस में बदल दी गई हैं, इसलिए वह ग़लत है। पहला और तीसरा दोनों को एक ही काम का बता रहे हैं, जो सही नहीं। ध्यान रखिए, interface से interface पर फिर extends ही चलता है।
Joining a class to a class uses extends, while a class that promises to complete the work of an interface uses implements. That makes the second option correct. The fourth option swaps the two roles, so it is wrong. The first and third options claim that both words do the same job, which is not true. Note also that going from one interface to another interface uses extends again.
प्रश्न 8उत्तर + हल
निम्नलिखित प्रोग्राम का आउटपुट क्या होगा?
class First
{
static void staticMethod()
{
System.out.println("Static Method");
}
}
public class MainClass
{
public static void main(String[] args)
{
First first = null;
first.staticMethod();
}
}
What will be the output of the following program?
class First
{
static void staticMethod()
{
System.out.println("Static Method");
}
}
public class MainClass
{
public static void main(String[] args)
{
First first = null;
first.staticMethod();
}
}
AStatic Method
BNullPointerException फेंकेगाThrows a NullPointerException
Cकंपाइल टाइम एररCompile-time error
Dरनटाइम एररRuntime error
सही उत्तर / Answer: (A)
हल / Solution
staticMethod पूरी class की है, किसी object की नहीं। इसलिए reference का मान null होने पर भी उसे चलाने में कोई अड़चन नहीं आती। compiler उस पंक्ति को चुपचाप बदल देता है और कॉल class के नाम से लगती है, यानी object की ज़रूरत पड़ती ही नहीं। इसी वजह से आउटपुट में वही पंक्ति छपती है जो method के भीतर लिखी है। null से गड़बड़ तब होती है जब instance method बुलाई जाए।
The method staticMethod belongs to the whole class, not to any object. So even though the reference holds null, running it causes no trouble at all. The compiler quietly rewrites that line and makes the call through the class name, which means no object is needed. That is why the output is simply the line written inside the method. A null reference fails only when an instance method is called on it.
प्रश्न 9उत्तर + हल
निम्नलिखित जावा प्रोग्राम का आउटपुट क्या है?
class Grandparent {
public void print() {
System.out.println("Grandparent's Print()");
}
}
class Parent extends Grandparent { }
class Child extends Parent { }
public class Main {
public static void main(String[] args) {
Child child = new Child();
child.print();
}
}
What is the output of the following Java program?
class Grandparent {
public void print() {
System.out.println("Grandparent's Print()");
}
}
class Parent extends Grandparent { }
class Child extends Parent { }
public class Main {
public static void main(String[] args) {
Child child = new Child();
child.print();
}
}
A"Child's Print()"
B"Parent's Print()"
C"Grandparent's Print()"
Dकंपाइलेशन एररCompilation error
सही उत्तर / Answer: (C)
हल / Solution
यह multilevel inheritance है — Grandparent से Parent, फिर Parent से Child। Parent और Child दोनों ख़ाली हैं, किसी ने print को दोबारा नहीं लिखा। इसलिए child.print() बुलाते ही JVM ऊपर की ओर खोजता है और Grandparent वाली print चला देता है। छपने वाली पंक्ति वही है जो उस method के भीतर लिखी है। override न होने से Parent या Child के नाम कहीं नहीं छपते, और कोड पूरी तरह सही है इसलिए compile भी हो जाता है।
This is multilevel inheritance: Grandparent to Parent and then Parent to Child. Both Parent and Child are empty bodies, and neither rewrites print. So when child.print() is called, the JVM searches upward and runs the version found in Grandparent. The line that appears is exactly the one written inside that method. Since nothing is overridden, the names Parent or Child are never printed, and the code is valid so it compiles.
प्रश्न 10उत्तर + हल
क्या जावा में multiple inheritance पाने के लिए interface का उपयोग किया जा सकता है?
Can interfaces be used to achieve multiple inheritance in Java?
AहाँYes
BनहींNo
Cकुछ कहा नहीं जा सकताNothing can be said
Dइनमें से कोई नहींNone of these
सही उत्तर / Answer: (A)
हल / Solution
एक class कई interface एक साथ implements कर सकती है, इसलिए multiple inheritance का काम interface से निकल आता है। class से class वाला रास्ता बंद है, पर यह खुला रखा गया — वजह यह कि interface में सिर्फ़ method का ढाँचा होता है, शरीर class ख़ुद लिखती है। इसलिए दो जगह से एक ही नाम आने पर भी उलझन नहीं होती और diamond problem खड़ी नहीं होती।
A class may implement several interfaces at the same time, so the effect of multiple inheritance is obtained through interfaces. The class to class route is closed, but this one is deliberately left open. The reason is that an interface normally carries only the shape of a method, while the class supplies the body itself. Even if the same name arrives from two sides there is no confusion, so no diamond problem arises.
आगे के 20 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
Aयह वह प्रक्रिया है जिसमें एक class दूसरी class के field और method पा लेती हैIt's a process where one class acquires the properties (fields) and behaviors (methods) of another class
Bयह main() method की मदद से नई class बनाने की प्रक्रिया हैIt's a process of creating a new class using the main() method
Cयह जावा में object बनाने की तकनीक हैIt's a technique to create objects in Java
Dयह package आयात करने के लिए जावा का ख़ास शब्द हैIt's a Java-specific term for importing packages
यदि कोई class दो class को extends करे और दोनों में एक ही नाम तथा signature वाली method हो तो क्या होगा? मान लीजिए कि वह class उस method को ख़ुद लागू नहीं कर रही है।
What would be the result if a class extends two classes and both have a method with the same name and signature? Assume that the class is not implementing that method.
Aरनटाइम एररRuntime error
Bकंपाइल टाइम एररCompile time error
Cकोड सफलतापूर्वक चल जाएगाCode runs successfully
Dपहले वाली method चल जाएगीFirst called method is executed successfully