random access file में file pointer को किसी ख़ास स्थान पर ले जाने के लिए कौन-सी method प्रयोग होती है?
Which method is used to move the file pointer to a specific position in a random access file?
- AmoveTo()
- BjumpTo()
- Cseek()
- DmovePointer()
सही उत्तर / Answer: (C)
हल / Solution
RandomAccessFile की seek(long pos) method file pointer को file की शुरुआत से pos byte की दूरी पर रख देती है। अगला read या write वहीं से होता है। pos file की लंबाई से आगे भी हो सकता है। तब file की लंबाई तभी बढ़ती है जब वहाँ कुछ लिखा जाए। moveTo(), jumpTo() और movePointer() नाम की कोई method RandomAccessFile में नहीं है। इसलिए सही उत्तर seek() है।
The seek(long pos) method of RandomAccessFile places the file pointer pos bytes from the start of the file, and the next read or write happens there. The position may even be beyond the current end of the file; the file length grows only when something is written there. RandomAccessFile has no methods named moveTo(), jumpTo() or movePointer(). So seek(), option 3, is the correct method for moving the file pointer.
नीचे दिया गया जावा कोड क्या दर्शाता है?
class MyThread extends Thread {
public void run() {
System.out.println("Thread running...");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
What does the following Java code snippet demonstrate?
class MyThread extends Thread {
public void run() {
System.out.println("Thread running...");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
- AThread क्लास को extend करके और run() method को ओवरराइड करके नया थ्रेड बनाना।Creating a new thread by extending the Thread class and overriding the run() method.
- BRunnable interface लागू करके और run() method को ओवरराइड करके नया थ्रेड बनाना।Creating a new thread by implementing the Runnable interface and overriding the run() method.
- Cमुख्य थ्रेड (main thread) शुरू करना।Starting the main thread.
- Dदूसरे थ्रेड के ख़त्म होने का इंतज़ार करना।Waiting for another thread to finish its execution.
सही उत्तर / Answer: (A)
हल / Solution
यहाँ MyThread क्लास Thread को extend करती है और उसके run() method को ओवरराइड (override) करती है। main में उसकी वस्तु बनाकर start() बुलाया गया है, जिससे JVM नया थ्रेड बनाता है और उसी पर run() चलता है। इसलिए पहला विकल्प सही है। दूसरा विकल्प Runnable लागू करने वाला तरीक़ा बताता है, जो इस कोड में नहीं है। मुख्य थ्रेड तो JVM ख़ुद शुरू करता है, और इंतज़ार कराने के लिए join() चाहिए होता।
Here the class MyThread extends Thread and overrides its run() method. In main an object of it is created and start() is called, so the JVM makes a new thread and runs the overridden code on it. That is why the first option is correct. The second option describes the Runnable route, which this code does not use. The main thread is started by the JVM itself, and waiting for a thread would need join().
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.
Java की कौन-सी सुविधा किसी class को अपने गुणों (attributes) और method पर पूरा नियंत्रण देती है, जिससे encapsulation लागू होता है?
Which Java feature allows a class to have total control over its attributes and methods, enforcing encapsulation?
- Aइनहेरिटेंस (Inheritance)Inheritance
- Bपॉलीमॉर्फ़िज़्म (Polymorphism)Polymorphism
- Cएक्सेस मॉडिफ़ायर (Access modifiers)Access modifiers
- Dएक्सेसर और म्यूटेटर method (Accessor and mutator methods)Accessor and mutator methods
सही उत्तर / Answer: (C)
हल / Solution
access modifier यानी private, default, protected और public तय करते हैं कि class का कौन-सा गुण या method कहाँ से दिख सकता है। इन्हीं से class अपने गुणों और method, दोनों पर पूरा नियंत्रण रखती है। getter-setter भी मदद करते हैं, पर वे सिर्फ़ गुणों तक पहुँच देते हैं। वे भी तभी काम आते हैं जब गुण private हों, यानी वे भी access modifier पर टिके हैं। inheritance और polymorphism का पहुँच-नियंत्रण से सीधा संबंध नहीं है।
Access modifiers, namely private, default, protected and public, decide where each attribute or method of a class can be seen. With them, a class controls both its attributes and its methods. Getters and setters help too, but they only cover attributes, and they work only because the attributes are made private by an access modifier. Inheritance and polymorphism are not directly about access control. So option 3 is correct.
क्या marker interface में methods हो सकते हैं?
Can a marker interface have methods?
- Aहाँ, पर implement करने वाली क्लासों को उन्हें implement करना होगा।Yes, but they must be implemented by the implementing classes.
- Bनहीं, marker interfaces में कोई method नहीं होता।No, marker interfaces do not have any methods.
- Cहाँ, पर वे abstract methods होने चाहिए।Yes, but they must be abstract methods.
- Dनहीं, marker interfaces के सभी methods अपने-आप final होते हैं।No, all methods in marker interfaces are implicitly final.
सही उत्तर / Answer: (B)
हल / Solution
Marker interface की पहचान ही यह है कि वह खाली होता है; उसमें न method होता है, न field। उसका काम सिर्फ़ क्लास पर निशान लगाना है। अगर उसमें एक भी method जोड़ दें तो वह साधारण इंटरफ़ेस बन जाता है, marker नहीं रहता; इसलिए पहला और तीसरा विकल्प ग़लत हैं। चौथा विकल्प अपने-आप में उलटा है, क्योंकि जिसमें method है ही नहीं उसके methods final कैसे होंगे। साथ ही इंटरफ़ेस के methods final हो भी नहीं सकते।
The defining feature of a marker interface is that it is empty, with no methods and no fields. Its only job is to tag a class. If you add even one method, it becomes an ordinary interface and is no longer a marker, so options 1 and 3 are wrong. Option 4 contradicts itself: an interface with no methods cannot have final methods, and interface methods can never be final anyway.
"use-a" संबंध का उपयोग करने का निम्नलिखित में से कौन-सा लाभ है?
Which of the following is an advantage of using a "use-a" relationship?
- Aइससे क्लासों के बीच मज़बूत जुड़ाव (tight coupling) बनता है।It leads to tight coupling between classes.
- Bइससे कोड की पुनः उपयोगिता (code reusability) बढ़ती है।It enhances code reusability.
- Cइससे कोडबेस का लचीलापन (flexibility) घटता है।It reduces the flexibility of the codebase.
- Dइससे क्लासों का डिज़ाइन जटिल हो जाता है।It complicates the design of the classes.
सही उत्तर / Answer: (B)
हल / Solution
"use-a" संबंध में एक क्लास वही logic दोबारा लिखने के बजाय दूसरी क्लास की बनी-बनाई methods कॉल करती है। एक ही सहायक क्लास, जैसे Calculator या Logger, कई क्लासों के काम आ जाती है। इससे कोड की पुनः उपयोगिता (code reusability) बढ़ती है। यह जुड़ाव ढीला (loose coupling) होता है। मज़बूत जुड़ाव (tight coupling), कम लचीलापन (flexibility) और जटिल डिज़ाइन तो नुक़सान हैं, लाभ नहीं। इसलिए विकल्प 2 सही है।
In a "use-a" relationship a class calls the ready-made methods of another class instead of writing that logic again. The same helper class, such as a Calculator or Logger, can then serve many classes, so code reusability increases. The link is loose, so it does not create tight coupling. Tight coupling, lower flexibility and a more complex design are disadvantages, not advantages. So option 2 is correct.
जावा में लॉजिकल ऑपरेटर (logical operator) कौन से हैं?
Which of the following are the logical operators in Java?
- A&&
- B||
- CAll of the above
- DNone of the above
सही उत्तर / Answer: (C)
हल / Solution
जावा के लॉजिकल (logical) ऑपरेटर पहले और दूसरे विकल्प, दोनों हैं। पहला लॉजिकल AND है — दोनों शर्तें सही हों तभी true देता है। दूसरा लॉजिकल OR है — कोई एक शर्त सही होने पर ही true मिल जाता है। दोनों शॉर्ट-सर्किट (short-circuit) तरीक़े से चलते हैं, यानी नतीजा पहले हिस्से से तय हो जाए तो दूसरा हिस्सा जाँचा ही नहीं जाता। चूँकि दोनों लॉजिकल हैं, इसलिए तीसरा विकल्प सही है।
Both the first and the second option are logical operators in Java. The first one is logical AND and returns true only when both conditions are true. The second is logical OR and returns true as soon as any one condition is true. Both work in a short-circuit way, so the second condition is not even checked when the first already decides the result. Since both belong to the group, the third option is correct.
निम्नलिखित जावा कोड पर विचार कीजिए। इसका 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().
निम्नलिखित में से कौन वंशानुक्रम (inheritance) का सबसे अच्छा वर्णन करता है?
Which among the following best describes Inheritance?
- Aआधार क्लास के डेटा और function को व्युत्पन्न भाग (derived segment) में इस्तेमाल करनाUsing the data and functions into derived segment
- Bकिसी programming language में पहले से बने function इस्तेमाल करनाUsing already defined functions in a programming language
- Cएक बार लिखे हुए कोड को इस्तेमाल करनाUsing the code already written once
- Dपहले लिखे हुए कोड की नक़ल कर लेनाCopying the code already written
सही उत्तर / Answer: (A)
हल / Solution
वंशानुक्रम (inheritance) का मतलब है आधार क्लास (base class) के डेटा और function को व्युत्पन्न क्लास में आगे बढ़ाकर इस्तेमाल करना — इसलिए पहला विकल्प सबसे सटीक है। पहले से बने function बुलाना तो library का साधारण उपयोग है, उसमें क्लास के बीच कोई नया संबंध नहीं बनता। एक बार लिखा कोड इस्तेमाल करना बहुत ढीली बात है, वह हर method बुलाने पर भी लागू होगी। नक़ल करना तो उलटा है, क्योंकि वहाँ कोड दो जगह हो जाता है।
Inheritance means the data and functions of a base class are carried into a derived class and used there, so the first option describes it best. Calling functions that already exist is just ordinary library use and creates no new relationship between classes. Saying you use code written once is far too loose, since that is true of any method call. Copying code is the opposite, because the code then lives in two places.
नीचे दिया गया इंटरफ़ेस देखिए:
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.