public class Main { public static void main(String[] args) { String s = "hello"; s = s.toUpperCase(); s = s.substring(1, 4); System.out.println(s); } }
The string "hello" is first converted to uppercase "HELLO". Then substring(1,4) extracts characters at indices 1, 2, and 3, which are 'E', 'L', 'L'. So the output is "ELL".
Strings are sequences of characters stored in memory with an order. They provide operations like indexing, slicing, concatenation, and searching, which are typical features of data structures.
public class Main { public static void main(String[] args) { String s = "hello"; s.charAt(0) = 'H'; System.out.println(s); } }
The expression s.charAt(0) returns a character value, not a variable. You cannot assign a new value to it. This causes a compilation error.
Because strings cannot change, Java can safely reuse the same string object in multiple places (string pool), saving memory and improving performance.
public class Main { public static void main(String[] args) { String s1 = "Data"; String s2 = "Structure"; String s3 = s1 + s2.substring(0, 3); System.out.println(s3.length()); } }
s1 is "Data" (length 4). s2.substring(0,3) is "Str" (length 3). Concatenation is "DataStr" with length 7. But option C says 9, so re-check carefully.
Actually, s2 is "Structure" (length 9). substring(0,3) is "Str" (3 chars). Concatenation: "Data" + "Str" = "DataStr" (7 chars). So correct output is 7.
Option C is 7, so correct answer is A.
