0% found this document useful (0 votes)
24 views4 pages

String

The document covers fundamental concepts of Strings in Java, including definitions, immutability, and differences between String and StringBuffer. It also includes output predictions for various String methods and programming logic for tasks such as extracting words, checking for palindromes, sorting names, and converting characters to uppercase. Additionally, it provides a quick reference for ASCII values relevant to String operations.

Uploaded by

Abhishek Paul
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

String

The document covers fundamental concepts of Strings in Java, including definitions, immutability, and differences between String and StringBuffer. It also includes output predictions for various String methods and programming logic for tasks such as extracting words, checking for palindromes, sorting names, and converting characters to uppercase. Additionally, it provides a quick reference for ASCII values relevant to String operations.

Uploaded by

Abhishek Paul
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Section 1: Theory & Concepts (2 Mark Questions)

Q1. Define a String in Java. Is it a primitive data type? Answer: No, a String is not a
primitive data type in Java. It is an Object of the class [Link]. It represents a sequence
of characters.
Q2. What is meant by "String Immutability"? Answer: Immutability means that once a String
object is created, its state (content) cannot be changed.
●​ If you attempt to modify a String (e.g., using concat() or replace()), Java does not change
the original object. Instead, it creates a new String object in memory with the modified
value.
Q3. Differentiate between String and StringBuffer classes. Answer: | Feature | String |
StringBuffer | | :--- | :--- | :--- | | Mutability | Immutable (cannot be changed). | Mutable (can be
modified in place). | | Performance | Slower for frequent concatenation (creates many
temporary objects). | Faster for frequent modifications. | | Storage | Stored in String Constant
Pool or Heap. | Stored in Heap memory only. |
Q4. What is the difference between the length and length() members? Answer:
●​ length: It is a property (variable) used to find the size of an Array (e.g., [Link]).
●​ length(): It is a method used to find the number of characters in a String (e.g.,
[Link]()).
Q5. Explain the difference between == operator and .equals() method with an example.
Answer:
●​ ==: Compares the reference (memory address) to see if two variables point to the exact
same object.
●​ .equals(): Compares the content (actual character sequence) of the strings.
Example:
String s1 = new String("ISC");​
String s2 = new String("ISC");​
[Link](s1 == s2); // Output: false (Different memory
addresses)​
[Link]([Link](s2)); // Output: true (Same content)​

Section 2: Output Prediction (Tricky Method Calls)


Q6. Give the output of the following code snippet regarding substring.
String s = "APPLICATION";​
[Link]([Link](3));​
[Link]([Link](2, 6));​

Solution:
LICATION​
PLIC​

Explanation:
●​ substring(3) starts from index 3 ('L') to the end.
●​ substring(2, 6) starts at index 2 ('P') and goes up to, but excluding, index 6 ('A'). It
captures indices 2, 3, 4, 5.
Q7. Calculate the return value of compareTo in the following cases.
String s1 = "Apple";​
String s2 = "Apply";​
String s3 = "Apple";​
[Link]([Link](s2));​
[Link]([Link](s3));​

Solution:
-20​
0​

Explanation:
●​ "Apple" vs "Apply": The first 4 chars match. The mismatch is at index 4 ('e' vs 'y').
○​ ASCII of 'e' = 101, 'y' = 121.
○​ Result = 101 - 121 = -20.
●​ "Apple" vs "Apple": Strings are identical, so result is 0.
Q8. What is the output of the following indexOf operations?
String x = "Mississipi";​
[Link]([Link]('s'));​
[Link]([Link]('s'));​
[Link]([Link]("sip"));​

Solution:
2​
6​
6​

Explanation:
●​ indexOf('s') finds the first occurrence (at index 2).
●​ lastIndexOf('s') finds the last occurrence (at index 6).
●​ indexOf("sip") finds the starting index of the substring "sip".
Q9. Analyze the output of replace and trim.
String s = " Good Morning ";​
[Link]([Link]().replace('o', 'K'));​

Solution:
GKKd MKrning​

Explanation:
1.​ trim() removes leading/trailing spaces -> "Good Morning".
2.​ replace('o', 'K') replaces all 'o's with 'K'.

Section 3: Programming Logic (Code Snippets)


Q10. How do you extract and print each word from a sentence "Java is fun"? Solution
Logic: Add a space to the end of the string to ensure the last word is processed. Iterate through
the string; if a character is not a space, add it to a temporary variable (word). If it is a space,
print the word and reset it.
Code:
String s = "Java is fun";​
s = s + " "; // Vital step​
String w = "";​
for(int i = 0; i < [Link](); i++) {​
char ch = [Link](i);​
if(ch != ' ') {​
w = w + ch; // Build word​
} else {​
[Link](w); // Process word​
w = ""; // Reset​
}​
}​

Q11. Write a snippet to check if a String s is a Palindrome. Solution:


String s = "MADAM";​
String rev = "";​
for(int i = 0; i < [Link](); i++) {​
char ch = [Link](i);​
rev = ch + rev; // Prepend char to reverse​
}​
if([Link](rev)) {​
[Link]("Palindrome");​
} else {​
[Link]("Not Palindrome");​
}​

Q12. How do you sort an array of names alphabetically? Solution: Use Bubble Sort logic
with the compareTo method.
String names[] = {"Zen", "Ben", "Ken"};​
for(int i = 0; i < [Link] - 1; i++) {​
for(int j = 0; j < [Link] - 1 - i; j++) {​
// If names[j] is "greater" (comes after) names[j+1]​
if(names[j].compareTo(names[j+1]) > 0) {​
String temp = names[j];​
names[j] = names[j+1];​
names[j+1] = temp;​
}​
}​
}​

Q13. How to convert a character from Lowercase to Uppercase without using


toUpperCase()? Solution: Subtract 32 from the character (ASCII logic).
char ch = 'm';​
if(ch >= 'a' && ch <= 'z') {​
ch = (char)(ch - 32); // 97 - 32 = 65 ('A')​
}​
[Link](ch); // Output: M​

Section 4: Quick Fire ASCII Reference


Memorize these for output questions involving calculations:
●​ 'A' - 'Z': 65 to 90
●​ 'a' - 'z': 97 to 122
●​ '0' - '9': 48 to 57
●​ Space: 32
●​ Difference between Cases: 32 (e.g., 'a' - 'A' = 32)
Would you like me to create a specific "predict the output" worksheet based on these notes?

You might also like