What does the following code output?
```java
String s = "abc";
System.out.println(s.substring(1, 3));
```
bc
Identify the error in the following code:
```java
String s = "hello";
for (int i = 0; i <= s.length(); i++) {
System.out.println(s.charAt(i));
}
```
IndexOutOfBoundsException: Loop should be `i < s.length()`.
What does the following code output?
```java
String s1 = "hello";
String s2 = new String("hello");
System.out.println(s1.equals(s2));
```
true
What does the following code output?
```java
String s = "world";
String result = "";
for (int i = 0; i < s.length(); i++) {
result = result + s.charAt(i);
}
System.out.println(result);
```
world
Identify the error in the following code:
```java
String s = "test";
System.out.println(s.substring(2, 1));
```
StringIndexOutOfBoundsException: endIndex cannot be less than beginIndex.
What does the following code output?
```java
String s = "apple";
System.out.println(s.length());
```
5
What does the following code output?
```java
String s = "OpenAI";
System.out.println(s.substring(4));
```
AI
Identify the error in the following code:
```java
String s = null;
System.out.println(s.length());
```
NullPointerException: Cannot call length() on a null string.
What does the following code output?
```java
String s1 = "hello";
String s2 = "Hello";
System.out.println(s1.equals(s2));
```
false
What does the following code output?
```java
String s = "programming";
System.out.println(s.indexOf("gram"));
```
3
Steps to reverse a string.
1. Initialize empty string. 2. Iterate through original string. 3. Prepend each character to the new string. 4. Return the new string.
Steps to check if a substring exists.
1. Iterate through main string. 2. Extract substring of target length. 3. Compare with target substring. 4. Return true if match found, else false.
Steps to get all substrings of length N.
1. Iterate through the string. 2. Extract substring of length N. 3. Print or process the substring.
Steps to find the first occurrence of a character in a string.
1. Iterate through the string. 2. Check if the current character matches the target. 3. If match, return the index. 4. If no match, return -1.
Steps to count the occurrences of a substring in a string.
1. Iterate through the string. 2. Extract substring of target length. 3. Compare with target substring. 4. Increment counter if match found. 5. Return the counter.
Steps to convert a string to uppercase.
1. Create a new empty string. 2. Iterate through each character of the original string. 3. Convert the character to uppercase. 4. Append the uppercase character to the new string. 5. Return the new string.
Steps to remove leading and trailing whitespace from a string.
1. Find the index of the first non-whitespace character. 2. Find the index of the last non-whitespace character. 3. Extract the substring between these indices.
Steps to replace all occurrences of a substring with another substring.
1. Use the replaceAll() method. 2. Specify the substring to replace and the replacement substring.
Steps to split a string into an array of substrings based on a delimiter.
1. Use the split() method. 2. Specify the delimiter.
Steps to check if a string starts with a specific prefix.
1. Use the startsWith() method. 2. Specify the prefix.
What is a string?
A sequence of characters.
What is `substring()`?
A method that returns a portion of a string.
What does `equals()` do?
Compares two strings for content equality.
What is an index in a string?
The position of a character in the string, starting from 0.
Define 'string reversal'.
Creating a new string with characters in reverse order.
What is a 'sliding window'?
A technique to process substrings of a fixed size.
Define 'substring'.
A contiguous sequence of characters within a string.
What is string immutability?
The property that strings cannot be changed after creation.
What is the purpose of `length()`?
Returns the number of characters in a string.
What is string concatenation?
Joining two or more strings end-to-end.