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

OOPs Concept - Object in java — प्रश्न और उत्तर

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

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

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

भाषा
प्रश्न 1उत्तर + हल
निम्नलिखित जावा कोड पर विचार कीजिए। इसका output क्या होगा?
public class MyClass {
    private int x;
    public MyClass(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
    public static void main(String[] args) {
        MyClass obj1 = new MyClass(5);
        MyClass obj2 = new MyClass(5);
        System.out.println(obj1.equals(obj2));
    }
}
Consider the following Java code snippet. What will be the output of the code?
public class MyClass {
    private int x;
    public MyClass(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
    public static void main(String[] args) {
        MyClass obj1 = new MyClass(5);
        MyClass obj2 = new MyClass(5);
        System.out.println(obj1.equals(obj2));
    }
}
  1. Atrue
  2. Bfalse
  3. CCompilation error
  4. DRuntime error

सही उत्तर / Answer: (B)

हल / Solution
MyClass ने equals() को फिर से नहीं लिखा है, इसलिए Object class वाला रूप चलता है, जो केवल reference मिलाता है। obj1 और obj2 अलग-अलग new से बने हैं, इसलिए दोनों heap पर अलग जगह लिए हुए हैं, चाहे भीतर x की value 5 ही हो। इसलिए तुलना false देती है और दूसरा विकल्प सही है। true पाने के लिए class में equals() को x मिलाने के हिसाब से लिखना पड़ता, साथ में hashCode() भी।
MyClass does not override equals(), so the version from the Object class runs and that one only matches references. obj1 and obj2 were built by separate calls to new, so they hold different places on the heap even though x is 5 in both. The comparison therefore gives false and option 2 is correct. Getting true would need equals() written to match x, along with hashCode().
प्रश्न 2उत्तर + हल
Object class की clone() method क्या करती है?
What does the clone() method in the Object class do?
  1. Aobject की deep copy बनाती है।Creates a deep copy of the object.
  2. Bobject की shallow copy बनाती है।Creates a shallow copy of the object.
  3. Cदो object की बराबरी जाँचती है।Compares two objects for equality.
  4. DCloneNotSupportedException फेंकती है।Throws a CloneNotSupportedException.

सही उत्तर / Answer: (B)

हल / Solution
Object class वाला clone() नई जगह लेकर उसमें पुराने object के field जैसे हैं वैसे उतार देता है। जहाँ field ख़ुद कोई reference है, वहाँ केवल वही reference उतरता है, भीतर का object नया नहीं बनता। इसलिए वह छिछली नक़ल यानी shallow copy है और दूसरा विकल्प सही है। गहरी नक़ल चाहिए तो clone() को अपनी class में फिर से लिखना पड़ता है। CloneNotSupportedException सिर्फ़ तब आती है जब class ने Cloneable लागू न किया हो।
The clone() of the Object class takes fresh space and copies the fields of the old object just as they are. Where a field is itself a reference, only that reference is copied and the inner object is not built again. So it is a shallow copy and option 2 is correct. A deep copy needs clone() to be overridden in your own class. A CloneNotSupportedException comes only when the class has not implemented Cloneable.
प्रश्न 3उत्तर + हल
यदि जावा में कोई class किसी दूसरी class को स्पष्ट रूप से extend न करे तो क्या होता है?
What happens if a class in Java does not explicitly extend any other class?
  1. Aवह अपने आप Object class को extend करती है।It extends the Object class implicitly.
  2. Bcompilation error आ जाती है।It throws a compilation error.
  3. Cवह अपने आप Class class को extend करती है।It extends the Class class implicitly.
  4. Dवह अपने आप Abstract class को extend करती है।It extends the Abstract class implicitly.

सही उत्तर / Answer: (A)

हल / Solution
जावा में यदि class के आगे extends न लिखा हो तो compiler अपने आप उसे Object की संतान बना देता है। इसलिए पहला विकल्प सही है। यही वजह है कि हर object पर toString(), equals() और hashCode() चल जाती हैं, चाहे आपने उन्हें लिखा ही न हो। ऐसा लिखने में कोई ग़लती नहीं होती, इसलिए दूसरा विकल्प ग़लत है। Class नाम की class जड़ नहीं है, और abstract class अपने आप किसी की जड़ नहीं बनती।
In Java, if a class carries no extends clause, the compiler itself makes it a child of Object. So option 1 is correct. That is why toString(), equals() and hashCode() work on every object even when you never wrote them. Writing such a class is no error at all, so option 2 is wrong. The class named Class is not the root, and an abstract class does not become anybody root on its own.
प्रश्न 4उत्तर + हल
जावा में यदि किसी object को println() में दिया जाए तो कौन-सी method अपने आप बुलाई जाती है?
In Java, which method is called implicitly if an object is used in a println() statement?
  1. Aprint()
  2. Bdisplay()
  3. CtoString()
  4. Dshow()

सही उत्तर / Answer: (C)

हल / Solution
println() में किसी object का नाम देते ही जावा उस पर चुपचाप toString() चला देता है और लौटा हुआ पाठ छापता है। इसलिए तीसरा विकल्प सही है। यही कारण है कि अपनी class में toString() न लिखने पर नाम और hash code वाला अजीब पाठ दिखता है। print(), display() और show() नाम की कोई method Object class में नहीं होती, इसलिए बाक़ी तीन विकल्प ग़लत हैं। null reference पर केवल शब्द null छपता है।
As soon as the name of an object is given to println(), Java quietly runs toString() on it and prints the text that comes back. So option 3 is correct. This is why, without overriding toString(), you see odd text made of a class name and a hash code. No method called print(), display() or show() exists in the Object class, so the other three options are wrong. For a null reference only the word null is printed.
प्रश्न 5उत्तर + हल
Object class की toString() method क्या करती है?
What does the toString() method in the Object class do?
  1. Aobject को string रूप में बदलती है।Converts an object to a string representation.
  2. Bदो object की बराबरी जाँचती है।Compares two objects for equality.
  3. Cobject का hash code लौटाती है।Returns the hash code of the object.
  4. Dदो object के memory पते मिलाती है।Compares the memory addresses of two objects.

सही उत्तर / Answer: (A)

हल / Solution
toString() का काम object को पढ़े जा सकने वाले पाठ में बदलना है। Object class वाला रूप class का नाम और hash code मिलाकर लौटाता है, इसलिए अपनी class में उसे फिर से लिखना अच्छा रहता है। इसी कारण पहला विकल्प सही है। बराबरी जाँचने का काम equals() का है और hash code लौटाना hashCode() का। पते मिलाना भी असल में equals() के default रूप में होता है, toString() में नहीं।
The job of toString() is to turn an object into text that can be read. The version in the Object class returns the class name joined with a hash code, so overriding it in your own class is a good habit. That is why option 1 is correct. Checking equality is the work of equals() and returning a hash code is the work of hashCode(). Matching addresses also belongs to the default equals(), not to toString().
प्रश्न 6उत्तर + हल
जावा में किसी object का hash code प्राप्त करने के लिए कौन-सी method काम आती है?
Which of the following methods is used to obtain a hash code value for an object in Java?
  1. Ahash()
  2. Bhashcode()
  3. ChashCode()
  4. DgetHashCode()

सही उत्तर / Answer: (C)

हल / Solution
जावा में hash code लेने वाली method का नाम hashCode() है, जिसमें दूसरा शब्द बड़े अक्षर से शुरू होता है। यही camel case का नियम है, और जावा नाम में छोटे तथा बड़े अक्षर का फ़र्क़ रखता है। इसलिए तीसरा विकल्प सही है। hash(), hashcode() और getHashCode() नाम Object class में नहीं हैं, ऐसा लिखने पर compiler ग़लती बता देगा। HashMap इसी hashCode() से bucket तय करता है।
The method for getting a hash code in Java is named hashCode(), with the second word beginning in capital. That is the camel case rule, and Java treats small and capital letters in a name as different. So option 3 is correct. The names hash(), hashcode() and getHashCode() do not exist in the Object class, and writing them makes the compiler report an error. HashMap uses this hashCode() to pick a bucket.
प्रश्न 7उत्तर + हल
Object class में equals() method का default रूप क्या करता है?
What is the default implementation of the equals() method in the Object class?
  1. Aयह memory के पते की तुलना करता है।It compares memory addresses.
  2. Bयह object के content की तुलना करता है।It compares the contents of the objects.
  3. Cयह NullPointerException फेंकता है।It throws a NullPointerException.
  4. Dयह सभी object के लिए false लौटाता है।It returns false for all objects.

सही उत्तर / Answer: (A)

हल / Solution
Object class वाला equals() केवल reference मिलाता है, यानी वह देखता है कि दोनों नाम एक ही object की ओर इशारा कर रहे हैं या नहीं। इस अर्थ में यह memory के पते की तुलना है, इसलिए पहला विकल्प सही है। भीतर की value मिलाने के लिए equals() को अपनी class में फिर से लिखना पड़ता है, जैसे String करती है। null देने पर वह false लौटाता है, कोई exception नहीं फेंकता, और एक ही object पर true देता है।
The equals() of the Object class only matches references, that is, it checks whether both names point at one and the same object. In that sense it compares memory addresses, so option 1 is correct. To match the contents, equals() has to be overridden in your own class, as String does. Given null it returns false rather than throwing an exception, and for one and the same object it returns true.
प्रश्न 8उत्तर + हल
निम्नलिखित में से कौन-सी method Object class में परिभाषित नहीं है?
Which of the following methods is not defined in the Object class?
  1. Aequals()
  2. BhashCode()
  3. CtoString()
  4. DcompareTo()

सही उत्तर / Answer: (D)

हल / Solution
Object class में equals(), hashCode(), toString(), clone(), getClass(), finalize(), wait() और notify() जैसी method हैं। compareTo() उनमें नहीं है, वह Comparable interface की method है जिसे String या Integer जैसी class लागू करती हैं। इसलिए चौथा विकल्प सही है। यही कारण है कि अपनी बनाई class के object को क्रम में लगाने के लिए Comparable या Comparator लिखना पड़ता है, वरना compareTo() मिलती ही नहीं।
The Object class holds methods like equals(), hashCode(), toString(), clone(), getClass(), finalize(), wait() and notify(). compareTo() is not among them; it belongs to the Comparable interface, which classes such as String and Integer implement. So option 4 is correct. That is why sorting objects of your own class needs Comparable or Comparator to be written, otherwise no compareTo() is available at all.
प्रश्न 9उत्तर + हल
जावा में सभी class की superclass कौन-सी है?
What is the superclass of all classes in Java?
  1. AMain
  2. BSuper
  3. CObject
  4. DClass

सही उत्तर / Answer: (C)

हल / Solution
जावा में हर class सीधे या घुमाकर Object class से ही उतरती है, इसलिए वही सबसे ऊपरी class है और तीसरा विकल्प सही है। इसी कारण equals(), hashCode() और toString() जैसी method हर object के पास होती हैं। Main कोई ख़ास class नहीं, वह सिर्फ़ एक आम नाम है। super एक keyword है, class नहीं। Class नाम की class मौजूद तो है, पर वह चलते समय जानकारी देने के लिए है, सबकी जड़ नहीं।
In Java every class comes down, directly or through others, from the Object class, so that is the topmost class and option 3 is correct. This is why methods like equals(), hashCode() and toString() are present on every object. Main is no special class, only a common name. super is a keyword and not a class. A class named Class does exist, but it carries runtime information and is not the root of all.
प्रश्न 10उत्तर + हल
निम्नलिखित जावा कोड पर विचार कीजिए। इसका output क्या होगा?
public class MyClass {
    private int x;
    public MyClass(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
    public static void main(String[] args) {
        MyClass obj1 = new MyClass(5);
        MyClass obj2 = new MyClass(10);
        obj1 = obj2;
        System.out.println(obj1.getX());
    }
}
Consider the following Java code. What will be the output of the code?
public class MyClass {
    private int x;
    public MyClass(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
    public static void main(String[] args) {
        MyClass obj1 = new MyClass(5);
        MyClass obj2 = new MyClass(10);
        obj1 = obj2;
        System.out.println(obj1.getX());
    }
}
  1. A5
  2. B10
  3. CCompilation error
  4. DRuntime error

सही उत्तर / Answer: (B)

हल / Solution
पहले obj1 का x 5 और obj2 का x 10 होता है। फिर obj1 = obj2 लिखा गया है। इससे object की नक़ल नहीं होती, सिर्फ़ obj1 भी उसी object की ओर इशारा करने लगता है जिसकी ओर obj2 है। इसलिए obj1.getX() अब 10 लौटाता है और दूसरा विकल्प सही है। 5 वाला object किसी reference के बिना रह जाता है और garbage collector की राह देखता है। पंक्तियाँ वैध हैं, इसलिए ग़लती वाले विकल्प नहीं बनते।
At first the x of obj1 is 5 and that of obj2 is 10. Then obj1 = obj2 is written. This copies no object; it only makes obj1 point at the very object that obj2 points to. So obj1.getX() now returns 10 and option 2 is correct. The object holding 5 is left with no reference and waits for the garbage collector. The lines are legal, so the error options do not arise.
आगे के 20 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
App खोलिए
प्रश्न 11उत्तर app में
निम्नलिखित जावा कोड का output क्या होगा?
public class MyClass {
    private int x;
    public MyClass() {
        x = 10;
    }
    public MyClass(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
    public static void main(String[] args) {
        MyClass obj1 = new MyClass();
        MyClass obj2 = new MyClass(20);
        System.out.println(obj1.getX() + obj2.getX());
    }
}
What is the output of the following Java code snippet?
public class MyClass {
    private int x;
    public MyClass() {
        x = 10;
    }
    public MyClass(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
    public static void main(String[] args) {
        MyClass obj1 = new MyClass();
        MyClass obj2 = new MyClass(20);
        System.out.println(obj1.getX() + obj2.getX());
    }
}
  1. A10
  2. B20
  3. C30
  4. DCompilation error
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 12उत्तर app में
निम्नलिखित कोड पर विचार कीजिए। इसका output क्या होगा?
public class MyClass {
    private int x;
    public MyClass(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
    public static void main(String[] args) {
        MyClass obj1 = new MyClass(5);
        MyClass obj2 = new MyClass(10);
        System.out.println(obj1.getX() + obj2.getX());
    }
}
Consider the following code. What will be the output of the code?
public class MyClass {
    private int x;
    public MyClass(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
    public static void main(String[] args) {
        MyClass obj1 = new MyClass(5);
        MyClass obj2 = new MyClass(10);
        System.out.println(obj1.getX() + obj2.getX());
    }
}
  1. A5
  2. B10
  3. C15
  4. D20
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 13उत्तर app में
निम्नलिखित जावा कोड क्या करता है?
public class MyClass {
    private int x;
    public MyClass(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
    public static void main(String[] args) {
        MyClass obj = new MyClass(5);
        System.out.println(obj.getX());
    }
}
What does the following Java code snippet do?
public class MyClass {
    private int x;
    public MyClass(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
    public static void main(String[] args) {
        MyClass obj = new MyClass(5);
        System.out.println(obj.getX());
    }
}
  1. A0 छापता हैPrints 0
  2. B5 छापता हैPrints 5
  3. CCompilation error
  4. DRuntime error
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 14उत्तर app में
जावा में जब object बनता है तब क्या होता है?
What happens when an object is created in Java?
  1. Aobject के लिए stack पर memory दी जाती है।Memory is allocated for the object on the stack.
  2. Bobject के लिए heap पर memory दी जाती है।Memory is allocated for the object on the heap.
  3. Cobject JVM के method area में रखा जाता है।The object is stored in the method area of the JVM.
  4. Dobject तुरंत garbage collect हो जाता है।The object is immediately garbage-collected.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 16उत्तर app में
जावा के object के संदर्भ में instantiation शब्द का अर्थ क्या है?
What does the term instantiation mean in the context of Java objects?
  1. Aनई class बनाने की प्रक्रिया।The process of creating a new class.
  2. Bobject के लिए memory आवंटित करने की प्रक्रिया।The process of allocating memory for an object.
  3. Cclass को परिभाषित करने की प्रक्रिया।The process of defining a class.
  4. Dकिसी method को बुलाने की प्रक्रिया।The process of calling a method.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 17उत्तर app में
जावा में object के बारे में निम्नलिखित में से क्या सही है?
Which of the following is true about objects in Java?
  1. Aobject को method में argument के रूप में नहीं भेजा जा सकता।Objects cannot be passed as arguments to methods.
  2. Bobject हमेशा stack memory में रखे जाते हैं।Objects are always stored on the stack memory.
  3. Cobject primitive data type के instance होते हैं।Objects are instances of primitive data types.
  4. Dobject में अवस्था और व्यवहार होते हैं।Objects have state and behavior.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 18उत्तर app में
निम्नलिखित कोड का परिणाम क्या होगा?
String str = "Hello";
String newStr = new String(str);
What is the result of the following code snippet?
String str = "Hello";
String newStr = new String(str);
  1. Aयह str के समान reference वाला नया String object बनाता है।It creates a new String object with the same reference as str.
  2. Bयह str के समान content वाला नया String object बनाता है।It creates a new String object with the same content as str.
  3. Cयह compilation error देता है।It throws a compilation error.
  4. Dयह बिना किसी content का नया String object बनाता है।It creates a new String object without any content.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 19उत्तर app में
जावा में object के लिए memory का आवंटन कब होता है?
In Java, when does the memory allocation for an object occur?
  1. Acompile होते समयDuring compilation
  2. Bclass load होते समयDuring class loading
  3. Cruntime परDuring runtime
  4. Dinitialization के समयDuring initialization
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 20उत्तर app में
यदि हम जावा में new keyword के बिना object बनाने की कोशिश करें तो क्या होगा?
What happens if we try to create an object in Java without using the new keyword?
  1. Aobject बन जाता है पर initialize नहीं होता।The object is created but not initialized.
  2. Bcompiler ग़लती बता देता है।The compiler throws an error.
  3. Cobject default value के साथ बन जाता है।The object is created with default values.
  4. Dobject null reference के रूप में बन जाता है।The object is created as a null reference.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 21उत्तर app में
जावा में new keyword के बारे में निम्नलिखित में से क्या सही है?
Which of the following is true regarding the new keyword in Java?
  1. Aयह class के static member तक पहुँचने के लिए इस्तेमाल होता है।It is used to access static members of a class.
  2. Bयह class के instance बनाने के लिए इस्तेमाल होता है।It is used to create instances of classes.
  3. Cयह class के instance variable तक पहुँचने के लिए इस्तेमाल होता है।It is used to access instance variables of a class.
  4. Dयह class में abstract method परिभाषित करने के लिए इस्तेमाल होता है।It is used to define abstract methods in a class.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 22उत्तर app में
object बनाने में constructor की भूमिका क्या है?
What is the role of constructors in object creation?
  1. Aconstructor object के लिए memory देता है।Constructors allocate memory for the object.
  2. Bconstructor object के variable initialize करता है।Constructors initialize the object variables.
  3. Cconstructor object की memory मुक्त करता है।Constructors release memory for the object.
  4. Dconstructor class के method तय करता है।Constructors define the class methods.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 23उत्तर app में
निम्नलिखित में से कौन-सा कथन जावा में Person class का object सही ढंग से बनाता है?
Which of the following statements correctly creates an object of class Person in Java?
  1. APerson myPerson = Person();
  2. BPerson myPerson = new Person();
  3. CPerson myPerson = create Person();
  4. DPerson myPerson = allocate Person();
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 24उत्तर app में
जावा में object बनाते समय new keyword क्या करता है?
What does the new keyword do when used for object creation in Java?
  1. Acompile time पर object के लिए memory देता है।Allocates memory for the object at compile time.
  2. Bकिसी constructor को बुलाए बिना object के variable initialize कर देता है।Initializes the object variables without calling any constructor.
  3. Cobject को initialize करने के लिए constructor को बुलाता है।Invokes the constructor to initialize the object.
  4. Dobject की memory मुक्त कर देता है।Releases memory for the object.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 25उत्तर app में
मान लीजिए Student एक class है। निम्नलिखित कोड से कितने reference variable और कितने object बनते हैं?
Student studentName, studentId;
studentName = new Student();
Student stud_class = new Student();
Given that Student is a class, how many reference variables and objects are created by the following code?
Student studentName, studentId;
studentName = new Student();
Student stud_class = new Student();
  1. Aतीन reference variable और दो object बनते हैं।Three reference variables and two objects are created.
  2. Bदो reference variable और दो object बनते हैं।Two reference variables and two objects are created.
  3. Cएक reference variable और दो object बनते हैं।One reference variable and two objects are created.
  4. Dतीन reference variable और तीन object बनते हैं।Three reference variables and three objects are created.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 27उत्तर app में
____ को कंप्यूटर memory का वह विभाजित क्षेत्र माना जाता है जिसमें डेटा और उस डेटा तक पहुँचने वाली क्रियाओं का समूह रहता है।
____ is considered to be a partitioned area of computer memory that stores data and a set of operations that can access that data.
  1. AClasses
  2. BObjects
  3. CVariables
  4. DFunctions
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 30उत्तर app में
जावा में instanceof operator का उद्देश्य क्या है?
What is the purpose of the instanceof operator in Java?
  1. Aएक ही class के दो instance की तुलना करने के लिएTo compare two instances of the same class
  2. Bयह देखने के लिए कि object null है या नहींTo check if an object is null
  3. Cयह जाँचने के लिए कि object किसी ख़ास class प्रकार का है या नहींTo test if an object is of a particular class type
  4. Dobject के instance variable तक पहुँचने के लिएTo access instance variables of an object
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
OOPs Concept - Object in java के सभी 50 प्रश्न — उत्तर, हल और अभ्यास

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

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

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

Programmer Java के दूसरे topics

सभी topics