पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 20 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
Java में private कन्स्ट्रक्टर के बारे में निम्न कथनों में से कौन-सा सही है?
Which of the following statements is true regarding private constructors in Java?
Aprivate कन्स्ट्रक्टर को reflection से बुलाया जा सकता हैPrivate constructors can be called using reflection
Bprivate कन्स्ट्रक्टर सबक्लास से पहुँच योग्य होते हैंPrivate constructors are accessible from subclasses
Cclass के लोड होते ही private कन्स्ट्रक्टर अपने आप चल जाता हैA private constructor runs by itself as soon as the class is loaded
Dprivate कन्स्ट्रक्टर exception नहीं फेंक सकतेPrivate constructors cannot throw exceptions
सही उत्तर / Answer: (A)
हल / Solution
साधारण कोड से private कन्स्ट्रक्टर तक नहीं पहुँचा जा सकता, पर reflection उसे setAccessible देकर खोल लेती है और बुला भी लेती है। इसीलिए पहला कथन सही है। कन्स्ट्रक्टर विरासत में जाते ही नहीं, इसलिए सबक्लास तक उनकी पहुँच का सवाल नहीं उठता। कोई भी कन्स्ट्रक्टर अपने आप नहीं चलता, वह तभी चलता है जब object बनाया जाए। और exception फेंकने पर private होने से कोई रोक नहीं लगती।
Ordinary code cannot reach a private constructor, but reflection can open it with setAccessible and then call it, which is why the first statement is true. Constructors are never inherited, so a subclass has no access to them at all. No constructor runs on its own; it runs only when an object is actually created. And being private puts no limit whatsoever on throwing exceptions from inside it.
प्रश्न 2उत्तर + हल
निम्नलिखित Java class पर विचार कीजिए:
public class Singleton {
private static Singleton instance;
private Singleton() {
// Constructor implementation
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
इस class में कौन-सा डिज़ाइन पैटर्न लागू किया गया है?
Consider the following Java class:
public class Singleton {
private static Singleton instance;
private Singleton() {
// Constructor implementation
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
What design pattern is implemented in this class?
AFactory Method
BPrototype
CSingleton
DBuilder
सही उत्तर / Answer: (C)
हल / Solution
यहाँ तीन निशान एक साथ दिख रहे हैं — कन्स्ट्रक्टर private है, class के भीतर एक static reference रखा है, और getInstance पहली बार object बनाकर उसी को हर बार लौटाती रहती है। इन तीनों का जोड़ Singleton पैटर्न ही है, जिसका मक़सद पूरे प्रोग्राम में एक ही object रखना है। Factory Method कई प्रकार के object बनाने के लिए होता है, Prototype नक़ल उतारने के लिए, और Builder टुकड़ों में object खड़ा करने के लिए।
Three signs appear together here: the constructor is private, a static reference is kept inside the class, and getInstance builds the object the first time and returns that same one afterwards. Taken together these are the marks of the Singleton pattern, whose aim is to keep exactly one object alive in the whole program. Factory Method creates objects of many types, Prototype copies an existing object, and Builder assembles one step by step.
प्रश्न 3उत्तर + हल
Java में private कन्स्ट्रक्टर का निम्न में से कौन-सा प्रयोग सही बैठता है?
Which of the following is a valid use case for a private constructor in Java?
Aclass को subclass बनने से रोकनाTo restrict subclassing of a class
Bclass की कुछ मेथडों तक पहुँच रोकनाTo restrict access to certain methods of a class
Cदूसरी class से आसानी से object बनने देनाTo allow easy instantiation of objects from other classes
Dobject तैयार करने के लिए कई कन्स्ट्रक्टर देनाTo provide multiple constructors for object initialization
सही उत्तर / Answer: (A)
हल / Solution
सबक्लास के कन्स्ट्रक्टर को ऊपर वाले कन्स्ट्रक्टर तक पहुँचना ही पड़ता है। अगर class के सारे कन्स्ट्रक्टर private हों तो बाहर की कोई class उसे बढ़ा ही नहीं सकती, यानी वह अपने आप subclass बनने से रुक जाती है। पहला विकल्प यही कहता है। मेथड तक पहुँच रोकना कन्स्ट्रक्टर का काम नहीं, वह हर मेथड पर अलग से तय होता है। बाक़ी दो विकल्प private के असर से ठीक उलटी बात कहते हैं।
A subclass constructor must be able to reach a constructor of its parent. If every constructor in a class is private, no outside class can extend it, so the class stops being subclassed on its own. That is what the first option says. Blocking access to methods is not a constructor's job; each method carries its own modifier. The other two options describe the exact opposite of what private does.
प्रश्न 4उत्तर + हल
निम्नलिखित Java class पर विचार कीजिए:
public class MyClass {
private MyClass() {
// Constructor implementation
}
}
अगर किसी दूसरी class से MyClass का object बनाने की कोशिश की जाए तो क्या होगा?
Consider the following Java class:
public class MyClass {
private MyClass() {
// Constructor implementation
}
}
What will happen if you attempt to create an object of MyClass from another class?
Aकम्पाइलेशन त्रुटिCompilation error
Bरनटाइम त्रुटिRuntime error
Cobject सफलतापूर्वक बन जाएगाThe object will be created successfully
Dobject बन जाएगा, पर डिफ़ॉल्ट मानों के साथThe object will be created, but with default values
सही उत्तर / Answer: (A)
हल / Solution
इस class का इकलौता कन्स्ट्रक्टर private है, इसलिए उस तक पहुँच सिर्फ़ उसी class के भीतर है। दूसरी class में new लिखते ही compiler पहुँच का नियम टूटता देखकर वहीं रोक देता है। यह ग़लती प्रोग्राम चलने से पहले ही पकड़ी जाती है, इसलिए कम्पाइलेशन त्रुटि आती है, रनटाइम त्रुटि नहीं। और जब कोड कम्पाइल ही नहीं हुआ, तो object बनने या उसे डिफ़ॉल्ट मान मिलने का सवाल ही नहीं उठता।
The only constructor in this class is private, so it can be reached just from inside that class. The moment another class writes new, the compiler sees an access rule being broken and stops right there. The mistake is caught before the program ever runs, so it is a compilation error and not a runtime error. Since the code never compiles, no object is created, with default values or otherwise.
प्रश्न 5उत्तर + हल
private कन्स्ट्रक्टर के बारे में निम्न में से कौन-सा कथन सही है?
Which of the following statements regarding private constructors is correct?
Aprivate कन्स्ट्रक्टर में पैरामीटर हो सकते हैंPrivate constructors can have parameters
Bprivate कन्स्ट्रक्टर सबक्लास को विरासत में मिलते हैंPrivate constructors are inherited by subclasses
Cprivate कन्स्ट्रक्टर को abstract घोषित किया जा सकता हैPrivate constructors can be declared abstract
Dprivate कन्स्ट्रक्टर सिर्फ़ static वेरिएबल में मान भर सकते हैंPrivate constructors can only initialize static variables
सही उत्तर / Answer: (A)
हल / Solution
private होना सिर्फ़ पहुँच की बात है, बनावट की नहीं। इसलिए ऐसा कन्स्ट्रक्टर पैरामीटर भी ले सकता है और उन्हें फ़ील्ड में भर भी सकता है, इसलिए पहला कथन सही है। कन्स्ट्रक्टर विरासत में जाते ही नहीं, इसलिए दूसरा ग़लत है। abstract का मतलब है शरीर के बिना, जबकि कन्स्ट्रक्टर का शरीर होना ज़रूरी है, इसलिए तीसरा भी ग़लत है। चौथा कथन बेवजह की एक सीमा गढ़ देता है।
Being private only changes who may reach the constructor; it does not change what the constructor may contain. So it can take parameters and copy them into fields, which makes the first statement true. Constructors are not inherited, so the second is wrong. Abstract means having no body, while a constructor must have one, so the third fails. The fourth invents a limit that Java never imposes.
प्रश्न 6उत्तर + हल
अगर किसी class में सिर्फ़ private कन्स्ट्रक्टर हों तो क्या होता है?
What happens when a class has only private constructors?
Aclass के बाहर से उसका object नहीं बनाया जा सकताObjects of the class cannot be created from outside the class
Bकोई भी दूसरी class new लिखकर उसका object बना सकती हैAny other class can create its objects using the new keyword
Cवह class abstract बन जाती हैThe class becomes abstract
Dवह class final बन जाती हैThe class becomes final
सही उत्तर / Answer: (A)
हल / Solution
private कन्स्ट्रक्टर तक पहुँच सिर्फ़ उसी class के भीतर रहती है। इसलिए बाहर की कोई class new लिखकर उसका object नहीं बना सकती, और पहला विकल्प यही कहता है। भीतर से object ज़रूर बन सकता है — Singleton में getInstance यही करती है। ऐसा करने से class न abstract बनती है न final, वे दोनों अलग कीवर्ड से आते हैं। बस object बनाने का नियंत्रण class अपने ही हाथ में रख लेती है।
A private constructor can be reached only from inside its own class. So no outside class can write new and build an object of it, which is exactly what the first option says. Inside the class an object can still be made, and that is how getInstance works in a Singleton. Doing this makes the class neither abstract nor final; those come from separate keywords. The class simply keeps control of its own creation.
प्रश्न 7उत्तर + हल
Java में private कन्स्ट्रक्टर के बारे में निम्न में से कौन-सा कथन सही है?
Which of the following statements regarding private constructors in Java is true?
Aprivate कन्स्ट्रक्टर तक class के बाहर से पहुँचा जा सकता हैPrivate constructors can be accessed from outside the class
Bprivate कन्स्ट्रक्टर उसी class के भीतर से ही बुलाया जा सकता हैPrivate constructors can only be called within the same class
Cprivate कन्स्ट्रक्टर को सबक्लास ओवरराइड कर सकती हैPrivate constructors can be overridden by subclasses
Dprivate कन्स्ट्रक्टर पर public access modifier भी लग सकता हैPrivate constructors can have public access modifiers
सही उत्तर / Answer: (B)
हल / Solution
private लगाते ही पहुँच का दायरा उसी class की चारदीवारी तक सिमट जाता है। इसलिए ऐसा कन्स्ट्रक्टर वहीं से बुलाया जा सकता है, बाहर से नहीं। दूसरा कथन यही कहता है और पहला उसका उलटा। कन्स्ट्रक्टर विरासत में जाते ही नहीं, इसलिए उन्हें ओवरराइड करने का सवाल नहीं उठता। और किसी एक सदस्य पर एक ही access modifier लगता है, इसलिए private और public साथ-साथ नहीं चल सकते।
The word private shrinks access to the walls of that one class. So such a constructor can be called only from inside the class itself, never from outside, which is what the second statement says and the first denies. Constructors are not inherited, so overriding them never arises. And a single member carries only one access modifier, so private and public cannot sit together on it.
प्रश्न 8उत्तर + हल
Java में कन्स्ट्रक्टर को private घोषित करने का क्या मक़सद होता है?
What is the purpose of declaring a constructor as private in Java?
Aclass के बाहर से उसका object बनने से रोकनाTo prevent the class from being instantiated from outside
Bclass के बाहर से कन्स्ट्रक्टर तक पहुँच देनाTo allow access to the constructor from outside the class
Cकन्स्ट्रक्टर को सिर्फ़ उसी package में पहुँच योग्य बनानाTo make the constructor accessible only within the same package
Dयह पक्का करना कि कन्स्ट्रक्टर ओवरराइड हो सकेTo ensure that the constructor can be overridden
सही उत्तर / Answer: (A)
हल / Solution
कन्स्ट्रक्टर private होते ही उस तक पहुँच सिर्फ़ उसी class के भीतर रह जाती है। बाहर से कोई new नहीं लिख सकता, इसलिए object कब और कितने बनें, इसका नियंत्रण class अपने हाथ में ले लेती है। Singleton और उपयोगिता वर्ग इसी तरकीब से बनते हैं। दूसरा विकल्प ठीक उलटी बात कहता है। package तक सीमित रखने वाला रूप तो default होता है, private नहीं। और कन्स्ट्रक्टर ओवरराइड होते ही नहीं, इसलिए चौथा विकल्प भी ग़लत है।
Once a constructor is private, only code inside that same class can reach it. No outside class can write new, so the class itself decides when and how many objects come into being. Singletons and utility classes are built on this trick. The second option says the opposite. Limiting access to one package is what the default modifier does, not private. And constructors are never overridden, so the fourth fails too.
प्रश्न 9उत्तर + हल
निम्नलिखित Java class पर विचार कीजिए:
public class MyClass {
int x;
public MyClass(int value) {
this();
x = value;
}
public MyClass() {
x = 0;
}
}
अगर MyClass का object पैरामीटर वाले कन्स्ट्रक्टर से 5 देकर बनाया जाए तो x का मान क्या होगा?
Consider the following Java class:
public class MyClass {
int x;
public MyClass(int value) {
this();
x = value;
}
public MyClass() {
x = 0;
}
}
What will be the value of x if an object of MyClass is created using the parameterized constructor with a value of 5?
Aयह 0 होगाIt will be 0
Bयह 5 होगाIt will be 5
Cयह 10 होगाIt will be 10
Dइससे कम्पाइलेशन त्रुटि आएगीIt will result in a compilation error
सही उत्तर / Answer: (B)
हल / Solution
पैरामीटर वाला कन्स्ट्रक्टर पहले this() लिखता है, इसलिए नियंत्रण बिना तर्क वाले कन्स्ट्रक्टर के पास जाता है और वहाँ x शून्य हो जाता है। वापस लौटने पर अगली पंक्ति चलती है, जो x में पाँच भर देती है। बाद वाला लेखन पहले वाले को ढक देता है, इसलिए अंतिम मान पाँच ही रहता है। यह चेनिंग बिलकुल वैध है क्योंकि this() सबसे पहली पंक्ति में लिखा है, इसलिए कोई त्रुटि नहीं आती।
The parameterized constructor starts with this(), so control first goes to the no-argument constructor, which sets x to zero. When control comes back, the next line runs and stores five in x. The later assignment simply overwrites the earlier one, so the final value is 5. The chaining is perfectly legal because this() sits on the very first line, which means no compilation error occurs.
प्रश्न 10उत्तर + हल
कन्स्ट्रक्टर में this कीवर्ड का निम्न में से कौन-सा प्रयोग वैध नहीं है?
Which of the following is NOT a valid use of this keyword in a constructor?
Athis.methodName();
Bthis.variableName = 10;
Cthis(10);
Dthis.class();
सही उत्तर / Answer: (D)
हल / Solution
पहले तीनों रूप Java में चलते हैं — this लगाकर उसी object की मेथड बुलाई जा सकती है, उसके फ़ील्ड में मान भरा जा सकता है, और कोष्ठक वाला रूप उसी class के दूसरे कन्स्ट्रक्टर को बुला देता है। चौथा रूप वैध नहीं है, क्योंकि class कोई मेथड नहीं, एक आरक्षित शब्द है। चलते हुए प्रोग्राम में किसी वर्ग की जानकारी चाहिए तो getClass() काम आती है; इस तरह लिखा हुआ रूप कम्पाइल ही नहीं होगा।
The first three forms are all legal in Java. With this you can call a method on the same object, you can store a value in one of its fields, and the bracket form calls another constructor of the same class. The fourth form is not valid, because class is a reserved word and not a method. If you need class information at runtime you use getClass(), so that line would never compile.
आगे के 20 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
कन्स्ट्रक्टर चेनिंग के बारे में निम्न कथनों में से कौन-सा सही है?
Which of the following statements about constructor chaining in Java is true?
Aकन्स्ट्रक्टर चेनिंग केवल this() कीवर्ड से होती हैConstructor chaining is achieved using the this() keyword only
Bकन्स्ट्रक्टर चेनिंग केवल उसी class के भीतर हो सकती हैConstructor chaining can only be done within the same class
Cकन्स्ट्रक्टर चेनिंग का मतलब उसी class के दूसरे कन्स्ट्रक्टर को बुलाना हैConstructor chaining is the process of calling another constructor from the same class
DJava में कन्स्ट्रक्टर चेनिंग होती ही नहींConstructor chaining is not possible in Java
Java में कन्स्ट्रक्टर की पुनरावृत्ति के बारे में निम्न में से कौन-सा कथन सही है?
Which of the following statements regarding constructor recursion in Java is correct?
AJava में कन्स्ट्रक्टर दूसरे कन्स्ट्रक्टर को बुला ही नहीं सकतेConstructors in Java cannot call other constructors
BJava में कन्स्ट्रक्टर this() या super() से दूसरे कन्स्ट्रक्टर को बुला सकते हैंConstructors in Java can call other constructors using the this() or super() keyword
CJava में कन्स्ट्रक्टर अपने आप एक-दूसरे को बार-बार बुलाते रहते हैंConstructors in Java automatically call each other recursively
Dकन्स्ट्रक्टर की पुनरावृत्ति ग़लती भरा तरीक़ा है और इससे बचना चाहिएConstructor recursion is an error-prone practice and should be avoided
System.out.println("SubClass constructor with parameter");
}
}
जब SubClass obj = new SubClass(5); चलाया जाएगा तो आउटपुट क्या होगा?
Consider the following Java code:
class SuperClass {
public SuperClass() {
System.out.println("SuperClass constructor");
}
}
class SubClass extends SuperClass {
public SubClass() {
System.out.println("SubClass constructor");
}
public SubClass(int x) {
this();
System.out.println("SubClass constructor with parameter");
}
}
What will be the output when SubClass obj = new SubClass(5); is executed?
Aपहले SuperClass constructor, फिर SubClass constructorSuperClass constructor followed by SubClass constructor
Bपहले SubClass constructor, फिर SuperClass constructorSubClass constructor followed by SuperClass constructor
Cपहले SubClass constructor, फिर SubClass constructor with parameterSubClass constructor followed by SubClass constructor with parameter
Dपहले SuperClass constructor, फिर SubClass constructor, फिर SubClass constructor with parameterSuperClass constructor followed by SubClass constructor followed by SubClass constructor with parameter
System.out.println("SubClass constructor with parameter");
}
}
जब SubClass obj = new SubClass(); चलाया जाएगा तो आउटपुट क्या होगा?
Consider the following Java code:
class SuperClass {
public SuperClass() {
System.out.println("SuperClass constructor");
}
}
class SubClass extends SuperClass {
public SubClass() {
this(10);
System.out.println("SubClass constructor");
}
public SubClass(int x) {
System.out.println("SubClass constructor with parameter");
}
}
What will be the output when SubClass obj = new SubClass(); is executed?
Aकम्पाइलेशन त्रुटिCompilation error
Bपहले SuperClass constructor, फिर SubClass constructorSuperClass constructor followed by SubClass constructor
Cपहले SubClass constructor with parameter, फिर SubClass constructorSubClass constructor with parameter followed by SubClass constructor
Dपहले SuperClass constructor, फिर SubClass constructor with parameter, फिर SubClass constructorSuperClass constructor followed by SubClass constructor with parameter followed by SubClass constructor