पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 20 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
नीचे दिया गया जावा कोड क्या दर्शाता है? 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().
प्रश्न 2उत्तर + हल
जावा में डीमन थ्रेड (daemon thread) और कचरा बीनने (garbage collection) का क्या संबंध है?
In Java, what is the relationship between daemon threads and garbage collection?
Aडीमन थ्रेड कचरा बीनने में भाग नहीं ले सकते।Daemon threads cannot participate in garbage collection.
Bकचरा बीनते समय डीमन थ्रेड अपने आप चल पड़ते हैं।Daemon threads are automatically invoked during garbage collection.
Cडीमन थ्रेड कचरा बीनना होने ही नहीं देते।Daemon threads prevent garbage collection from occurring.
Dकचरा बीनते समय डीमन थ्रेड अपने आप रोक दिए जाते हैं।Daemon threads are automatically paused during garbage collection.
सही उत्तर / Answer: (B)
हल / Solution
जावा में कचरा बीनने का काम (garbage collection) JVM ख़ुद एक डीमन थ्रेड (daemon thread) से कराता है; प्रोग्राम उसे बुलाता नहीं, वह पीछे अपने आप चलता रहता है। इसलिए दूसरा विकल्प सही है। पहला विकल्प उलटा है, क्योंकि यह काम डीमन थ्रेड ही करते हैं। तीसरा भी ग़लत है, डीमन थ्रेड कचरा बीनने से रोकते नहीं। चौथा भी ग़लत है; कचरा बीनने के लिए डीमन थ्रेड ख़ास तौर पर रोके नहीं जाते।
In Java the garbage collector itself is run by the JVM on a daemon thread; the program never calls it, and it simply works in the background. That is why the second option is right. The first option is the reverse, since this work is exactly what daemon threads do. The third is wrong because daemon threads do not stop collection from happening, and they are not specially paused for it either.
प्रश्न 3उत्तर + हल
Java में programmer के बनाए thread की default हैसियत क्या होती है?
In Java, what is the default status of a thread created by the programmer?
AdaemonDaemon
Bnon-daemonNon-daemon
CinterruptedInterrupted
DbackgroundBackground
सही उत्तर / Answer: (B)
हल / Solution
नया thread अपनी हैसियत बनाने वाले से लेता है। program का main thread user thread यानी non-daemon होता है, इसलिए उससे बना हर thread भी non-daemon ही रहता है। JVM तभी बंद होता है जब सारे user thread अपना काम पूरा कर लें। daemon बनाना हो तो start() से पहले setDaemon(true) लिखिए। interrupted कोई हैसियत नहीं, वह तो एक निशान है, और background नाम की कोई श्रेणी Java में है ही नहीं।
A new thread takes its status from the thread that created it. The main thread of a program is a user thread, that is non daemon, so every thread you create from it is non daemon as well. The JVM stays alive until all user threads have finished their work. To get a daemon you must call setDaemon(true) before start(). Interrupted is a flag, and background is not a Java category.
प्रश्न 4उत्तर + हल
Java में कोई daemon thread नया thread बनाए तो क्या होता है?
What happens if a daemon thread creates a new thread in Java?
Aनया thread अपने बनाने वाले का daemon होना विरासत में पा जाता है।The new thread inherits the daemon status of its parent thread.
Bनया thread daemon हो ही नहीं सकता।The new thread cannot be a daemon thread.
Cनया thread user thread बन जाता है।The new thread becomes a user thread.
Dनया thread हमेशा सबसे ऊँची प्राथमिकता पा जाता है।The new thread always gets the maximum priority.
सही उत्तर / Answer: (A)
हल / Solution
Java में नया thread अपना daemon होना बनाने वाले thread से विरासत में लेता है। इसलिए daemon thread के भीतर बना thread भी daemon ही रहेगा, जब तक start() से पहले setDaemon(false) लिखकर उसे बदल न दिया जाए। यही वजह है कि दूसरा और तीसरा विकल्प ग़लत हैं। प्राथमिकता भी विरासत में मिलती है, पर सबसे ऊँची अपने आप नहीं मिल जाती, इसीलिए चौथा विकल्प भी ग़लत है।
In Java a new thread inherits its daemon flag from the thread that created it. So a thread created inside a daemon thread is itself a daemon, unless you call setDaemon(false) on it before start(). That rules out the second and the third options. Priority is inherited as well, but the new thread certainly does not jump to the maximum priority, which is why the last option is wrong.
प्रश्न 5उत्तर + हल
daemon thread के बारे में इनमें से कौन-सी बात ग़लत है?
Which of the following statements about daemon threads is false?
Adaemon thread garbage collection जैसे पीछे के कामों के लिए ठीक रहते हैं।Daemon threads are suitable for background tasks like garbage collection.
Bकिसी thread को daemon बनाने के लिए start() से पहले setDaemon(true) लिखना पड़ता है।A thread becomes a daemon only if setDaemon(true) is called before start().
Cdaemon thread का काम JVM बंद होने से पहले पूरा हो ही जाता है, यह पक्का है।Daemon threads are guaranteed to complete their tasks before the JVM exits.
Ddaemon thread उन कामों के लिए काम आते हैं जो program के लिए ज़रूरी नहीं हैं।Daemon threads can be used for tasks that are not critical for the application.
सही उत्तर / Answer: (C)
हल / Solution
तीसरी बात ग़लत है। सारे user thread ख़त्म होते ही JVM बंद हो जाता है और daemon thread वहीं काट दिए जाते हैं, चाहे उनका काम अधूरा ही क्यों न हो। इसलिए पूरा होने की कोई गारंटी है ही नहीं। बाक़ी तीनों बातें सही हैं — पीछे के सहायक कामों के लिए वे बने ही हैं, setDaemon(true) start() से पहले लगाना ज़रूरी है, और ग़ैर-ज़रूरी कामों के लिए वे बिलकुल ठीक रहते हैं।
The third statement is the false one. Once the last user thread ends, the JVM shuts down and the daemon threads are cut off wherever they are, so nothing guarantees they will finish. The other three are true, because daemons suit background chores, setDaemon(true) must be called before start() for a thread to become one, and they are meant for work the application can live without.
प्रश्न 6उत्तर + हल
Java में JVM बंद होने पर daemon thread का क्या होता है?
In Java, what happens to daemon threads when the JVM exits?
Adaemon thread अपने आप समाप्त हो जाते हैं।Daemon threads are automatically terminated.
Bdaemon thread JVM के दोबारा चलने तक रोक दिए जाते हैं।Daemon threads are suspended until the JVM is restarted.
Cdaemon thread JVM बंद होने के बाद भी चलते रहते हैं।Daemon threads continue running even after the JVM exits.
Ddaemon thread तब तक रुके रहते हैं जब तक उन्हें हाथ से दोबारा न चलाया जाए।Daemon threads are paused until they are manually resumed.
सही उत्तर / Answer: (A)
हल / Solution
सारे user thread ख़त्म होते ही JVM बंद होने लगता है, और उसी घड़ी बचे हुए daemon thread अपने आप समाप्त कर दिए जाते हैं। उनका काम बीच में अधूरा रह जाए तो भी कोई इंतज़ार नहीं होता, यहाँ तक कि finally खंड के चलने की भी गारंटी नहीं रहती। इसीलिए ज़रूरी काम daemon thread को कभी मत सौंपिए। JVM बंद होने के बाद कोई thread चल नहीं सकता, और उसे हाथ से जगाने का सवाल ही नहीं उठता।
As soon as the last user thread finishes, the JVM begins to shut down and every daemon thread still alive is killed at that moment. Its work may be left half done, and even a finally block is not guaranteed to run. That is why important work should never be handed to a daemon thread. No thread can keep running after the JVM has gone, and none of them can be resumed by hand.
प्रश्न 7उत्तर + हल
Java में daemon thread किसे कहते हैं?
What is a daemon thread in Java?
Aऐसा thread जो पीछे चलता रहता है और दूसरे thread से अलग अपना काम करता है।A thread that executes in the background and performs tasks independently of other threads.
Bऐसा thread जिसे JVM अलग से समाप्त करता है।A thread that is explicitly terminated by the JVM.
Cऐसा thread जिसकी प्राथमिकता बाक़ी सब से ऊँची होती है।A thread that has a higher priority than other threads.
Dऐसा thread जिसे सिर्फ़ operating system बना सकता है।A thread that can only be created by the operating system.
सही उत्तर / Answer: (A)
हल / Solution
Daemon thread पीछे रहकर सहायक काम करता है, जैसे कचरा बीनने वाला garbage collector। वह मुख्य काम का हिस्सा नहीं होता, इसलिए program उसके पूरा होने का इंतज़ार नहीं करता। किसी thread को daemon बनाने के लिए start() से पहले setDaemon(true) लिखना पड़ता है, यानी programmer भी बना सकता है। उसकी प्राथमिकता अपने आप ऊँची नहीं होती। और JVM उसे अलग से चुनकर नहीं मारता, वह तो पूरा JVM बंद होते समय अपने आप चला जाता है।
A daemon thread runs in the background doing supporting work, such as garbage collection. It is not part of the main job, so the program never waits for it to finish. You make one by calling setDaemon(true) before start(), which means a programmer can certainly create them. Its priority is not raised automatically, and the JVM does not pick it out for termination, since it simply goes when the whole JVM shuts down.
प्रश्न 8उत्तर + हल
Java में deadlock का मुख्य कारण क्या होता है?
What is the primary cause of deadlocks in Java?
Amemory का कम मिलनाInsufficient memory allocation
Bthread scheduling का ठीक न होनाInadequate thread scheduling
Cसंसाधनों का ग़लत ढंग से बाँटा जानाImproper resource allocation
Dsynchronization का हद से ज़्यादा इस्तेमालExcessive use of synchronization
सही उत्तर / Answer: (C)
हल / Solution
Deadlock की जड़ यही है कि संसाधन ग़लत ढंग से बाँटे और माँगे जाते हैं। एक thread पहला lock पकड़े-पकड़े दूसरा माँगता है और दूसरा thread उलटे क्रम में, बस घेरा बन गया। memory कम पड़ने पर OutOfMemoryError मिलता है, deadlock नहीं। scheduler सिर्फ़ बारी तय करता है, घेरा नहीं बनाता। synchronization ज़्यादा होने से रफ़्तार घटती है, पर असली दोष क्रम का है, गिनती का नहीं।
Deadlock grows out of the way resources are asked for and held. One thread holds the first lock and asks for the second while another does it the other way round, and the ring closes. Shortage of memory gives an OutOfMemoryError, not a deadlock. The scheduler only decides whose turn it is and cannot build the ring. Heavy synchronization slows a program, but the real fault lies in the ordering.
प्रश्न 9उत्तर + हल
Java में deadlock को program के भीतर से पकड़ने के लिए कौन-सी विधि दी गई है?
Which method is used to detect deadlocks programmatically in Java?
AThread वर्ग की checkDeadlock()checkDeadlock() in Thread class
BRuntime वर्ग की detectDeadlock()detectDeadlock() in Runtime class
CSystem वर्ग की findDeadlock()findDeadlock() in System class
DJava ऐसी कोई सीधी विधि देता ही नहीं।There is no direct method provided by Java to detect deadlocks.
सही उत्तर / Answer: (D)
हल / Solution
Java की मूल कक्षाओं में deadlock पकड़ने की कोई सीधी विधि है ही नहीं। Thread में checkDeadlock(), Runtime में detectDeadlock() और System में findDeadlock() — ये तीनों नाम गढ़े हुए हैं और Java में मिलेंगे ही नहीं। असल में deadlock ढूँढ़ने के लिए thread dump देखा जाता है या jconsole तथा jstack जैसे औज़ार लगाए जाते हैं। सबसे अच्छा तरीक़ा तो रोकथाम है — lock हमेशा एक ही क्रम में लीजिए, ताकि घेरा बने ही नहीं।
The core Java classes offer no direct call for finding a deadlock. The names in the other options are invented, since Thread has no checkDeadlock(), Runtime has no detectDeadlock() and System has no findDeadlock(). In practice you look at a thread dump or use tools such as jconsole and jstack to see which threads are stuck. The best cure is prevention, so always take your locks in one fixed order.
प्रश्न 10उत्तर + हल
deadlock बनने के लिए इनमें से कौन-सी शर्तें एक साथ पूरी होनी चाहिए?
Which of the following conditions must be satisfied for a deadlock to occur?
Amutual exclusion, hold and wait, no preemption और circular waitMutual exclusion, hold and wait, no preemption, and circular wait.
Bno mutual exclusion, hold and wait, no preemption और circular waitNo mutual exclusion, hold and wait, no preemption, and circular wait.
Cmutual exclusion, no hold and wait, no preemption और circular waitMutual exclusion, no hold and wait, no preemption, and circular wait.
Dmutual exclusion, hold and wait, preemption और no circular waitMutual exclusion, hold and wait, preemption, and no circular wait.
सही उत्तर / Answer: (A)
हल / Solution
Deadlock के लिए चारों शर्तें एक साथ चाहिए। mutual exclusion यानी संसाधन एक समय में एक ही thread के पास रहे। hold and wait यानी एक lock पकड़े हुए दूसरे की माँग करना। no preemption यानी lock ज़बरदस्ती छीना न जा सके। और circular wait यानी इंतज़ार का घेरा पूरा बन जाना। इनमें से एक भी शर्त तोड़ दीजिए तो deadlock बनेगा ही नहीं, इसीलिए बाक़ी तीनों विकल्प ग़लत हैं।
All four conditions must hold at the same time. Mutual exclusion means a resource is held by one thread only. Hold and wait means a thread keeps one lock while asking for another. No preemption means a lock cannot be taken away by force. Circular wait means the chain of waiting closes into a ring. Break any single one of them and the deadlock cannot form, so the other options fail.
आगे के 20 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
Aजब कोई thread किसी दूसरे के lock छोड़ने के इंतज़ार में अनिश्चित काल तक रुका रहे।It occurs when a thread is suspended indefinitely waiting for another thread to release a lock.
Bजब कोई thread दूसरे के पास पड़े संसाधन के इंतज़ार में रुकी हुई अवस्था में चला जाए।It happens when a thread enters a blocked state because it has to wait indefinitely for a resource held by another thread.
Cजब दो या अधिक thread आगे बढ़ ही न पाएँ क्योंकि हर एक दूसरे के lock छोड़ने की राह देख रहा है।It occurs when two or more threads are unable to proceed because each is waiting for the other to release a lock.
Dजब कोई thread अचानक बीच में ही समाप्त हो जाए।It is a situation where a thread terminates unexpectedly.
Aयह thread का चलना तब तक रोक देती है जब तक कोई दूसरा thread उसे सूचित न करे।It suspends the execution of a thread until it is notified by another thread.
Bयह किसी दूसरे thread को चलने का संकेत भेजती है।It sends a signal to another thread to resume its execution.
Cयह thread का चलना हमेशा के लिए रोक देती है।It stops the execution of a thread indefinitely.
Dयह प्रतीक्षा के दौरान object का lock पकड़े रहती है।It keeps holding the lock on the object while waiting.