पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 15 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
निम्न Java code क्या छापेगा? System.out.println("a,b,,".split(",").length);
What will the following Java code print? System.out.println("a,b,,".split(",").length);
A4
B2
C3
D1
सही उत्तर / Answer: (B)
हल / Solution
एक argument वाले साधारण रूप में split() नतीजे के अंत में बनने वाले ख़ाली strings को हटा देता है। a,b,, पाठ टूटकर a, b और दो ख़ाली टुकड़े देता, पर अंत के दोनों ख़ाली टुकड़े फेंक दिए जाते हैं और केवल a तथा b का array बचता है, इसलिए लंबाई 2 छपेगी। पाठ के बीच पड़ने वाले ख़ाली टुकड़े हमेशा बने रहते हैं, केवल पीछे वाले हटते हैं।
With the usual one argument form, split() drops the empty strings that would stand at the end of the result. The text a,b,, would break into a, b and two empty pieces, but both trailing empty pieces are thrown away, leaving an array of just a and b, so the length printed is 2. Empty pieces falling in the middle of the text are always kept, only trailing ones go.
प्रश्न 2उत्तर + हल
निम्न Java code क्या छापेगा? String s = "Fliqi"; String t = s.substring(5); System.out.println(t.length());
What will the following Java code print? String s = "Fliqi"; String t = s.substring(5); System.out.println(t.length());
Aयह StringIndexOutOfBoundsException फेंकता हैIt throws a StringIndexOutOfBoundsException
B1
C5
D0
सही उत्तर / Answer: (D)
हल / Solution
Fliqi में पाँच characters हैं, इसलिए वैध index शून्य से चार तक हैं; पर substring शुरुआती index के रूप में string की लंबाई के बराबर मान भी स्वीकार करता है। शुरुआती index 5 देने पर नक़ल करने को कुछ बचता नहीं, इसलिए एक ख़ाली string लौटती है और उसका length() शून्य होता है। यहाँ कोई exception नहीं आता; उसके लिए index छह या उससे बड़ा चाहिए होता।
Fliqi has five characters, so its valid indexes run from 0 to 4; but substring also accepts a beginning index equal to the length of the string itself. With a beginning index of 5 there is nothing left to copy, so an empty string is returned and its length() is 0. No exception is thrown here; that would need a beginning index of six or more.
प्रश्न 3उत्तर + हल
निम्न Java code क्या छापेगा? String s = null; System.out.println("Value: " + s);
What will the following Java code print? String s = null; System.out.println("Value: " + s);
Aयह NullPointerException फेंकता हैIt throws a NullPointerException
BValue:
CValue: null
Dयह compile ही नहीं होताIt fails to compile
सही उत्तर / Answer: (C)
हल / Solution
जब null reference string जोड़ने में शामिल होता है तो Java कोई exception नहीं फेंकता; भाषा के नियम कहते हैं कि null reference को पहले null पाठ में बदला जाता है और फिर जोड़ा जाता है। इसलिए output में Value: के बाद एक स्थान और null छपता है। NullPointerException तब आता जब उसी null reference पर s.length() जैसा कोई method बुलाया जाता, क्योंकि तब object चाहिए होता है।
When a null reference takes part in string concatenation Java does not throw an exception; the rules of the language say the null reference is first turned into the four letter text null and then joined. So the output shows Value: followed by a space and null. A NullPointerException would arise only if a method such as s.length() were called on that null reference, because that needs an object.
प्रश्न 4उत्तर + हल
निम्न Java code क्या छापेगा? String a = "Java"; String b = "Ja" + a.substring(2); System.out.println(a == b);
What will the following Java code print? String a = "Java"; String b = "Ja" + a.substring(2); System.out.println(a == b);
Atrue
Bfalse
CJava
Dचलते समय exception आता हैA run-time exception is thrown
सही उत्तर / Answer: (B)
हल / Solution
यहाँ एक operand a.substring(2) है, जिसका मान चलते समय ही पता चलता है, इसलिए compiler इस व्यंजक को स्थिर मान में नहीं बदल सकता। चलते समय strings जोड़ने पर heap में हर बार एक नया String object बनता है, और ऐसा object अपने आप pool में नहीं जाता। इसलिए a और b के references अलग हैं और == से false छपेगा, जबकि a.equals(b) true होता।
Here one operand is a.substring(2), whose value is known only while the program runs, so the compiler cannot fold this expression into a constant. Joining strings at run time always produces a brand new String object on the heap, and such an object is not placed in the pool by itself. Hence a and b hold different references and == prints false, although a.equals(b) would be true.
प्रश्न 5उत्तर + हल
निम्न Java code क्या छापेगा? String a = "Fl" + "iqi"; String b = "Fliqi"; System.out.println(a == b);
What will the following Java code print? String a = "Fl" + "iqi"; String b = "Fliqi"; System.out.println(a == b);
Atrue
Bfalse
CFliqi
Dयह compile ही नहीं होताIt fails to compile
सही उत्तर / Answer: (A)
हल / Solution
यहाँ दोनों operand literal हैं, इसलिए compiler उन्हें compile करते समय ही जोड़ देता है और पूरे व्यंजक को एक ही स्थिर मान Fliqi मानता है। वह स्थिर मान string pool में रखा जाता है, और अगली पंक्ति का literal उसी pool वाले object को पा लेता है, इसलिए दोनों references एक ही object पर जाते हैं और तुलना true छापती है। इस code में कुछ भी चलते समय नहीं बनता।
Both operands here are literals, so the compiler joins them while compiling and treats the whole expression as the single constant Fliqi. That constant is placed in the string pool, and the literal written on the next line finds the very same pooled object, so the two references point at one object and the comparison prints true. Nothing in this code is built at run time.
प्रश्न 6उत्तर + हल
निम्न Java code क्या छापेगा? char[] c = {'F', 'Q'}; System.out.println(String.valueOf(c));
What will the following Java code print? char[] c = {'F', 'Q'}; System.out.println(String.valueOf(c));
A[C@ के बाद एक hash मान[C@ followed by a hash value
BF
C2
DFQ
सही उत्तर / Answer: (D)
हल / Solution
String.valueOf() overloaded है, और जब उसे char array दिया जाता है तो compiler वह रूप चुनता है जो array के characters पढ़कर उनसे string बनाता है, इसलिए FQ छपेगा। Object के लिए बना रूप, जो type का नाम और hash दिखाता है, यहाँ नहीं चुना जाता क्योंकि char array वाला रूप अधिक सटीक है। यह method न अकेला पहला character लौटाता है और न array की लंबाई।
String.valueOf() is overloaded, and when it is given a char array the compiler picks the version that reads the characters of the array and builds a string out of them, so FQ is printed. The version written for Object, which would show a type name and a hash, is not chosen here because the char array version is more specific. The method returns neither the first character alone nor the array length.
प्रश्न 7उत्तर + हल
string constant pool में रखे एक ही String object को कई references के बीच साझा करना JVM के लिए सुरक्षित क्यों है?
Why is it safe for the JVM to let several references share one String object kept in the string constant pool?
Aक्योंकि pool हर reference के लिए अलग नक़ल रखता हैBecause the pool keeps a separate copy for every reference
Bक्योंकि pool में केवल 64 characters से छोटी strings रखी जाती हैंBecause the pool stores only strings shorter than 64 characters
Cक्योंकि String object की सामग्री बनने के बाद कभी बदली नहीं जा सकतीBecause a String object's contents can never be changed after it is created
Dक्योंकि pool वाले String तक हर पहुँच synchronized होती हैBecause every access to a pooled String is synchronized
सही उत्तर / Answer: (C)
हल / Solution
pool में रखा String किसी भी संख्या के references को दिया जा सकता है, क्योंकि बनने के बाद उसकी सामग्री कभी बदली नहीं जा सकती; reference रखने वाला कोई भी characters नहीं बदल सकता, इसलिए किसी दूसरे धारक के साथ धोखा नहीं होता। pool हर reference के लिए अलग नक़ल नहीं, एक ही साझा नक़ल रखता है, लंबाई से छाँटता नहीं, और इसमें कोई synchronization भी नहीं लगता।
A pooled String can be handed to any number of references because its contents can never be changed once it has been created; no holder of the reference is able to alter the characters, so no other holder is ever taken by surprise. The pool keeps one shared copy rather than a copy per reference, it does not judge strings by length, and no synchronization is involved.
प्रश्न 8उत्तर + हल
String class का intern() method क्या लौटाता है?
What does the intern() method of the String class return?
Aheap में बने एक नए object में रखी string की नक़लA copy of the string stored in a newly made heap object
Bउस सामग्री के लिए string constant pool का प्रामाणिक (canonical) String objectThe canonical String object for that content from the string constant pool
Cstring में characters की लंबाईThe length of the string in characters
Dउन्हीं characters वाला एक StringBuilderA StringBuilder holding the same characters
सही उत्तर / Answer: (B)
हल / Solution
intern() string constant pool में उसी characters वाला string ढूँढ़ता है; यदि वह पहले से मौजूद है तो वही pool वाला object लौटा देता है, वरना यह सामग्री pool में जोड़कर pool का reference देता है। इस तरह यह method उस सामग्री का एक ही प्रामाणिक object देता है, जिससे समान strings की तुलना निडर हो जाती है। यह न लंबाई बताता है और न कोई StringBuilder बनाता है।
intern() looks in the string constant pool for a string holding the same characters; if one is already there it hands back that pooled object, otherwise it adds this content to the pool and returns the pooled reference. The method therefore always gives the one canonical object for that content, which makes comparing equal strings safe. It neither reports a length nor builds a StringBuilder.
प्रश्न 9उत्तर + हल
निम्न Java code चलाने पर क्या होगा? String s = "Java"; System.out.println(s.charAt(4));
What happens when the following Java code is run? String s = "Java"; System.out.println(s.charAt(4));
Aचलते समय StringIndexOutOfBoundsException फेंका जाता हैA StringIndexOutOfBoundsException is thrown at run time
Bएक ख़ाली स्थान छपता हैA blank space is printed
Ca छपता हैThe letter a is printed
Dयह compile ही नहीं होताIt fails to compile
सही उत्तर / Answer: (A)
हल / Solution
Java में चार characters हैं, जो index 0 से index 3 तक रखे हैं, इसलिए index 4 अंतिम character से एक कदम आगे पड़ता है। Code बिना किसी शिकायत के compile हो जाता है, क्योंकि index की जाँच चलते समय ही होती है, और उसी क्षण charAt() एक StringIndexOutOfBoundsException फेंक देता है। यह ख़ाली स्थान नहीं छापता, और a तो index 3 पर बैठा character है।
Java has four characters, held at index 0 up to index 3, so index 4 lies one step past the last character. The code compiles without any complaint, because the index is checked only while the program runs, and at that moment charAt() throws a StringIndexOutOfBoundsException. It does not print a blank space, and the letter a is the character sitting at index 3.
प्रश्न 10उत्तर + हल
निम्नलिखित में से कौन String class का constructor नहीं है?
Which of the following is NOT a constructor of the String class?
AString(String original)
BString(char[] value)
CString(byte[] bytes)
DString(int length)
सही उत्तर / Answer: (D)
हल / Solution
String class में दूसरे String, character array, byte array, StringBuffer और बिना argument वाले constructor मिलते हैं, पर अकेला int लेने वाला कोई constructor नहीं है। String बनने के बाद बड़ा नहीं किया जा सकता, इसलिए पहले से लंबाई रोक रखने का कोई अर्थ नहीं रहता; यह विचार StringBuffer का है, जिसका constructor int capacity स्वीकार करता है। इसलिए String(int length) ग़लत है।
The String class offers constructors that take another String, a character array, a byte array, a StringBuffer or nothing at all, but it has no constructor that takes a single int. Since a String cannot be enlarged once it is built, reserving a length in advance would be pointless; that idea belongs to StringBuffer, whose constructor does accept an int capacity. So String(int length) is the odd one.
आगे के 15 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
What does the constructor String(byte[] bytes) do?
Aयह byte के मानों को पाठ की तरह रखता है, इसलिए String में 74 और 97 जैसे अंक आ जाते हैंIt stores the byte values as text, so the String ends up holding digits such as 74 and 97
Bयह bytes को default charset से decode करके characters बनाता हैIt decodes the bytes into characters using the default charset
Cयह हर byte को बिना किसी decoding के सीधे char में रख देता है और charset की उपेक्षा करता हैIt copies each byte straight into a char with no decoding and ignores the charset
Dयह compile-time error देता है, क्योंकि bytes से String नहीं बन सकताIt gives a compile-time error, because a String cannot be built from bytes