String Handling — Question 6
Back to all questionsQuestion 6
What do the following functions return for :
String x = "hello";
String y = "world";(i) System.out.println(x + y);
(ii) System.out.println(x.length( ));
(iii) System.out.println(x.charAt(3));
(iv) System.out.println(x.equals(y));
(i) System.out.println(x + y);
Output
helloworld
Explanation
+ operator concatenates String objects x and y.
(ii) System.out.println(x.length( ));
Output
5
Explanation
length() returns the length of the String object. Since x has 5 characters, the length of the string is 5.
(iii) System.out.println(x.charAt(3));
Output
l
Explanation
chatAt() returns the character at the given index. Index starts from 0 and continues till length - 1. Thus, the character at index 3 is l.
(iv) System.out.println(x.equals(y));
Output
false
Explanation
equals() method checks for value equality, i.e., whether both objects have the same value regardless of their memory location. As values of x and y are not the same, so the method returns false.