पहले 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)); } }
Atrue
Bfalse
CCompilation error
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?
Aobject की deep copy बनाती है।Creates a deep copy of the object.
Bobject की shallow copy बनाती है।Creates a shallow copy of the object.
Cदो object की बराबरी जाँचती है।Compares two objects for equality.
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?
Aवह अपने आप Object class को extend करती है।It extends the Object class implicitly.
Bcompilation error आ जाती है।It throws a compilation error.
Cवह अपने आप Class class को extend करती है।It extends the Class class implicitly.
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?
Aprint()
Bdisplay()
CtoString()
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?
Aobject को string रूप में बदलती है।Converts an object to a string representation.
Bदो object की बराबरी जाँचती है।Compares two objects for equality.
Cobject का hash code लौटाती है।Returns the hash code of the object.
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?
Ahash()
Bhashcode()
ChashCode()
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?
Aयह memory के पते की तुलना करता है।It compares memory addresses.
Bयह object के content की तुलना करता है।It compares the contents of the objects.
Cयह NullPointerException फेंकता है।It throws a NullPointerException.
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?
Aequals()
BhashCode()
CtoString()
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?
AMain
BSuper
CObject
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()); } }
A5
B10
CCompilation error
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 — अभ्यास, हल और आपकी प्रगति
निम्नलिखित जावा कोड का 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()); } }
निम्नलिखित कोड पर विचार कीजिए। इसका 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()); } }
निम्नलिखित जावा कोड क्या करता है? 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()); } }
मान लीजिए 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();
Aतीन reference variable और दो object बनते हैं।Three reference variables and two objects are created.
Bदो reference variable और दो object बनते हैं।Two reference variables and two objects are created.
Cएक reference variable और दो object बनते हैं।One reference variable and two objects are created.
Dतीन reference variable और तीन object बनते हैं।Three reference variables and three objects are created.