String Methods: length, charAt, substring, indexOf, toUpperCase Notes in Hindi & English : स्ट्रिंग मेथड्स: length, charAt, substring, concat, trim और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम String Methods: length, charAt, substring, indexOf, toUpperCase (स्ट्रिंग मेथड्स: length, charAt, substring, concat, trim) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: java.lang.String provides a rich immutable API including length(), charAt(), substring(), indexOf(), concat(), trim(), and case converters. Mastering 0-based bounds, exclusive end indices in substring(), and Java 11 Unicode-aware strip() prevents classic runtime exceptions.

अध्याय सार: java.lang.String में length(), charAt(), substring(), indexOf(), concat(), trim() और केस कन्वर्ज़न जैसे अनेक उपयोगी मेथड्स शामिल हैं। 0-आधारित इंडेक्सिंग, substring() के एक्सक्लूसिव एंड इंडेक्स और जावा 11 के strip() को समझना सामान्य रनटाइम त्रुटियों से बचाता है।

String Methods: length, charAt, substring, indexOf, toUpperCase: Detailed Notes & Architecture (स्ट्रिंग मेथड्स: length, charAt, substring, concat, trim)

1. Inspection Methods: length(), charAt(), isEmpty(), and isBlank() (इंस्पेक्शन मेथड्स: length(), charAt(), isEmpty() और isBlank())

[EN] Unlike arrays which use the .length field, String exposes int length() as a method returning the number of UTF-16 code units. charAt(int index) returns the char at 0-based position index, throwing StringIndexOutOfBoundsException if index < 0 or index >= length(). isEmpty() returns true strictly when length() == 0, whereas isBlank() (added in Java 11) returns true if the string is empty or contains only Unicode whitespace characters. Be careful: for surrogate-pair emojis outside the BMP, length() counts 2 UTF-16 code units while codePointCount(0, length()) gives the actual Unicode character count.

[HI] एरे की .length प्रॉपर्टी के विपरीत, String में int length() एक मेथड होता है जो UTF-16 कोड यूनिट्स की संख्या लौटाता है। charAt(int index) 0-आधारित इंडेक्स पर स्थित कैरेक्टर लौटाता है और इंडेक्स सीमा से बाहर होने पर StringIndexOutOfBoundsException फेंकता है। isEmpty() केवल length() == 0 पर true देता है, जबकि जावा 11 में जुड़ा isBlank() खाली या केवल व्हाइटस्पेस वाली स्ट्रिंग पर भी true देता है। इमोजी जैसे सरोगेट पेयर के लिए length() 2 गिनता है, जबकि codePointCount() वास्तविक यूनिकोड कैरेक्टर्स गिनता है।

2. Slicing with substring() and Java 7 Memory-Leak Fix (substring() द्वारा स्लाइसिंग और जावा 7 मेमोरी-लीक सुधार)

[EN] String offers two overloaded extraction methods: substring(int beginIndex) (extracts to the end) and substring(int beginIndex, int endIndex). Crucially, beginIndex is inclusive and endIndex is exclusive, producing a substring of length endIndex - beginIndex. Passing beginIndex == length() and endIndex == length() returns an empty string "", while beginIndex > endIndex or out-of-range indices throw StringIndexOutOfBoundsException. Up to Java 6, substring() shared the large parent char[] (causing memory leaks); since Java 7, substring() allocates a fresh trimmed array copy.

[HI] String में स्लाइसिंग के लिए substring(int beginIndex) और substring(int beginIndex, int endIndex) दो ओवरलोडेड मेथड्स हैं। यहाँ beginIndex समावेशी (inclusive) और endIndex बहिष्कृत (exclusive) होता है, जिससे कुल (endIndex - beginIndex) लंबाई की स्ट्रिंग मिलती है। गलत सीमा या beginIndex > endIndex होने पर StringIndexOutOfBoundsException आता है, जबकि beginIndex == length() खाली स्ट्रिंग "" देता है। जावा 6 तक substring() पुराने बड़े char[] को साझा करके मेमोरी लीक करता था, परंतु जावा 7+ में यह नई अलग कॉपी बनाता है।

3. Searching and Concatenation: indexOf(), lastIndexOf(), contains(), concat() (खोज व संयोजन: indexOf(), lastIndexOf(), contains() और concat())

[EN] indexOf(int ch) or indexOf(String str) returns the 0-based index of the first matching occurrence, or -1 if absent; an optional fromIndex starts searching forward from a given offset. lastIndexOf(...) searches backward from the end (or specified offset) and returns the rightmost matching start index or -1. contains(CharSequence s) returns boolean true if the sequence exists anywhere inside the string, internally delegating to indexOf(s.toString()) >= 0. concat(String str) appends str strictly to the end; unlike the + operator which turns null into "null", s.concat(null) throws NullPointerException.

[HI] indexOf() किसी कैरेक्टर या सबस्ट्रिंग के पहले मिलान का 0-आधारित इंडेक्स लौटाता है, और न मिलने पर -1 देता है। lastIndexOf() पीछे से खोजकर अंतिम मिलान का इंडेक्स या -1 लौटाता है। contains(CharSequence s) जांचता है कि सबस्ट्रिंग मौजूद है या नहीं और बूलियन true/false लौटाता है। concat(String str) अंत में नई स्ट्रिंग जोड़ता है; + ऑपरेटर के विपरीत जो null को "null" में बदल देता है, s.concat(null) सीधे NullPointerException फेंकता है।

4. Case Conversion, Whitespace Trimming (trim vs strip), and Replace (केस परिवर्तन, ट्रिमिंग (trim बनाम strip) और रिप्लेस)

[EN] toUpperCase() and toLowerCase() convert characters using the default Locale (Locale.ROOT is safer in Turkish/internationalized environments to avoid 'i'/'I' dotted bugs). trim() removes only leading and trailing characters whose Unicode value is <= '\u0020' (ASCII space, tab, newline), leaving non-breaking spaces ('\u00A0') untouched. Introduced in Java 11, strip(), stripLeading(), and stripTrailing() recognize all Unicode whitespace defined by Character.isWhitespace(). Finally, replace(char, char) / replace(CharSequence, CharSequence) replaces literal text cleanly, whereas replaceAll(regex, replacement) treats the first parameter as a regular expression.

[HI] toUpperCase() और toLowerCase() डिफ़ॉल्ट लोकेल के अनुसार केस बदलते हैं (अंतरराष्ट्रीय कोड में 'i'/'I' बग से बचने हेतु Locale.ROOT सुरक्षित है)। trim() केवल शुरुआती व अंतिम ऐसे कैरेक्टर्स हटाता है जिनका कोड <= '\u0020' (ASCII स्पेस, टैब, न्यूलाइन) हो, इसलिए यह '\u00A0' को नहीं हटाता। जावा 11 में जुड़े strip(), stripLeading() और stripTrailing() Character.isWhitespace() के तहत सभी यूनिकोड रिक्त स्थान हटाते हैं। replace() साधारण टेक्स्ट बदलता है जबकि replaceAll() पहले पैरामीटर को रेगुलर एक्सप्रेशन (Regex) मानता है।

Syntax, Formula & Technical Example (String Methods: length, charAt, substring, indexOf, toUpperCase)

String s = "  Computer Sir  ";
String t = s.trim();
System.out.println(t.length());          // 12
System.out.println(t.charAt(0));         // C
System.out.println(t.substring(0, 8));   // Computer
System.out.println(t.indexOf("Sir"));    // 9
System.out.println(t.toUpperCase());     // COMPUTER SIR

String Methods: length, charAt, substring, indexOf, toUpperCase Quick Reference & Comparison Table

String MethodReturn & Key RuleExam Fact
substring(b, e)Includes index b, excludes index eLength is e - b; throws StringIndexOutOfBoundsException
indexOf(sub)0-based first match index or -1Case-sensitive; returns 0 for empty string ""
concat(str)New combined String; strict non-nullThrows NullPointerException if arg is null (unlike +)
trim() vs strip()ASCII <= '\u0020' vs Unicode whitespacestrip() added in Java 11 handles '\u00A0' & Unicode spaces

String Methods: length, charAt, substring, indexOf, toUpperCase Top 20 Bilingual MCQs with Answers

Q1. What does "Rajasthan".substring(2, 6) return in Java?

(जावा में "Rajasthan".substring(2, 6) क्या लौटाता है?)

(A) "asth"

(B) "jast"

(C) "ajast"

(D) "jasta"

Correct Answer: (B)

[EN] Indices are 0='R', 1='a', 2='j', 3='a', 4='s', 5='t'; start 2 is inclusive and end 6 is exclusive, yielding "jast".

[HI] इंडेक्स 2 से 5 तक के अक्षर (2 समावेशी, 6 बहिष्कृत) 'j', 'a', 's', 't' हैं, इसलिए "jast" प्राप्त होता है।

Q2. What happens when you call "Hello".charAt(5)?

("Hello".charAt(5) कॉल करने पर क्या होता है?)

(A) Returns -1

(B) Returns 'o'

(C) Throws StringIndexOutOfBoundsException at runtime

(D) Returns '\u0000' null terminator

Correct Answer: (C)

[EN] Unlike C strings with a trailing null byte at index 5, Java "Hello" has valid indices 0..4, so index 5 is out of bounds.

[HI] C स्ट्रिंग के विपरीत जावा की "Hello" में वैध इंडेक्स केवल 0..4 तक हैं, इसलिए इंडेक्स 5 पर StringIndexOutOfBoundsException आता है।

Q3. What does "Programming".indexOf("Java") return?

("Programming".indexOf("Java") क्या मान लौटाता है?)

(A) null

(B) Throws NoSuchElementException

(C) 0

(D) -1

Correct Answer: (D)

[EN] Whenever a character or substring is not found, indexOf() returns -1.

[HI] जब कोई कैरेक्टर या सबस्ट्रिंग नहीं मिलती, तो indexOf() हमेशा -1 लौटाता है।

Q4. What is the result of executing String s = "Hi"; System.out.println(s.concat(null));?

(String s = "Hi"; System.out.println(s.concat(null)); चलाने का परिणाम क्या होगा?)

(A) Throws NullPointerException

(B) Compile-time error

(C) Prints Hi

(D) Prints Hinull

Correct Answer: (A)

[EN] String.concat(null) invokes str.length() internally and throws NullPointerException, unlike s + null which prints "Hinull".

[HI] s + null जहाँ "Hinull" प्रिंट करता है, वहीं String.concat(null) अंदरूनी लंबाई जांचने पर NullPointerException फेंकता है।

Q5. How does Java 11's strip() method differ from the older trim() method?

(जावा 11 का strip() मेथड पुराने trim() मेथड से किस प्रकार भिन्न है?)

(A) strip() removes all Unicode whitespace, whereas trim() only removes characters <= '\u0020'

(B) strip() modifies the original String in-place

(C) trim() removes spaces from the middle of the string too

(D) strip() also removes digits and punctuation

Correct Answer: (A)

[EN] trim() only checks ASCII control/space codes <= 32, missing Unicode spaces like '\u00A0' or '\u2003' that strip() properly handles.

[HI] trim() केवल ASCII <= 32 वाले स्पेस हटाता है, जबकि strip() यूनिकोड मानकों के सभी व्हाइटस्पेस (जैसे '\u00A0') को भी हटा देता है।

Q6. What does " ".isBlank() vs " ".isEmpty() return in Java 11+?

(जावा 11+ में " ".isBlank() और " ".isEmpty() क्रमशः क्या लौटाते हैं?)

(A) true and true

(B) false and false

(C) true and false

(D) false and true

Correct Answer: (C)

[EN] isBlank() returns true because only whitespace exists, while isEmpty() returns false because length() is 3.

[HI] केवल स्पेस होने से isBlank() true लौटाता है, जबकि लंबाई 3 होने के कारण isEmpty() false लौटाता है।

Q7. What does "Java".substring(4) return?

("Java".substring(4) क्या लौटाता है?)

(A) null

(B) "" (empty string of length 0)

(C) Throws StringIndexOutOfBoundsException

(D) "a"

Correct Answer: (B)

[EN] Passing beginIndex equal to length() (4) is a documented edge case returning an empty string "".

[HI] beginIndex का मान length() के बराबर (4) होना वैध है और यह लंबाई 0 वाली खाली स्ट्रिंग "" लौटाता है।

Q8. What does "banana".lastIndexOf('a') return?

("banana".lastIndexOf('a') क्या मान लौटाता है?)

(A) 1

(B) 5

(C) 6

(D) 3

Correct Answer: (B)

[EN] In "banana" (indices 0='b', 1='a', 2='n', 3='a', 4='n', 5='a'), the rightmost 'a' is at index 5.

[HI] "banana" में अंतिम 'a' इंडेक्स 5 पर स्थित है, इसलिए lastIndexOf('a') 5 लौटाता है।

Q9. What is the output of "computer".substring(3, 3).length()?

("computer".substring(3, 3).length() का आउटपुट क्या होगा?)

(A) Throws StringIndexOutOfBoundsException

(B) 0

(C) 1

(D) 3

Correct Answer: (B)

[EN] When beginIndex equals endIndex (within 0..length), substring returns an empty string whose length is endIndex - beginIndex = 0.

[HI] जब beginIndex और endIndex बराबर (वैध सीमा में) हों, तो substring 0 लंबाई की खाली स्ट्रिंग लौटाता है।

Q10. Why did Java 7 change String.substring() from sharing the parent char[] to copying a new array?

(जावा 7 में String.substring() को मूल char[] साझा करने के बजाय नया एरे कॉपी करने के लिए क्यों बदला गया?)

(A) To make substring mutable

(B) To prevent memory leaks when a tiny substring kept a huge multi-megabyte parent char[] alive

(C) To double memory consumption intentionally

(D) To avoid checking beginIndex bounds

Correct Answer: (B)

[EN] In Java 6, taking a 5-char substring from a 10MB string retained the entire 10MB char[] array in memory until the tiny substring was GC'd.

[HI] जावा 6 में बड़ी स्ट्रिंग से छोटा substring लेने पर भी पूरा मेगाबाइट एरे मेमोरी में अटका रहता था, जिससे मेमोरी लीक होता था।

Q11. What is the key difference between replace(CharSequence, CharSequence) and replaceAll(String, String)?

(replace(CharSequence, CharSequence) और replaceAll(String, String) में क्या मुख्य अंतर है?)

(A) replace() mutates the String in place

(B) replace() performs literal text matching, whereas replaceAll() interprets the first argument as a regular expression

(C) replaceAll() works only on single char arguments

(D) replace() replaces only the first match

Correct Answer: (B)

[EN] Both replace ALL occurrences, but replaceAll uses regex (so "." matches every char), while replace matches literal ".".

[HI] दोनों सभी मिलानों को बदलते हैं, परंतु replace() साधारण टेक्स्ट मिलान करता है जबकि replaceAll() पहले पैरामीटर को Regex मानता है।

Q12. What does "Jaipur".indexOf("") return?

("Jaipur".indexOf("") क्या मान लौटाता है?)

(A) 6

(B) -1

(C) 0

(D) Throws IllegalArgumentException

Correct Answer: (C)

[EN] Every String trivially contains the empty string "" starting at index 0.

[HI] प्रत्येक स्ट्रिंग की शुरुआत इंडेक्स 0 पर खाली स्ट्रिंग "" से मानी जाती है, इसलिए indexOf("") 0 लौटाता है।

Q13. What does "a,b,,".split(",") produce in Java?

(जावा में "a,b,,".split(",") कितने तत्वों का String[] एरे लौटाता है?)

(A) Array of length 3: {"a", "b", ""}

(B) Throws PatternSyntaxException

(C) Array of length 2: {"a", "b"} because single-arg split drops trailing empty strings

(D) Array of length 4: {"a", "b", "", ""}

Correct Answer: (C)

[EN] String.split(regex) invokes split(regex, 0), which discards trailing empty strings unless a negative limit like split(",", -1) is passed.

[HI] एक-आर्गुमेंट split(regex) अंत की खाली स्ट्रिंग्स को हटा देता है इसलिए {"a", "b"} मिलता है; उन्हें रखने के लिए split(",", -1) चाहिए।

Q14. Which of the following is NOT true about Java String methods?

(जावा String मेथड्स के बारे में निम्न में से कौन-सा कथन सत्य नहीं है?)

(A) substring(begin, end) excludes the character at end

(B) trim() removes all spaces from inside the middle of a sentence

(C) length() returns an int representing UTF-16 code unit count

(D) toUpperCase() returns a new String if any character changes

Correct Answer: (B)

[EN] trim() strips whitespace exclusively from the beginning and end of the string, never internal middle spaces.

[HI] trim() केवल स्ट्रिंग के शुरुआत और अंत से ही व्हाइटस्पेस हटाता है, बीच के स्पेस को नहीं हटाता।

Q15. What will System.out.println("abc".replace('x', 'y') == "abc"); print?

(System.out.println("abc".replace('x', 'y') == "abc"); क्या प्रिंट करेगा?)

(A) true

(B) false

(C) Throws NullPointerException

(D) Compile-time error

Correct Answer: (A)

[EN] Because 'x' does not appear in "abc", String.replace returns this (the exact same pooled reference).

[HI] चूंकि 'x' "abc" में मौजूद नहीं है, replace() नया ऑब्जेक्ट बनाए बिना मौजूदा रेफरेंस (this) ही लौटा देता है।

Q16. What exception does "abc".substring(2, 1) throw?

("abc".substring(2, 1) कौन-सा एक्सेप्शन फेंकता है?)

(A) IllegalArgumentException

(B) StringIndexOutOfBoundsException

(C) ArrayIndexOutOfBoundsException

(D) NegativeArraySizeException

Correct Answer: (B)

[EN] When beginIndex > endIndex (subLen < 0), String.substring throws StringIndexOutOfBoundsException.

[HI] जब beginIndex का मान endIndex से बड़ा होता है, तो String.substring() StringIndexOutOfBoundsException फेंकता है।

Q17. Within String Methods: length, charAt, substring, indexOf, toUpperCase, which string method directly maps to: "Includes index b, excludes index e"?

(String Methods: length, charAt, substring, indexOf, toUpperCase में कौन सा String Method सीधे "Includes index b, excludes index e" से संबंधित है?)

(A) indexOf(sub)

(B) trim() vs strip()

(C) substring(b, e)

(D) concat(str)

Correct Answer: (C)

[EN] In String Methods: length, charAt, substring, indexOf, toUpperCase, 'substring(b, e)' handles 'Includes index b, excludes index e' (Length is e - b; throws StringIndexOutOfBoundsException).

[HI] String Methods: length, charAt, substring, indexOf, toUpperCase में 'substring(b, e)' का कार्य/विवरण 'Includes index b, excludes index e' (Length is e - b; throws StringIndexOutOfBoundsException) है।

Q18. Which option accurately describes 'indexOf(sub)' in String Methods: length, charAt, substring, indexOf, toUpperCase?

(String Methods: length, charAt, substring, indexOf, toUpperCase के संदर्भ में 'indexOf(sub)' की सही विशेषता कौन सी है?)

(A) Includes index b, excludes index e

(B) New combined String; strict non-null

(C) 0-based first match index or -1

(D) ASCII <= '\u0020' vs Unicode whitespace

Correct Answer: (C)

[EN] 'indexOf(sub)' provides '0-based first match index or -1' — key point: Case-sensitive; returns 0 for empty string "".

[HI] 'indexOf(sub)' का मुख्य विवरण '0-based first match index or -1' है (Case-sensitive; returns 0 for empty string "")।

Q19. In String Methods: length, charAt, substring, indexOf, toUpperCase, identify the correct parameter for the specification: "New combined String; strict non-null".

(String Methods: length, charAt, substring, indexOf, toUpperCase में "New combined String; strict non-null" विवरण किस विकल्प के लिए सही है?)

(A) trim() vs strip()

(B) concat(str)

(C) substring(b, e)

(D) indexOf(sub)

Correct Answer: (B)

[EN] 'concat(str)' corresponds to 'New combined String; strict non-null' (Throws NullPointerException if arg is null (unlike +)).

[HI] 'concat(str)' का संबंध 'New combined String; strict non-null' (Throws NullPointerException if arg is null (unlike +)) से है।

Q20. Considering String Methods: length, charAt, substring, indexOf, toUpperCase, what key behavior defines 'trim() vs strip()'?

(String Methods: length, charAt, substring, indexOf, toUpperCase में 'trim() vs strip()' की मुख्य भूमिका या मान क्या है?)

(A) ASCII <= '\u0020' vs Unicode whitespace

(B) Includes index b, excludes index e

(C) 0-based first match index or -1

(D) New combined String; strict non-null

Correct Answer: (A)

[EN] 'trim() vs strip()' represents 'ASCII <= '\u0020' vs Unicode whitespace' (strip() added in Java 11 handles '\u00A0' & Unicode spaces).

[HI] 'trim() vs strip()' का अर्थ/मान 'ASCII <= '\u0020' vs Unicode whitespace' है (strip() added in Java 11 handles '\u00A0' & Unicode spaces)।

String Methods: length, charAt, substring, indexOf, toUpperCase — Exam FAQs

Q1: Why does array use .length while String uses .length() and Collection uses .size()?

एरे में .length, String में .length() और Collection में .size() क्यों होता है?

[EN] Arrays are language-level fixed structures exposing a final field; String wraps a sequence via a CharSequence method length(), and Collections follow the Collection.size() API.

[HI] एरे भाषा-स्तरीय निश्चित संरचना है जिसकी final प्रॉपर्टी length होती है; String CharSequence इंटरफ़ेस से length() मेथड देता है और कलेक्शंस size() मेथड का पालन करते हैं।

Q2: Why is String.toLowerCase(Locale.ROOT) recommended over parameterless toLowerCase()?

बिना पैरामीटर वाले toLowerCase() की तुलना में String.toLowerCase(Locale.ROOT) की सलाह क्यों दी जाती है?

[EN] Default locales like Turkish convert uppercase 'I' to dotless 'ı' rather than ASCII 'i', breaking protocol/keyword comparisons; Locale.ROOT guarantees locale-independent ASCII rules.

[HI] तुर्की जैसे डिफ़ॉल्ट लोकेल में 'I' को 'i' के बजाय अलग अक्षर में बदल दिया जाता है जिससे प्रोटोकॉल तुलना टूट सकती है; Locale.ROOT लोकेल-स्वतंत्र परिणाम देता है।

Q3: Does String.replace(oldChar, newChar) use regular expressions?

क्या String.replace(oldChar, newChar) या replace(CharSequence, CharSequence) रेगुलर एक्सप्रेशन का उपयोग करता है?

[EN] No. Both overloads of replace() perform exact literal substitution; only replaceAll() and replaceFirst() treat the search pattern as a regex.

[HI] नहीं। replace() के दोनों रूप सटीक लिटरल बदलाव करते हैं; केवल replaceAll() और replaceFirst() ही खोज पैटर्न को रेगुलर एक्सप्रेशन मानते हैं।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)