FliQi Education App Download
प्रश्न-बैंक · Java

String class and String class Constructor — प्रश्न और उत्तर

Java · 25 जाँचे हुए (verified) प्रश्न

  • 25 जाँचे हुए प्रश्न
  • हिंदी + English

पहले 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);
  1. A4
  2. B2
  3. C3
  4. 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());
  1. Aयह StringIndexOutOfBoundsException फेंकता हैIt throws a StringIndexOutOfBoundsException
  2. B1
  3. C5
  4. 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);
  1. Aयह NullPointerException फेंकता हैIt throws a NullPointerException
  2. BValue:
  3. CValue: null
  4. 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);
  1. Atrue
  2. Bfalse
  3. CJava
  4. 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);
  1. Atrue
  2. Bfalse
  3. CFliqi
  4. 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));
  1. A[C@ के बाद एक hash मान[C@ followed by a hash value
  2. BF
  3. C2
  4. 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?
  1. Aक्योंकि pool हर reference के लिए अलग नक़ल रखता हैBecause the pool keeps a separate copy for every reference
  2. Bक्योंकि pool में केवल 64 characters से छोटी strings रखी जाती हैंBecause the pool stores only strings shorter than 64 characters
  3. Cक्योंकि String object की सामग्री बनने के बाद कभी बदली नहीं जा सकतीBecause a String object's contents can never be changed after it is created
  4. 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?
  1. Aheap में बने एक नए object में रखी string की नक़लA copy of the string stored in a newly made heap object
  2. Bउस सामग्री के लिए string constant pool का प्रामाणिक (canonical) String objectThe canonical String object for that content from the string constant pool
  3. Cstring में characters की लंबाईThe length of the string in characters
  4. 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));
  1. Aचलते समय StringIndexOutOfBoundsException फेंका जाता हैA StringIndexOutOfBoundsException is thrown at run time
  2. Bएक ख़ाली स्थान छपता हैA blank space is printed
  3. Ca छपता हैThe letter a is printed
  4. 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?
  1. AString(String original)
  2. BString(char[] value)
  3. CString(byte[] bytes)
  4. 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 — अभ्यास, हल और आपकी प्रगति
App खोलिए
प्रश्न 12उत्तर app में
String(byte[] bytes) constructor क्या करता है?
What does the constructor String(byte[] bytes) do?
  1. Aयह byte के मानों को पाठ की तरह रखता है, इसलिए String में 74 और 97 जैसे अंक आ जाते हैंIt stores the byte values as text, so the String ends up holding digits such as 74 and 97
  2. Bयह bytes को default charset से decode करके characters बनाता हैIt decodes the bytes into characters using the default charset
  3. Cयह हर byte को बिना किसी decoding के सीधे char में रख देता है और charset की उपेक्षा करता हैIt copies each byte straight into a char with no decoding and ignores the charset
  4. Dयह compile-time error देता है, क्योंकि bytes से String नहीं बन सकताIt gives a compile-time error, because a String cannot be built from bytes
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 18उत्तर app में
कौन-सा String method दो strings की सामग्री की तुलना करते समय बड़े और छोटे अक्षर को एक ही मानता है?
Which String method compares the contents of two strings while treating upper case and lower case letters as the same?
  1. Aequals()
  2. BcompareTo()
  3. CcontentEquals()
  4. DequalsIgnoreCase()
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 19उत्तर app में
String का indexOf() method तब क्या लौटाता है जब खोजा गया character string में कहीं नहीं होता?
What does the indexOf() method of String return when the searched character is not present anywhere in the string?
  1. A0
  2. B-1
  3. Cयह StringIndexOutOfBoundsException फेंकता हैIt throws a StringIndexOutOfBoundsException
  4. Dstring की लंबाईThe length of the string
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 20उत्तर app में
Java में String class को final कहकर घोषित किया गया है। इसका अर्थ क्या है?
The String class in Java is declared with the final keyword. What does this mean?
  1. Aहर String object को मान केवल घोषणा के समय एक बार ही दिया जा सकता हैEvery String object may be given a value only once, at the point of declaration
  2. BString class के पास अपना कोई constructor नहीं होताThe String class has no constructors of its own
  3. Cकोई class String को extend नहीं कर सकती, यानी String की subclass नहीं बन सकतीNo class can extend String, so String cannot be subclassed
  4. DString के सभी method static होते हैंAll methods of String are static
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 23उत्तर app में
String class का कौन-सा constructor किसी character array के केवल एक हिस्से से String बनाता है?
Which constructor of the String class builds a String from only a part of a character array?
  1. AString(char[] value)
  2. BString(String original)
  3. CString()
  4. DString(char[] value, int offset, int count)
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 24उत्तर app में
String s = new String(); यह वाक्य क्या बनाता है?
What does the statement String s = new String(); create?
  1. Aएक ख़ाली String, जिसका length() शून्य होता हैAn empty String whose length() is 0
  2. Bएक String जिसमें केवल n अक्षर होता हैA String containing only the single character n
  3. Cएक null reference, इसलिए s.length() पर NullPointerException आता हैA null reference, so s.length() throws a NullPointerException
  4. Dसोलह ख़ाली स्थानों वाला एक StringA String made of sixteen blank spaces
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
String class and String class Constructor के सभी 25 प्रश्न — उत्तर, हल और अभ्यास

FliQi Question Bank app में topic-वार अभ्यास, कठिनाई-स्तर और आपकी प्रगति।

  • पहला test मुफ़्त
  • हिंदी + English
  • जाँचे हुए उत्तर और हल

यह topic किन परीक्षाओं में आता है

Java के दूसरे topics

सभी topics