(a) To convert a string to upper case in Java, the
toUpperCase() method is used.
It returns a new string with all characters converted to uppercase.
System.out.println(str1.toUpperCase());
This statement prints the string "HELLO".
The original value of str1 remains unchanged, as strings in Java are immutable.
(b) Java strings use zero-based indexing. The character at index 6 is the 7th character.
The
charAt() method returns the character at a given index.
System.out.println(str2.charAt(6));
In "Java Programming", index 6 corresponds to the letter 'r'.
(c) The
indexOf() method returns the position of the first occurrence of a character or substring.
System.out.println(str1.indexOf('l'));
In the string "Hello", the first 'l' appears at index 2.
If the character is not found, indexOf() returns -1.
(d) The
replace() method replaces all occurrences of the specified character with another.
System.out.println(str1.replace('l', '*'));
This will output: "He**o" — as both 'l's are replaced with '*'.
The original string remains unchanged because strings in Java are immutable.