पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 20 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
नीचे दिया गया इंटरफ़ेस देखिए:
public interface Drawable {
default void draw() {
System.out.println("Drawing...");
}
} इस इंटरफ़ेस में draw() विधि का उद्देश्य क्या है?
Consider the following interface:
public interface Drawable {
default void draw() {
System.out.println("Drawing...");
}
}
What is the purpose of the draw() method in this interface?
Adraw() विधि के लिए एक पूर्व-निर्धारित (default) कार्यान्वयन देना।To define a default implementation for the draw() method.
Bयह पक्का करना कि इंटरफ़ेस लागू करने वाली हर क्लास draw() को ओवरराइड करे।To enforce that all classes implementing the interface must override the draw() method.
Cकिसी भी क्लास को Drawable इंटरफ़ेस लागू करने से रोकना।To prevent any class from implementing the Drawable interface.
Dयह बताना कि draw() विधि को ओवरराइड नहीं किया जा सकता।To specify that the draw() method cannot be overridden by implementing classes.
सही उत्तर / Answer: (A)
हल / Solution
जावा 8 से इंटरफ़ेस में default कीवर्ड लगाकर शरीर वाली विधि लिखी जा सकती है। उसका मक़सद यही है कि लागू करने वाली क्लास अगर draw() न लिखे तो भी उसे यह तैयार कार्यान्वयन मिल जाए — यानी पुराने कोड को तोड़े बिना इंटरफ़ेस में नई विधि जोड़ी जा सके। इसलिए पहला विकल्प सही है। default विधि को ओवरराइड करना ज़रूरी नहीं, पर चाहें तो किया जा सकता है; इसलिए दूसरा और चौथा ग़लत हैं।
Since Java 8 an interface may hold a method with a body if it is marked default. The purpose is that a class which implements the interface still gets this ready made version even when it does not write draw() itself, so a new method can be added to an interface without breaking old code. That is option 1. Overriding a default method is allowed but never required, so options 2 and 4 are wrong.
प्रश्न 2उत्तर + हल
नीचे दिया गया इंटरफ़ेस (interface) देखिए:
public interface Printable {
void print();
} इस इंटरफ़ेस के बारे में निम्नलिखित में से कौन कथन सही है?
Consider the following interface:
public interface Printable {
void print();
}
Which of the following statements is true about this interface?
Aइंटरफ़ेस Printable में print() एक मूर्त (concrete) विधि है।The interface Printable contains a concrete method print().
Bइंटरफ़ेस Printable की सभी विधियाँ अपने आप public होती हैं।All methods in the interface Printable are automatically public.
Cइंटरफ़ेस Printable को कोई क्लास लागू (implement) नहीं कर सकती।The interface Printable cannot be implemented by any class.
Dइंटरफ़ेस Printable में कंस्ट्रक्टर होना ज़रूरी है।The interface Printable must contain a constructor.
सही उत्तर / Answer: (B)
हल / Solution
इंटरफ़ेस में घोषित हर विधि अपने आप public और abstract मानी जाती है, चाहे ये शब्द लिखे ही न जाएँ। इसलिए print() public है — यही दूसरा विकल्प कहता है। पहला विकल्प ग़लत है, क्योंकि print() का शरीर ही नहीं है, वह मूर्त (concrete) नहीं अमूर्त है। तीसरा भी ग़लत है; कोई भी क्लास implements लिखकर इसे लागू कर सकती है। चौथा ग़लत है, क्योंकि इंटरफ़ेस का कंस्ट्रक्टर होता ही नहीं।
Every method declared in an interface is implicitly public and abstract, even when those words are not written. So print() is public, which is exactly what option 2 states. Option 1 is wrong because print() has no body at all, so it is abstract and not concrete. Option 3 is wrong since any class may adopt the interface with implements. Option 4 is wrong too, because an interface can never have a constructor.
प्रश्न 3उत्तर + हल
जावा में निम्नलिखित में से कौन सी interface घोषणा मान्य है?
Which of the following is a valid interface declaration in Java?
Apublic interface MyInterface {
public void myMethod();
}
Binterface MyInterface {
void myMethod() {}
}
Cinterface MyInterface {
public void myMethod() {}
}
Dpublic interface MyInterface {
int count;
void myMethod();
}
सही उत्तर / Answer: (A)
हल / Solution
interface में लिखी सामान्य method केवल घोषित होती है — उसके आगे अर्धविराम आता है, घुँघराले कोष्ठक नहीं। पहला विकल्प यही करता है, इसलिए वह मान्य है। दूसरे और तीसरे विकल्प में method के आगे body लगा दी गई है, जो default या static लिखे बिना मान्य नहीं। चौथे विकल्प में count घोषित तो है पर उसे कोई मान नहीं दिया गया, जबकि interface का हर चर अपने आप final होता है और उसे वहीं भरना पड़ता है।
A normal method inside an interface is only declared, so a semicolon follows it rather than a pair of braces. The first option does exactly that and is therefore valid. The second and third options attach a body to the method, which is not allowed unless the method is marked default or static. In the fourth, count is declared but given no value, although every interface field is implicitly final and must be assigned right there.
प्रश्न 4उत्तर + हल
क्या जावा में एक interface दूसरे interface को extend कर सकता है?
Can an interface extend another interface in Java?
Aनहीं, interface दूसरे interface को extend नहीं कर सकताNo, interfaces cannot extend other interfaces.
Bहाँ, पर interface केवल abstract class को extend कर सकता हैYes, but interfaces can only extend abstract classes.
Cहाँ, interface दूसरे interface को extends keyword से extend कर सकता हैYes, interfaces can extend other interfaces using the extends keyword.
Dहाँ, पर interface केवल class को extend कर सकता हैYes, but interfaces can only extend classes.
सही उत्तर / Answer: (C)
हल / Solution
interface दूसरे interface को extends लगाकर आगे बढ़ा सकता है, और चाहे तो एक साथ कई interface को भी। ऐसा करने पर ऊपर वाले interface की सारी घोषित method नीचे वाले में जुड़ जाती हैं, और उसे implement करने वाली class को वे सब लिखनी पड़ती हैं। interface किसी class को, चाहे वह abstract हो या साधारण, कभी extend नहीं करता। class और interface का रिश्ता implements से बनता है।
An interface can carry another interface forward with extends, and it may even extend several of them at once. When that happens, every method declared in the upper interface joins the lower one, and the class that implements it must write them all. An interface never extends a class, whether that class is abstract or plain. The link between a class and an interface is made with implements.
प्रश्न 5उत्तर + हल
जावा में interface को implement करने का सही वाक्य-विन्यास इनमें से कौन सा है?
Which of the following is a correct syntax to implement an interface in Java?
Aclass MyClass extends MyInterface {}
Bclass MyClass implements MyInterface {}
Cinterface MyClass implements MyInterface {}
Dinterface MyClass extends MyInterface {}
सही उत्तर / Answer: (B)
हल / Solution
class किसी interface से जुड़ने के लिए implements लिखती है और किसी दूसरी class से जुड़ने के लिए extends। इसीलिए दूसरा विकल्प ही सही है। पहला विकल्प class के साथ extends लगाकर interface से जोड़ रहा है, जो मान्य नहीं। तीसरे विकल्प में interface किसी दूसरे interface को implement कर रहा है, जबकि interface सिर्फ़ extends कर सकता है। चौथा विकल्प interface से interface जोड़ता है, पर सवाल class के बारे में पूछा गया था।
A class uses implements to join an interface and extends to join another class. That is why the second option is correct. The first option tries to link an interface through extends on a class, which is not allowed. In the third, an interface is implementing another interface, although an interface may only extend one. The fourth does link interface to interface, but the question was asked about a class.
प्रश्न 6उत्तर + हल
जावा में किसी interface को implement करने का उद्देश्य क्या है?
What is the purpose of implementing an interface in Java?
Ainterface से instance चर विरासत में लेनाTo inherit instance variables from the interface.
Binterface की सभी method का ठोस कार्यान्वयन देनाTo provide concrete implementations for all methods in the interface.
Cऐसी method घोषित करना जिन्हें implement करने वाली class को लिखना ही पड़ेTo declare methods that must be implemented by the implementing class.
Dimplement करने वाली class की कुछ method तक पहुँच रोकनाTo restrict access to certain methods in the implementing class.
सही उत्तर / Answer: (C)
हल / Solution
interface एक अनुबंध तय करता है। उसे implement करते ही class यह वादा कर लेती है कि उसमें घोषित सारी method उसके पास मौजूद रहेंगी। यही इस अनुबंध का असली मक़सद है। interface में instance चर होते ही नहीं, वहाँ सब स्थिरांक हैं, इसलिए पहला विकल्प ग़लत है। जावा 8 की default method को दोबारा लिखना ज़रूरी नहीं, इसलिए दूसरा भी पूरा सच नहीं। पहुँच रोकना interface का काम है ही नहीं।
An interface sets down a contract. The moment a class implements it, the class promises that every method declared there will be present in it. That promise is the real purpose. An interface holds no instance variables, only constants, so the first option fails. A default method from Java 8 need not be rewritten, so the second is not wholly true either, and blocking access is simply not the job of an interface.
प्रश्न 7उत्तर + हल
क्या जावा में interface के भीतर ठोस (concrete) method रखी जा सकती हैं?
Can interfaces contain concrete methods in Java?
Aहाँ, पर interface की सभी method ठोस ही होनी चाहिएYes, but all methods in an interface must be concrete.
Bनहीं, interface में केवल abstract method रह सकती हैंNo, interfaces can only contain abstract methods.
Cहाँ, interface में abstract और ठोस दोनों तरह की method रखी जा सकती हैंYes, interfaces can contain both abstract and concrete methods.
Dनहीं, interface में कोई method रखी ही नहीं जा सकतीNo, interfaces cannot contain any methods.
सही उत्तर / Answer: (C)
हल / Solution
जावा 8 से पहले interface में केवल बिना body वाली method रहती थीं। जावा 8 के बाद वहाँ default और static method जोड़ी जा सकती हैं, जिनकी पूरी body लिखी होती है। इससे पुरानी interface में नया काम जोड़ना आसान हो गया, क्योंकि उसे implement करने वाली पुरानी class टूटती नहीं। इसलिए आज एक ही interface में abstract और ठोस दोनों तरह की method रह सकती हैं, और बाक़ी तीनों विकल्प ग़लत हैं।
Before Java 8 an interface could only hold methods without a body. From Java 8 onward you may add default and static methods there, and these carry a full body. This made it easy to add new work to an old interface, because the older classes that implement it do not break. So today one interface can hold both abstract and concrete methods, which rules out the other three options.
प्रश्न 8उत्तर + हल
जावा में interface के बारे में निम्नलिखित में से कौन सा कथन सही है?
Which of the following statements about interfaces in Java is true?
Ainterface में instance चर रखे जा सकते हैंInterfaces can contain instance variables.
Binterface का object new keyword से बनाया जा सकता हैInterfaces can be instantiated using the new keyword.
Cएक class एक से ज़्यादा interface implement कर सकती हैClasses can implement multiple interfaces.
Dinterface एक से ज़्यादा class को extend कर सकता हैInterfaces can extend multiple classes.
सही उत्तर / Answer: (C)
हल / Solution
जावा में एक class सिर्फ़ एक ही class को extend कर सकती है, पर interface जितने चाहे उतने implement कर सकती है। इसी रास्ते से जावा में बहु-उत्तराधिकार का लाभ मिल जाता है। interface में लिखे चर अपने आप public static final हो जाते हैं, इसलिए वे instance चर रहते ही नहीं। interface अधूरा होता है, इसलिए उसका object सीधे नहीं बनता। और interface सिर्फ़ दूसरे interface को extend करता है, class को कभी नहीं।
In Java a class can extend only one class, but it may implement as many interfaces as it likes. This is the route through which Java gains the benefit of multiple inheritance. Any variable written inside an interface automatically becomes public static final, so it never stays an instance variable. An interface is incomplete, so no object of it is made directly, and an interface extends only other interfaces, never a class.
प्रश्न 9उत्तर + हल
जावा में interface क्या होता है?
What is an interface in Java?
Aऐसी class जिसका object सीधे नहीं बनता और जो केवल दूसरी class के लिए आधार class का काम करती हैA class that cannot be instantiated directly and can only be used as a base class for other classes.
Bएक विशेष प्रकार की class जिसमें केवल abstract method और स्थिरांक (constants) होते हैंA special type of class that contains only abstract methods and constants.
Cऐसी class जो दूसरी class से मिली method का ठोस कार्यान्वयन देती हैA class that provides concrete implementations for methods inherited from other classes.
Dजावा में loop के लिए प्रयोग होने वाला keywordA keyword used for looping in Java.
सही उत्तर / Answer: (B)
हल / Solution
interface एक अनुबंध जैसा है। उसमें method के सिर्फ़ हस्ताक्षर लिखे जाते हैं और उसके चर अपने आप public static final यानी स्थिरांक बन जाते हैं। जो class उसे implement करती है वही उन method का असली काम लिखती है। पहला विकल्प abstract class का वर्णन है, interface का नहीं। तीसरा विकल्प उल्टा है, क्योंकि ठोस कार्यान्वयन implement करने वाली class देती है। loop के लिए for और while होते हैं।
An interface works like a contract. Only the signatures of its methods are written, and any variable inside it automatically becomes public static final, that is a constant. The class that implements it is the one that writes the real work of those methods. The first option describes an abstract class, not an interface. The third is the reverse, since the implementing class supplies the concrete code. Loops use for and while.
प्रश्न 10उत्तर + हल
नीचे दी गई abstract class देखिए:
public abstract class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public abstract void work();
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
} इस abstract class के बारे में निम्नलिखित में से कौन सा कथन सही है?
Consider the following abstract class:
public abstract class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public abstract void work();
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
} Which of the following statements about this abstract class is correct?
AEmployee class में constructor नहीं हो सकताThe Employee class cannot have a constructor.
BEmployee की सभी ठोस subclass को work() method का कार्यान्वयन देना पड़ेगाAll concrete subclasses of Employee must provide implementations for the work() method.
Cwork() method को subclass में override नहीं किया जा सकताThe work() method cannot be overridden in a subclass.
DEmployee class में कोई instance चर नहीं हो सकताThe Employee class cannot have any instance variables.
सही उत्तर / Answer: (B)
हल / Solution
work() को abstract घोषित किया गया है, इसलिए Employee उसका काम नहीं बताती। यह ज़िम्मेदारी उसकी हर ठोस subclass पर आती है, उसे work() लिखनी ही पड़ेगी। कोड ख़ुद बाक़ी कथन काट देता है — Employee का अपना constructor मौजूद है जो name और salary भरता है, और ये दोनों instance चर हैं। abstract method का पूरा मक़सद ही override होना है, इसलिए तीसरा कथन भी ग़लत है।
The method work() is declared abstract, so Employee does not say what it should do. That duty falls on each of its concrete subclasses, which must write work() themselves. The code itself cuts down the rest: Employee does have its own constructor that fills name and salary, and both of these are instance variables. The whole point of an abstract method is to be overridden, so the third statement fails as well.
आगे के 20 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
abstract class के बारे में निम्नलिखित में से कौन सा कथन ग़लत है?
Which of the following statements about abstract classes is false?
Aabstract class का उपयोग subclass के लिए साझा व्यवहार तय करने में किया जा सकता हैAbstract classes can be used to define common behavior for subclasses.
Babstract class को दूसरी class extend नहीं कर सकतीAbstract classes cannot be extended by other classes.
Cabstract class में abstract और ठोस दोनों तरह की method हो सकती हैंAbstract classes can contain both abstract and concrete methods.
Dabstract class में constructor और instance चर हो सकते हैंAbstract classes can have constructors and instance variables.
जावा में abstract class का object बनाने के बारे में निम्नलिखित में से कौन सा कथन सही है?
Which of the following statements is true regarding the instantiation of abstract classes in Java?
Aabstract class का object सीधे नहीं बनता, पर उसकी ठोस subclass का object बनाया जा सकता हैAbstract classes cannot be instantiated directly, but their concrete subclasses can be instantiated.
Babstract class का object new keyword से सीधे बनाया जा सकता हैAbstract classes can be instantiated directly using the new keyword.
Cabstract class का object केवल AbstractClass.getInstance() नाम की विशेष method से बनता हैAbstract classes can only be instantiated using a special AbstractClass.getInstance() method.
Dabstract class का object abstract keyword से बनाया जा सकता हैAbstract classes can be instantiated using the abstract keyword.
क्या जावा में abstract class किसी interface को implement कर सकती है?
Can an abstract class implement interfaces in Java?
Aनहीं, abstract class interface को implement नहीं कर सकतीNo, abstract classes cannot implement interfaces.
Bहाँ, कर सकती है, पर उसे interface की सभी method लिखना ज़रूरी नहींYes, abstract classes can implement interfaces, but they are not required to implement the interface methods.
Cहाँ, कर सकती है, और उसे interface की सभी method का कार्यान्वयन देना ही पड़ेगाYes, abstract classes can implement interfaces, and they must provide implementations for all interface methods.
Dहाँ, पर तभी जब वह final घोषित होYes, abstract classes can implement interfaces, but only if they are declared as final.
ARectangle नाम की एक method परिभाषित करना जो आयत का क्षेत्रफल निकालती हैTo define a method named Rectangle that calculates the area of a rectangle.
BRectangle नाम की एक class परिभाषित करना जिसमें private चर length और width हैं तथा getArea नाम की method क्षेत्रफल निकालती हैTo define a class named Rectangle with private attributes length and width, and a method getArea that calculates the area of a rectangle.
CRectangle नाम का एक constructor परिभाषित करना जो आयत की लंबाई और चौड़ाई तय करता हैTo define a constructor named Rectangle that initializes the length and width of a rectangle.
DgetArea नाम की एक method परिभाषित करना जो आयत का क्षेत्रफल निकालती हैTo define a method named getArea that calculates the area of a rectangle.
जावा में class के बारे में निम्नलिखित में से कौन सा कथन सही है?
Which of the following statements about classes in Java is true?
Aएक class में या तो data हो सकता है या method, दोनों नहींA class can only contain either data or methods, but not both.
Bclass का object सीधे नहीं बनता; object बनाने के लिए उसका उपयोग किया जाता हैA class cannot be instantiated directly; it needs to be used to create objects.
Cclass में constructor नहीं हो सकताA class cannot have constructors.
Dजावा में class एक आदिम (primitive) data type हैA class is a primitive data type in Java.