CORE JAVA FOR TCS IPA – Chapter 7: String
(Detailed Notes + MCQs)
What is a String?
A String is a sequence of characters.
Example:
String name = "Harshad";
Here,
H a r s h a d
is a String.
Unlike C/C++, Java provides a built-in String class.
The String class belongs to the [Link] package, which is automatically imported.
Why is String Important?
Strings are used almost everywhere:
• Names
• Passwords
• URLs
• Email IDs
• JSON
• File Paths
More than 60% of Java applications use Strings.
This is why TCS asks many String-based MCQs.
1
String is Immutable
The most important concept.
Immutable means:
Once a String object is created, its value cannot be changed.
Example
String s = "Java";
[Link](" Programming");
[Link](s);
Output
Java
Why?
Because
concat()
creates a new String.
It does not modify the existing String.
Correct
String s = "Java";
s = [Link](" Programming");
[Link](s);
Output
2
Java Programming
Memory
Before
Java
After concat()
Java
Programming
(New Object)
s still points to old object.
After assignment
Java Programming
Why Strings are Immutable?
Reasons
• Security
• Thread Safety
• String Pool Optimization
• HashMap Keys
• Performance
3
Ways to Create Strings
Method 1 (String Literal)
String s = "Hello";
Stored inside
String Constant Pool (SCP)
Most commonly used.
Method 2 (Using new)
String s = new String("Hello");
Creates an object in
• Heap Memory
and also uses the String Constant Pool if the literal is not already present.
String Constant Pool (SCP)
Special memory area inside Heap.
Stores only one copy of identical String literals.
Example
String s1 = "Java";
String s2 = "Java";
String s3 = "Java";
Memory
4
SCP
Java
s1
s2
s3
Only one object is created.
Objects Created
Example
String s1 = "abc";
String s2 = "abc";
Objects created?
One
Reason
Both refer to the same SCP object.
Now
String s = new String("abc");
Objects created?
5
Two
One
String Pool
One
Heap
Memory
Heap
abc
Reference
SCP
abc
This is one of the most frequently asked TCS MCQs.
== vs equals()
Very Important
==
Checks reference (memory address).
Example
6
String s1 = "Java";
String s2 = "Java";
[Link](s1 == s2);
Output
true
Both refer to the same object.
Now
String s1 = new String("Java");
String s2 = new String("Java");
[Link](s1 == s2);
Output
false
Different objects.
equals()
Checks content.
Example
String s1 = new String("Java");
String s2 = new String("Java");
[Link]([Link](s2));
Output
7
true
Because contents are equal.
Comparison Table
== equals()
Compares reference Compares content
Returns true if both refer to same object Returns true if values are equal
Operator Method
Memory Trick
==
Address
equals()
Content
Important String Methods
1. length()
Returns number of characters.
String s = "Harshad";
[Link]([Link]());
Output
8
7
2. charAt(index)
Returns character at given index.
String s = "Java";
[Link]([Link](2));
Output
3. substring()
Extracts part of String.
String s = "Programming";
[Link]([Link](3));
Output
gramming
Another example
[Link]([Link](3,7));
Output
gram
9
4. indexOf()
Returns first occurrence.
String s = "banana";
[Link]([Link]('a'));
Output
5. lastIndexOf()
Returns last occurrence.
[Link]("banana".lastIndexOf('a'));
Output
6. replace()
String s = "Java";
[Link]([Link]('a','o'));
Output
Jovo
10
7. trim()
Removes leading and trailing spaces.
String s = " Java ";
[Link]([Link]());
Output
Java
8. contains()
Checks whether substring exists.
[Link]("Programming".contains("gram"));
Output
true
9. startsWith()
[Link]("Programming".startsWith("Pro"));
Output
true
10. endsWith()
[Link]("Programming".endsWith("ing"));
11
Output
true
11. equalsIgnoreCase()
[Link]("JAVA".equalsIgnoreCase("java"));
Output
true
12. split()
String s="Java Python C";
String[] arr=[Link](" ");
Output
Java
Python
13. toUpperCase()
"java".toUpperCase()
Output
JAVA
12
14. toLowerCase()
"JAVA".toLowerCase()
Output
java
15. isEmpty()
"".isEmpty()
Output
true
16. concat()
"Java".concat(" Programming")
Output
Java Programming
Remember
concat() does not modify the original String because Strings are immutable.
StringBuffer
Mutable.
Thread Safe.
13
Slower.
Example
StringBuffer sb = new StringBuffer("Java");
[Link](" Programming");
[Link](sb);
Output
Java Programming
StringBuilder
Mutable.
Not Thread Safe.
Faster.
Example
StringBuilder sb = new StringBuilder("Java");
[Link](" Programming");
[Link](sb);
Output
Java Programming
14
String vs StringBuffer vs StringBuilder
Feature String StringBuffer StringBuilder
Mutable ❌ No ✅ Yes ✅ Yes
Thread Safe Yes Yes No
Performance Slow for modification Slower Fastest
Memory Creates new object on modification Same object Same object
Memory Trick
String
Immutable
Safe
StringBuffer
Mutable
Thread Safe
Slow
StringBuilder
Mutable
Not Thread Safe
15
↓
Fastest
Frequently Asked TCS MCQs
MCQ 1
String is
A) Mutable
B) Immutable
C) Interface
D) Abstract
Answer: B
MCQ 2
Output
String s="Java";
[Link](" Programming");
[Link](s);
A) Java Programming
B) Java
C) Programming
D) Error
Answer: B
16
MCQ 3
String s1="abc";
String s2="abc";
Objects created?
A) 1
B) 2
C) 3
D) 4
Answer: A
MCQ 4
new String("abc")
Objects created?
A) 1
B) 2
C) 3
D) Depends
Answer: B
MCQ 5
Difference between == and equals() ?
A) Both compare content
B) == compares references, equals() compares content
17
C) equals() compares references
D) No difference
Answer: B
MCQ 6
Fastest mutable String class?
A) String
B) StringBuffer
C) StringBuilder
D) Character
Answer: C
MCQ 7
Which String class is thread safe?
A) StringBuilder
B) StringBuffer
C) String
D) Character
Answer: B
MCQ 8
Output
[Link]("Java".length());
A) 3
18
B) 4
C) 5
D) Error
Answer: B
MCQ 9
Output
[Link]("Java".charAt(1));
A) J
B) a
C) v
D) Error
Answer: B
MCQ 10
Output
[Link]("Programming".substring(3,7));
A) gram
B) gramming
C) Prog
D) amm
Answer: A
19
MCQ 11
Output
[Link]("banana".indexOf('a'));
A) 0
B) 1
C) 3
D) 5
Answer: B
MCQ 12
Output
[Link]("JAVA".equalsIgnoreCase("java"));
A) true
B) false
C) Error
D) Null
Answer: A
MCQ 13
Output
[Link]("".isEmpty());
A) true
20
B) false
C) Error
D) null
Answer: A
MCQ 14
Which method removes leading and trailing spaces?
A) strip()
B) trim()
C) remove()
D) replace()
Answer: B
MCQ 15
Which method converts a String into an array?
A) split()
B) divide()
C) break()
D) parse()
Answer: A
Common Mistakes Asked in TCS
❌ Wrong
21
if(s1==s2)
when comparing String contents.
✔ Correct
if([Link](s2))
❌ Thinking concat() changes the original String.
✔ It creates a new String.
❌ Using length instead of length() for Strings.
✔ Correct
[Link]()
Memory Tricks
Remember
Array
length
String
length()
ArrayList
22
size()
Remember
==
Reference
equals()
Content
Remember
String
Immutable
StringBuffer
Mutable
Thread Safe
StringBuilder
Mutable
23
Not Thread Safe
Fastest
One-Minute Revision
✔ Strings are immutable.
✔ String literals are stored in the String Constant Pool (SCP).
✔ "abc" creates one object (if not already in the pool).
✔ new String("abc") creates two objects (one in the SCP, one in the Heap).
✔ == compares references.
✔ equals() compares contents.
✔ length() returns the number of characters.
✔ charAt() returns the character at a specific index.
✔ substring() extracts part of a String.
✔ trim() removes leading and trailing spaces.
✔ split() divides a String into an array.
✔ StringBuffer is mutable and thread safe.
✔ StringBuilder is mutable, not thread safe, and the fastest for string modifications.
TCS Tip: This is the highest-weightage Java topic. Be prepared for output-based questions, String Pool,
immutability, == vs equals() , String methods, and StringBuffer vs StringBuilder comparisons.
24