पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 11 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
Java में क्या कोई static method उसी class के non-static method को बुला सकता है?
In Java, can a static method call a non-static method of the same class?
Aहाँ, पर केवल तब जब non-static method को final घोषित किया गया हो।Yes, but only if the non-static method is declared as final.
Bनहीं, static methods, non-static methods को सीधे नहीं बुला सकते।No, static methods cannot directly call non-static methods.
Cहाँ, static methods, non-static methods को सीधे बुला सकते हैं।Yes, static methods can directly call non-static methods.
Dहाँ, पर केवल तब जब non-static method को private घोषित किया गया हो।Yes, but only if the non-static method is declared as private.
सही उत्तर / Answer: (B)
हल / Solution
non-static method किसी object पर चलता है, उसे this चाहिए। static method के पास कोई object या this नहीं होता। इसलिए static method के अंदर सीधे show() लिखने पर compile error आती है। रास्ता यह है कि पहले object बनाइए, फिर obj.show() बुलाइए। method को final या private बनाने से कुछ नहीं बदलता, क्योंकि वह फिर भी object का ही method रहता है। इसलिए विकल्प 2 सही है, सीधे बुलाना संभव नहीं।
A non-static method runs on an object and needs this. A static method has no object and no this. So writing show() directly inside a static method gives a compile error. The way around it is to create an object first and then call obj.show(). Making the method final or private changes nothing, because it is still an object's method. So option 2 is correct: a direct call is not possible.
प्रश्न 2उत्तर + हल
Java में static method दर्शाने के लिए निम्नलिखित में से कौन-सा कीवर्ड प्रयोग होता है?
Which of the following keywords is used to denote a static method in Java?
Afinal
Babstract
Cstatic
Dvoid
सही उत्तर / Answer: (C)
हल / Solution
method के आगे static लिखने से वह static method बन जाता है, जैसे public static void main। ऐसा method class का होता है और बिना object के बुलाया जा सकता है। final method को override होने से रोकता है। abstract method की सिर्फ़ घोषणा होती है, उसका शरीर subclass लिखती है। void केवल यह बताता है कि method कोई मान नहीं लौटाता, यह return type है। इसलिए सही उत्तर static है।
Writing static before a method makes it a static method, as in public static void main. Such a method belongs to the class and can be called without an object. final stops a method from being overridden. An abstract method has only a declaration, and a subclass writes its body. void only says that the method returns nothing; it is a return type. So the correct answer is static.
प्रश्न 3उत्तर + हल
क्या Java class में static block class के non-static सदस्यों को access कर सकता है?
In a Java class, can a static block access non-static members of the class?
Aहाँ, पर केवल तब जब वे सदस्य static block से पहले initialize हो चुके हों।Yes, but only if those members are initialized before the static block.
Bनहीं, static block केवल class के static सदस्यों को access कर सकता है।No, static blocks can only access static members of the class.
Cहाँ, static block class के static और non-static दोनों सदस्यों को access कर सकता है।Yes, static blocks can access both static and non-static members of the class.
Dनहीं, static block class के किसी भी सदस्य को access नहीं कर सकता।No, static blocks cannot access any members of the class.
सही उत्तर / Answer: (B)
हल / Solution
static block class load होते समय चलता है। उस समय कोई object ज़रूरी नहीं होता और this भी नहीं होता। इसलिए static block सीधे केवल static चर और static method इस्तेमाल कर सकता है। non-static सदस्य लिखने पर compile error आती है, चाहे वे पहले initialize हों या नहीं। static सदस्यों तक यह पहुँच सकता है, इसलिए विकल्प 4 ग़लत है। non-static सदस्य चाहिए तो block के अंदर object बनाना पड़ता है। इसलिए विकल्प 2 सही है।
A static block runs when the class loads. At that moment no object is needed and there is no this reference. So a static block can directly use only static variables and static methods. Writing a non-static member there is a compile error, whether or not it was initialized earlier. It can reach static members, so option 4 is wrong. To use a non-static member, you must create an object inside the block. So option 2 is correct.
प्रश्न 4उत्तर + हल
Java में static चर कब initialize होते हैं?
When do static variables get initialized in Java?
Acompile time परAt compile time
Bruntime पर, उनके पहले प्रयोग से ठीक पहलेAt runtime, just before their first usage
Cजब class का object बनाया जाता हैWhen an object of the class is created
Dजब class memory में load होती हैWhen the class is loaded into memory
सही उत्तर / Answer: (D)
हल / Solution
static चर class का हिस्सा है, इसलिए वह तब initialize होता है जब JVM class को memory में load करके तैयार करती है। यह काम एक ही बार होता है। compile time पर सिर्फ़ bytecode बनता है, चरों को memory नहीं मिलती। object बनने पर instance चर बनते हैं; static चर तब तक तैयार हो चुके होते हैं। static चर हर प्रयोग से पहले अलग से नहीं बनता, वह class के साथ बनता है। इसलिए विकल्प 4 सही है।
A static variable is part of the class, so it is initialized when the JVM loads the class into memory and prepares it. This happens only once. At compile time only bytecode is produced; variables get no memory then. Creating an object sets up instance variables; static variables are ready before that. A static variable is not set up separately before each use; it comes with the class. So option 4 is correct.
प्रश्न 5उत्तर + हल
निम्नलिखित कोड का output क्या है?
public class Example {
static int x = 5;
static {
x += 5;
}
public static void main(String[] args) {
System.out.println(x);
}
}
What is the output of the following code?
public class Example {
static int x = 5;
static {
x += 5;
}
public static void main(String[] args) {
System.out.println(x);
}
}
A0
B5
C10
D15
सही उत्तर / Answer: (C)
हल / Solution
class load होते समय static हिस्से लिखे क्रम में चलते हैं। पहले static int x = 5 से x का मान 5 होता है। फिर static block चलता है और x += 5 से x का मान 10 हो जाता है। इसके बाद main चलता है और 10 छापता है। 0 तब आता जब x को कोई मान न मिलता। 5 तब आता जब static block न चलता। 15 के लिए block दो बार चलना पड़ता, जो नहीं होता।
When the class loads, static parts run in the order they are written. First, static int x = 5 sets x to 5. Then the static block runs, and x += 5 makes x equal to 10. After that, main runs and prints 10. The output 0 would need x to have no value at all. The output 5 would need the static block to be skipped. 15 would need the block to run twice, which never happens.
प्रश्न 6उत्तर + हल
Java में static imports के बारे में कौन-सा कथन सत्य है?
Which of the following is true about static imports in Java?
Aये किसी class के static सदस्यों को सीधे import करने देते हैं।They allow importing static members of a class directly.
Bइनका प्रयोग बाहरी libraries से classes import करने के लिए होता है।They are used to import classes from external libraries.
CJava में इनकी अनुमति नहीं है।They are not allowed in Java.
Dये केवल non-static सदस्यों को import करने के लिए लागू होते हैं।They are only applicable for importing non-static members.
सही उत्तर / Answer: (A)
हल / Solution
Java 5 से static import आया। इससे किसी class के static सदस्य सीधे import हो जाते हैं। जैसे import static java.lang.Math.*; लिखने के बाद Math.sqrt की जगह सिर्फ़ sqrt लिख सकते हैं। किसी भी library से class लाने का काम सामान्य import करता है, static import नहीं। यह Java में पूरी तरह मान्य है, इसलिए तीसरा विकल्प ग़लत है। यह केवल static सदस्यों के लिए है, non-static के लिए नहीं। इसलिए विकल्प 1 सही है।
Static import was added in Java 5. It lets you import the static members of a class directly. For example, after import static java.lang.Math.*; you can write sqrt instead of Math.sqrt. Bringing in classes from any library is done by a normal import, not a static import. It is fully allowed in Java, so option 3 is wrong. It works only for static members, not non-static ones. So option 1 is correct.
प्रश्न 7उत्तर + हल
Java में static context से non-static सदस्य को access करने की कोशिश करने पर क्या होता है?
What happens if you try to access a non-static member from a static context in Java?
Aइससे compile-time error आती है।It results in a compile-time error.
Bइससे runtime error आती है।It results in a runtime error.
Cयह non-static सदस्य को अपने-आप static में बदल देता है।It automatically converts the non-static member to a static one.
Dयह access किए जा रहे सदस्य के प्रकार पर निर्भर करता है।It depends on the type of member being accessed.
सही उत्तर / Answer: (A)
हल / Solution
static context, जैसे static method या static block, किसी object से बँधा नहीं होता और वहाँ this भी नहीं होता। इसलिए non-static चर या method को सीधे लिखने पर compiler रोक देता है और error देता है। यह ग़लती compile के समय ही पकड़ी जाती है, runtime तक नहीं पहुँचती। Java किसी सदस्य को अपने-आप static नहीं बनाती। चर हो या method, नियम एक ही है। इसलिए विकल्प 1 सही है।
A static context, such as a static method or static block, is not tied to any object and has no this reference. So when you use a non-static variable or method directly there, the compiler stops you with an error. The mistake is caught at compile time and never reaches runtime. Java never turns a member static on its own. The rule is the same for variables and methods. So option 1 is correct.
प्रश्न 8उत्तर + हल
Java में static block कब execute होता है?
When is a static block executed in Java?
Aजब class का instance बनाया जाता हैWhen an instance of the class is created
Bजब class memory में load होती हैWhen the class is loaded into memory
Cजब कोई method बुलाया जाता हैWhen a method is invoked
Dजब कोई object garbage collect होता हैWhen an object is garbage collected
सही उत्तर / Answer: (B)
हल / Solution
static block class के साथ जुड़ा होता है। JVM जब class को memory में load करके initialize करती है, तब static block एक ही बार चलता है। इसके बाद चाहे कितने भी object बनें, यह दोबारा नहीं चलता। हर object बनने पर constructor या instance block चलता है, static block नहीं। method बुलाने पर सिर्फ़ उस method का कोड चलता है। garbage collection का static block से कोई संबंध नहीं। इसलिए विकल्प 2 सही है।
A static block is tied to the class. When the JVM loads the class into memory and initializes it, the static block runs exactly once. After that it never runs again, no matter how many objects are created. Creating an object runs the constructor or an instance block, not the static block. Calling a method only runs that method. Garbage collection has nothing to do with static blocks. So option 2 is correct.
प्रश्न 9उत्तर + हल
Java में static चरों को और किस नाम से जाना जाता है?
In Java, static variables are also known as:
AInstance variables
BClass variables
CLocal variables
DMethod variables
सही उत्तर / Answer: (B)
हल / Solution
static चर पूरी class का होता है और उसकी एक ही copy सब object बाँटते हैं, इसलिए इसे class variable कहते हैं। instance variable बिना static वाला चर है, जिसकी हर object में अलग copy होती है। local variable किसी method या block के अंदर बनता है और वहीं ख़त्म हो जाता है। local चर पर static लग ही नहीं सकता। method variable अलग से कोई शब्द नहीं, यह local चर ही है। इसलिए उत्तर Class variables है।
A static variable belongs to the whole class, and all objects share its single copy, so it is called a class variable. An instance variable is a non-static field; each object has its own copy. A local variable is created inside a method or block and disappears when it ends. static cannot be used on a local variable at all. Method variable is not a separate term; it just means a local variable. So the answer is Class variables.
प्रश्न 10उत्तर + हल
Java में निम्नलिखित में से किसे static घोषित किया जा सकता है?
Which of the following can be declared as static in Java?
Aचर (Variables)Variables
BMethods
CNested classes
Dउपरोक्त सभीAll of the above
सही उत्तर / Answer: (D)
हल / Solution
Java में static कई चीज़ों पर लगता है। static चर class का साझा चर होता है। static method बिना object के बुलाया जाता है, जैसे main। किसी class के अंदर बनी nested class भी static हो सकती है; तब उसे बाहरी class का object बनाए बिना इस्तेमाल किया जा सकता है। इसके अलावा static block भी होता है। केवल सबसे बाहरी (top-level) class static नहीं हो सकती। तीनों दिए गए विकल्प सही हैं, इसलिए उत्तर "उपरोक्त सभी" है।
In Java, static can be applied to several things. A static variable is a shared class variable. A static method, like main, is called without an object. A class written inside another class, a nested class, can also be static; then it can be used without an object of the outer class. There are static blocks too. Only a top-level class cannot be static. All three given items are valid, so the answer is All of the above.
आगे के 11 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
Aयह बताता है कि चर या method class का है, class के instances का नहीं।It denotes that the variable or method belongs to the class, not to instances of the class.
Bयह बताता है कि चर या method केवल उसी package के अंदर access हो सकता है।It denotes that the variable or method can only be accessed within the same package.
Cयह बताता है कि चर या method dynamic है और program चलते समय बदल सकता है।It signifies that the variable or method is dynamic and can change during program execution.
Dयह बताता है कि चर या method अपरिवर्तनीय (immutable) है।It indicates that the variable or method is immutable.
Java में static कीवर्ड के बारे में कौन-सा कथन सत्य है?
Which of the following is true about the static keyword in Java?
Aइसका प्रयोग instance चरों को access करने के लिए होता है।It can be used to access instance variables.
Bइसका प्रयोग non-static methods को access करने के लिए होता है।It can be used to access non-static methods.
Cइसका प्रयोग ऐसे methods घोषित करने के लिए होता है जो superclass के methods को override करते हैं।It can be used to declare methods that override superclass methods.
Dइसका प्रयोग ऐसे चर घोषित करने के लिए होता है जो class के सभी instances में साझा होते हैं।It can be used to declare variables that are shared among all instances of the class.