(From Lesson 17 of Blue Pelican Java)

Use the following code for problems 1 – 15. In each problem state what’s printed.

String s = “Lucky hockey puck”;

String m = “uck”;

int j = 6, z = 99;

1. int k = s.indexOf(m);

System.out.println(k);

2. int k = s.indexO”, j);

System.out.println(k);

3. int k = s.indexOf(‘c’);

System.out.println(k);

4. String str = s.replace(‘o’, ‘p’);

System.out.println(str);

5. int k = s.lastIndexOf(m, j + 3);

System.out.println(k);

6. char p = s.charAt(7);

System.out.println(p);

7. int k = s.indexOf(z);

System.out.println(k);

8. int k = s.lastIndexOf(m);

System.out.println(k);

9. int k = s.indexOf(‘y’, j);

System.out.println(k);

10. char p = s.charAt(z - 90);

System.out.println(p);

11. int k = s.indexOf(m,15);

System.out.println(k);

12. int k = s.indexOf(z + 2, 4);

System.out.println(k);

13. int k = s.lastIndexOf(‘h’);

System.out.println(k);

14. int k = s.lastIndexOf(121);

System.out.println(k);

15. String str = s.replace(‘y’, ‘A’);

System.out.println(str);

LuckA HockeA puck

1. The output will be 5. This is because the indexOf() method returns the index of the first occurrence of the specified substring in the given string. In this case, the substring "uck" is found at index 5 in the string "Lucky hockey puck".

2. The output will be 9. Here, the indexOf() method is used with an additional parameter that specifies the starting index for the search. In this case, the search for the substring "uck" starts at index 6. Since "uck" is found at index 9 in the given string, that is the output.

3. The output will be 3. The indexOf() method is used to find the index of the first occurrence of the character 'c' in the string "Lucky hockey puck". Since 'c' is found at index 3, that is the output.

4. The output will be "Lucky hockep puck". The replace() method is used to replace all occurrences of the character 'o' with the character 'p' in the string "Lucky hockey puck". Therefore, the output is the modified string.

5. The output will be -1. The lastIndexOf() method is used to find the last occurrence of the substring "uck" in the string "Lucky hockey puck", starting from index 9. Since there is no occurrence o" after index 9, the method returns -1.

6. The output will be 'h'. The charAt() method is used to get the character at index 7 in the string "Lucky hockey puck", which is 'h'.

7. The output will be -1. The indexOf() method is used to find the index of the first occurrence of the integer variable z (which has a value of 99) in the string "Lucky hockey puck". Since there is no occurrence of 99 in the string, the method returns -1.

8. The output will be 5. The lastIndexOf() method is used to find the last occurrence of the substring "uck" in the string "Lucky hockey puck". Since "uck" is found at index 5 in the given string, that is the output.

9. The output will be 10. The indexOf() method is used to find the index of the first occurrence of the character 'y' in the string "Lucky hockey puck", starting from index 6. Since 'y' is found at index 10, that is the output.

10. The output will be 'L'. The charAt() method is used to get the character at index 9 in the string "Lucky hockey puck", which is 'L'.

11. The output will be -1. The indexOf() method is used to find the index of the first occurrence of the substring "uck" in the string "Lucky hockey puck", starting from index 15. Since there is no occurrence o" after index 15, the method returns -1.

12. The output will be -1. The indexOf() method is used to find the index of the first occurrence of the integer expression (z + 2) (which has a value of 101) in the string "Lucky hockey puck", starting from index 4. Since there is no occurrence of 101 in the string after index 4, the method returns -1.

13. The output will be 14. The lastIndexOf() method is used to find the last occurrence of the character 'h' in the string "Lucky hockey puck". Since 'h' is found at index 14, that is the output.

14. The output will be -1. The lastIndexOf() method is used to find the last occurrence of the integer value 121 in the string "Lucky hockey puck". Since there is no occurrence of 121 (which is not a valid character) in the string, the method returns -1.

15. The output will be "Lucky Aockey puck". The replace() method is used to replace all occurrences of the character 'y' with the character 'A' in the string "Lucky hockey puck". Therefore, the output is the modified string.

To determine what's printed for each problem, we need to understand the methods being used and their parameters.

1. Problem: int k = s.indexOf(m);
Explanation: The indexOf method of a string returns the index of the first occurrence of the specified substring within the string. It takes the substring as a parameter and returns -1 if the substring is not found. In this case, it searches for "uck" within the string "Lucky hockey puck" and assigns the index of the first occurrence to k.
Answer: k = 5
Output: 5

2. Problem: int k = s.indexO", j);
Explanation: The indexOf method can also take an additional parameter specifying the index from where to start searching. In this case, it starts searching for "uck" from index 6 (j) within the string "Lucky hockey puck" and assigns the index of the first occurrence to k.
Answer: k = 6
Output: 6

3. Problem: int k = s.indexOf('c');
Explanation: The indexOf method can also search for individual characters. In this case, it searches for the character 'c' within the string "Lucky hockey puck" and assigns the index of the first occurrence to k.
Answer: k = 2
Output: 2

4. Problem: String str = s.replace('o', 'p');
Explanation: The replace method of a string replaces all occurrences of a specified character with another character. In this case, it replaces all 'o's with 'p's within the string "Lucky hockey puck" and assigns the modified string to str.
Answer: str = "Lucky hpeckey puck"
Output: "Lucky hpeckey puck"

5. Problem: int k = s.lastIndexOf(m, j + 3);
Explanation: The lastIndexOf method of a string returns the index of the last occurrence of the specified substring within the string. It takes the substring as a parameter and optionally a starting index from where to search backward. In this case, it starts searching for "uck" from index 9 (j + 3) backward within the string "Lucky hockey puck" and assigns the index of the last occurrence to k.
Answer: k = 5
Output: 5

6. Problem: char p = s.charAt(7);
Explanation: The charAt method of a string returns the character at the specified index. In this case, it retrieves the character at index 7 within the string "Lucky hockey puck" and assigns it to p.
Answer: p = 'h'
Output: 'h'

7. Problem: int k = s.indexOf(z);
Explanation: The indexOf method can search for an integer as a character code. In this case, it searches for the character with the ASCII code 99 (z) within the string "Lucky hockey puck" and assigns the index of the first occurrence to k. Since the character is not found, it returns -1.
Answer: k = -1
Output: -1

8. Problem: int k = s.lastIndexOf(m);
Explanation: The lastIndexOf method can also take a substring as a parameter. In this case, it searches for the last occurrence o" within the string "Lucky hockey puck" and assigns the index to k.
Answer: k = 5
Output: 5

9. Problem: int k = s.indexOf('y', j);
Explanation: The indexOf method can also take a starting index as a parameter. In this case, it starts searching for the character 'y' from index 6 (j) within the string "Lucky hockey puck" and assigns the index of the first occurrence to k.
Answer: k = 13
Output: 13

10. Problem: char p = s.charAt(z - 90);
Explanation: The charAt method can also take an integer as an index. In this case, it retrieves the character at index 9 within the string "Lucky hockey puck" and assigns it to p.
Answer: p = 'h'
Output: 'h'

11. Problem: int k = s.indexOf(m, 15);
Explanation: The indexOf method can also take a starting index as a parameter. In this case, it starts searching for "uck" from index 15 within the string "Lucky hockey puck" and assigns the index of the first occurrence to k.
Answer: k = -1
Output: -1

12. Problem: int k = s.indexOf(z + 2, 4);
Explanation: The indexOf method can search for an integer as a character code. In this case, it searches for the character with the ASCII code 101 (z + 2) within the string "Lucky hockey puck" starting from index 4 and assigns the index of the first occurrence to k.
Answer: k = 8
Output: 8

13. Problem: int k = s.lastIndexOf('h');
Explanation: The lastIndexOf method can also search for individual characters. In this case, it searches for the last occurrence of 'h' within the string "Lucky hockey puck" and assigns the index to k.
Answer: k = 10
Output: 10

14. Problem: int k = s.lastIndexOf(121);
Explanation: The lastIndexOf method can search for an integer as a character code. In this case, it searches for the character with the ASCII code 121 within the string "Lucky hockey puck" and assigns the index of the last occurrence to k. Since the character is not found, it returns -1.
Answer: k = -1
Output: -1

15. Problem: String str = s.replace('y', 'A');
Explanation: The replace method can also replace a character with another character. In this case, it replaces all 'y's with 'A's within the string "Lucky hockey puck" and assigns the modified string to str.
Answer: str = "LuckA hockeA puck"
Output: "LuckA hockeA puck"