FliQi Education App Download
प्रश्न-बैंक · Programmer Java

OOPs Concept - Constructors — प्रश्न और उत्तर

Programmer Java · 77 जाँचे हुए (verified) प्रश्न

  • 77 जाँचे हुए प्रश्न
  • हिंदी + English

पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 20 के उत्तर और हल FliQi Question Bank app में।

भाषा
प्रश्न 1उत्तर + हल
Java में private कन्स्ट्रक्टर के बारे में निम्न कथनों में से कौन-सा सही है?
Which of the following statements is true regarding private constructors in Java?
  1. Aprivate कन्स्ट्रक्टर को reflection से बुलाया जा सकता हैPrivate constructors can be called using reflection
  2. Bprivate कन्स्ट्रक्टर सबक्लास से पहुँच योग्य होते हैंPrivate constructors are accessible from subclasses
  3. Cclass के लोड होते ही private कन्स्ट्रक्टर अपने आप चल जाता हैA private constructor runs by itself as soon as the class is loaded
  4. 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?
  1. AFactory Method
  2. BPrototype
  3. CSingleton
  4. 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?
  1. Aclass को subclass बनने से रोकनाTo restrict subclassing of a class
  2. Bclass की कुछ मेथडों तक पहुँच रोकनाTo restrict access to certain methods of a class
  3. Cदूसरी class से आसानी से object बनने देनाTo allow easy instantiation of objects from other classes
  4. 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?
  1. Aकम्पाइलेशन त्रुटिCompilation error
  2. Bरनटाइम त्रुटिRuntime error
  3. Cobject सफलतापूर्वक बन जाएगाThe object will be created successfully
  4. 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?
  1. Aprivate कन्स्ट्रक्टर में पैरामीटर हो सकते हैंPrivate constructors can have parameters
  2. Bprivate कन्स्ट्रक्टर सबक्लास को विरासत में मिलते हैंPrivate constructors are inherited by subclasses
  3. Cprivate कन्स्ट्रक्टर को abstract घोषित किया जा सकता हैPrivate constructors can be declared abstract
  4. 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?
  1. Aclass के बाहर से उसका object नहीं बनाया जा सकताObjects of the class cannot be created from outside the class
  2. Bकोई भी दूसरी class new लिखकर उसका object बना सकती हैAny other class can create its objects using the new keyword
  3. Cवह class abstract बन जाती हैThe class becomes abstract
  4. 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?
  1. Aprivate कन्स्ट्रक्टर तक class के बाहर से पहुँचा जा सकता हैPrivate constructors can be accessed from outside the class
  2. Bprivate कन्स्ट्रक्टर उसी class के भीतर से ही बुलाया जा सकता हैPrivate constructors can only be called within the same class
  3. Cprivate कन्स्ट्रक्टर को सबक्लास ओवरराइड कर सकती हैPrivate constructors can be overridden by subclasses
  4. 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?
  1. Aclass के बाहर से उसका object बनने से रोकनाTo prevent the class from being instantiated from outside
  2. Bclass के बाहर से कन्स्ट्रक्टर तक पहुँच देनाTo allow access to the constructor from outside the class
  3. Cकन्स्ट्रक्टर को सिर्फ़ उसी package में पहुँच योग्य बनानाTo make the constructor accessible only within the same package
  4. 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?
  1. Aयह 0 होगाIt will be 0
  2. Bयह 5 होगाIt will be 5
  3. Cयह 10 होगाIt will be 10
  4. 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?
  1. Athis.methodName();
  2. Bthis.variableName = 10;
  3. Cthis(10);
  4. 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 — अभ्यास, हल और आपकी प्रगति
App खोलिए
प्रश्न 11उत्तर app में
कन्स्ट्रक्टर चेनिंग के बारे में निम्न कथनों में से कौन-सा सही है?
Which of the following statements about constructor chaining in Java is true?
  1. Aकन्स्ट्रक्टर चेनिंग केवल this() कीवर्ड से होती हैConstructor chaining is achieved using the this() keyword only
  2. Bकन्स्ट्रक्टर चेनिंग केवल उसी class के भीतर हो सकती हैConstructor chaining can only be done within the same class
  3. Cकन्स्ट्रक्टर चेनिंग का मतलब उसी class के दूसरे कन्स्ट्रक्टर को बुलाना हैConstructor chaining is the process of calling another constructor from the same class
  4. DJava में कन्स्ट्रक्टर चेनिंग होती ही नहींConstructor chaining is not possible in Java
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 12उत्तर app में
Java में कन्स्ट्रक्टर के भीतर this() लिखने का क्या मक़सद होता है?
In Java, what is the purpose of using this() keyword in a constructor?
  1. Aसुपरक्लास की किसी मेथड को बुलानाTo call a method from the superclass
  2. Bclass के वर्तमान instance को दर्शानाTo refer to the current instance of the class
  3. Cclass का नया instance बनानाTo create a new instance of the class
  4. Dउसी class के दूसरे कन्स्ट्रक्टर को बुलानाTo call another constructor within the same class
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 13उत्तर app में
Java में कन्स्ट्रक्टर की पुनरावृत्ति के बारे में निम्न में से कौन-सा कथन सही है?
Which of the following statements regarding constructor recursion in Java is correct?
  1. AJava में कन्स्ट्रक्टर दूसरे कन्स्ट्रक्टर को बुला ही नहीं सकतेConstructors in Java cannot call other constructors
  2. BJava में कन्स्ट्रक्टर this() या super() से दूसरे कन्स्ट्रक्टर को बुला सकते हैंConstructors in Java can call other constructors using the this() or super() keyword
  3. CJava में कन्स्ट्रक्टर अपने आप एक-दूसरे को बार-बार बुलाते रहते हैंConstructors in Java automatically call each other recursively
  4. Dकन्स्ट्रक्टर की पुनरावृत्ति ग़लती भरा तरीक़ा है और इससे बचना चाहिएConstructor recursion is an error-prone practice and should be avoided
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 14उत्तर app में
निम्नलिखित Java class पर विचार कीजिए:

public class MyClass {

    private int x;

    public MyClass() {

        x = 0;

    }

    public MyClass(int value) {

        x = value;

    }

}

MyClass class में कितने कन्स्ट्रक्टर हैं?
Consider the following Java class:

public class MyClass {

    private int x;

    public MyClass() {

        x = 0;

    }

    public MyClass(int value) {

        x = value;

    }

}

How many constructors does the class MyClass have?
  1. Aकोई नहींNone
  2. BएकOne
  3. CदोTwo
  4. DतीनThree
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 15उत्तर app में
कन्स्ट्रक्टर ओवरलोडिंग के बारे में निम्न में से कौन-सा कथन सही है?
Which of the following statements about constructor overloading is true?
  1. AJava में कन्स्ट्रक्टर ओवरलोड हो ही नहीं सकतेConstructors cannot be overloaded in Java
  2. Bओवरलोडिंग सिर्फ़ पैरामीटर की संख्या पर टिकी होती हैConstructor overloading is based only on the number of parameters
  3. Cओवरलोडिंग कन्स्ट्रक्टर के रिटर्न टाइप पर टिकी होती हैConstructor overloading is based on the return type of the constructor
  4. Dओवरलोडिंग पैरामीटर की संख्या और प्रकार, दोनों पर टिकी होती हैConstructor overloading is based on the number and type of parameters
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 16उत्तर app में
Java में कन्स्ट्रक्टर ओवरलोडिंग किसे कहते हैं?
What is constructor overloading in Java?
  1. Aएक ही संख्या के पैरामीटर वाले कई कन्स्ट्रक्टर बनानाDefining multiple constructors with the same number of parameters
  2. Bअलग-अलग access modifier वाले कई कन्स्ट्रक्टर बनानाDefining multiple constructors with different access modifiers
  3. Cअलग-अलग रिटर्न टाइप वाले कई कन्स्ट्रक्टर बनानाDefining multiple constructors with different return types
  4. Dएक ही नाम पर अलग-अलग पैरामीटर वाले कई कन्स्ट्रक्टर बनानाDefining multiple constructors with the same name but different parameters
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 17उत्तर app में
Java में कन्स्ट्रक्टर के भीतर this कीवर्ड के बारे में कौन-सा कथन सही है?
In Java, which of the following statements is true about using this keyword in a constructor?
  1. Athis से class का नया instance बनाया जा सकता हैthis can be used to create a new instance of the class
  2. Bthis से static वेरिएबल तक पहुँचा जा सकता हैthis can be used to access static variables
  3. Cthis से सुपरक्लास का कन्स्ट्रक्टर बुलाया जा सकता हैthis can be used to call a constructor of the superclass
  4. Dthis से उसी class के दूसरे कन्स्ट्रक्टर को बुलाया जा सकता हैthis can be used to call another constructor in the same class
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 18उत्तर app में
निम्नलिखित Java class पर विचार कीजिए:

public class MyClass {

    private int x;

    public MyClass() {

        this(0);

    }

    public MyClass(int x) {

        this.x = x;

    }

}

अगर MyClass का object डिफ़ॉल्ट कन्स्ट्रक्टर से बनाया जाए तो x का मान क्या होगा?
Consider the following Java class:

public class MyClass {

    private int x;

    public MyClass() {

        this(0);

    }

    public MyClass(int x) {

        this.x = x;

    }

}

What will be the value of x if an object of MyClass is created using the default constructor?
  1. Aयह 0 होगाIt will be 0
  2. Bयह 1 होगाIt will be 1
  3. Cयह तब तक अनिर्धारित रहेगा जब तक कोई मान न दिया जाएIt will stay unassigned until some value is given
  4. Dयह 10 होगाIt will be 10
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 20उत्तर app में
निम्नलिखित Java class पर विचार कीजिए:

public class MyClass {

    private int x;

    public MyClass(int x) {

        this.x = x;

    }

}

कन्स्ट्रक्टर में this.x = x लिखने का क्या मक़सद है?
Consider the following Java class:

public class MyClass {

    private int x;

    public MyClass(int x) {

        this.x = x;

    }

}

What is the purpose of using this.x = x in the constructor?
  1. Aयह x को ख़ुद उसी का मान दे देता हैIt assigns the value of x to itself
  2. Bयह तर्क में आए x का मान instance वेरिएबल x को दे देता हैIt assigns the value of x passed as an argument to the instance variable x
  3. Cयह x का मान MyClass के किसी नए instance को दे देता हैIt assigns the value of x to a new instance of MyClass
  4. Dयह x का मान किसी static वेरिएबल को दे देता हैIt assigns the value of x to a static variable
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 21उत्तर app में
कन्स्ट्रक्टर में this कीवर्ड के बारे में निम्न में से कौन-सा कथन सही है?
Which of the following statements about the this keyword in constructors is true?
  1. Aइसका उपयोग सिर्फ़ उसी class के दूसरे कन्स्ट्रक्टर दर्शाने में हो सकता हैIt can only be used to refer to other constructors of the same class
  2. Bइसका उपयोग सिर्फ़ उसी class के instance वेरिएबल दर्शाने में हो सकता हैIt can only be used to refer to instance variables of the same class
  3. Cइसका उपयोग सिर्फ़ पैरामीटर वाले कन्स्ट्रक्टर के भीतर हो सकता हैIt can only be used inside parameterized constructors
  4. Dइसका उपयोग उसी class के दूसरे कन्स्ट्रक्टर को बुलाने में हो सकता हैIt can be used to invoke another constructor of the same class
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 22उत्तर app में
Java में कन्स्ट्रक्टर के भीतर लिखा this कीवर्ड किसे दर्शाता है?
In Java, what does the keyword this refer to when used inside a constructor?
  1. Aउस class के वर्तमान instance कोIt refers to the current instance of the class
  2. Bउस class के सुपरक्लास कोIt refers to the superclass of the class
  3. Cclass को ही, किसी object को नहींIt refers to the class itself, not to any object
  4. Dअभी-अभी बनी object की नक़ल कोIt refers to a freshly made copy of the object
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 23उत्तर app में
निम्नलिखित Java कोड पर विचार कीजिए:

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");

    }

}

जब 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?
  1. Aपहले SuperClass constructor, फिर SubClass constructorSuperClass constructor followed by SubClass constructor
  2. Bपहले SubClass constructor, फिर SuperClass constructorSubClass constructor followed by SuperClass constructor
  3. Cपहले SubClass constructor, फिर SubClass constructor with parameterSubClass constructor followed by SubClass constructor with parameter
  4. Dपहले SuperClass constructor, फिर SubClass constructor, फिर SubClass constructor with parameterSuperClass constructor followed by SubClass constructor followed by SubClass constructor with parameter
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 24उत्तर app में
निम्नलिखित Java कोड पर विचार कीजिए:

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");

    }

}

जब 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?
  1. Aकम्पाइलेशन त्रुटिCompilation error
  2. Bपहले SuperClass constructor, फिर SubClass constructorSuperClass constructor followed by SubClass constructor
  3. Cपहले SubClass constructor with parameter, फिर SubClass constructorSubClass constructor with parameter followed by SubClass constructor
  4. Dपहले SuperClass constructor, फिर SubClass constructor with parameter, फिर SubClass constructorSuperClass constructor followed by SubClass constructor with parameter followed by SubClass constructor
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 25उत्तर app में
निम्नलिखित Java कोड पर विचार कीजिए:

class SuperClass {

    public SuperClass() {

        System.out.println("SuperClass constructor");

    }

}

class SubClass extends SuperClass {

    public SubClass() {

        System.out.println("SubClass constructor");

    }

}

जब SubClass obj = new SubClass(); चलाया जाएगा तो आउटपुट क्या होगा?
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");

    }

}

What will be the output when SubClass obj = new SubClass(); is executed?
  1. ASuperClass constructor
  2. BSubClass constructor
  3. Cपहले SuperClass constructor, फिर SubClass constructorSuperClass constructor followed by SubClass constructor
  4. Dपहले SubClass constructor, फिर SuperClass constructorSubClass constructor followed by SuperClass constructor
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 26उत्तर app में
Java में कन्स्ट्रक्टर के भीतर super() लिखने की ज़रूरत कब पड़ती है?
In Java, when is the super() call in a constructor required?
  1. Aजब सबक्लास और सुपरक्लास के कन्स्ट्रक्टर एक जैसे होंWhen subclass and superclass have the same constructors
  2. Bजब सबक्लास सुपरक्लास की किसी मेथड को ओवरराइड करेWhen subclass overrides a method from superclass
  3. Cजब सबक्लास के कन्स्ट्रक्टर में कोई पैरामीटर न होWhen subclass constructor has no parameters
  4. Dजब सबक्लास के कन्स्ट्रक्टर को सुपरक्लास का कोई कन्स्ट्रक्टर बुलाना होWhen subclass constructor needs to invoke a superclass constructor
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 27उत्तर app में
Java में कन्स्ट्रक्टर के भीतर super() का क्या काम है?
What is the purpose of super() in a constructor in Java?
  1. Aसुपरक्लास की किसी मेथड को बुलानाTo call a method in the superclass
  2. Bसुपरक्लास के वेरिएबल में मान भरनाTo initialize superclass variables
  3. Cसुपरक्लास के कन्स्ट्रक्टर को बुलानाTo invoke the superclass constructor
  4. Dसुपरक्लास का नया instance बनानाTo create a new instance of the superclass
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 28उत्तर app में
निम्नलिखित Java class पर विचार कीजिए:

public class MyClass {

    int x;

       public MyClass(int value) {

        x = value;

    }

       public MyClass() {

        x = 10;

    }

}

नीचे दिए कोड का आउटपुट क्या होगा?

MyClass obj = new MyClass();

System.out.println(obj.x);
Consider the following Java class:

public class MyClass {

    int x;

       public MyClass(int value) {

        x = value;

    }

       public MyClass() {

        x = 10;

    }

}

What will be the output of the code below?

MyClass obj = new MyClass();

System.out.println(obj.x);
  1. Aकम्पाइलेशन त्रुटिCompilation error
  2. Bरनटाइम त्रुटिRuntime error
  3. C0
  4. D10
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 29उत्तर app में
निम्नलिखित Java class पर विचार कीजिए:

public class MyClass {

    int x;

    // Default constructor

    public MyClass() {

        x = 10;

    }

    public MyClass(int value) {

        x = value;

    }

    public static void main(String[] args) {

        MyClass obj1 = new MyClass();

        MyClass obj2 = new MyClass(20);

        System.out.println(obj1.x + " " + obj2.x);

    }

}

ऊपर दिए कोड का आउटपुट क्या होगा?
Consider the following Java class:

public class MyClass {

    int x;

    // Default constructor

    public MyClass() {

        x = 10;

    }

    public MyClass(int value) {

        x = value;

    }

    public static void main(String[] args) {

        MyClass obj1 = new MyClass();

        MyClass obj2 = new MyClass(20);

        System.out.println(obj1.x + " " + obj2.x);

    }

}

What will be the output of the above code?
  1. A10 20
  2. B20 20
  3. C10 10
  4. Dकम्पाइलेशन त्रुटिCompilation error
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 30उत्तर app में
निम्नलिखित class परिभाषा पर विचार कीजिए:

public class MyClass {

    private int value;

    public MyClass() {

        value = 10;

    }

    public int getValue() {

        return value;

    }

}

MyClass के कन्स्ट्रक्टर के बारे में निम्न में से कौन-सा कथन सही है?
Consider the following class definition:

public class MyClass {

    private int value;

    public MyClass() {

        value = 10;

    }

    public int getValue() {

        return value;

    }

}

Which of the following statements regarding the MyClass constructor is true?
  1. Aयह डिफ़ॉल्ट यानी बिना-तर्क वाला कन्स्ट्रक्टर हैIt is a default, no-argument constructor
  2. Bयह पैरामीटर वाला कन्स्ट्रक्टर हैIt is a parameterized constructor
  3. Cयह value फ़ील्ड में शून्य भरता हैIt initializes the value field to 0
  4. Dयह private कन्स्ट्रक्टर हैIt is a private constructor
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
OOPs Concept - Constructors के सभी 77 प्रश्न — उत्तर, हल और अभ्यास

FliQi Question Bank app में topic-वार अभ्यास, कठिनाई-स्तर और आपकी प्रगति।

  • पहला test मुफ़्त
  • हिंदी + English
  • जाँचे हुए उत्तर और हल

यह topic किन परीक्षाओं में आता है

Programmer Java के दूसरे topics

सभी topics