Computer Applications
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));
Java String Handling
6 Likes
Answer
(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
.
Answered By
3 Likes
Related Questions
State the data type and values of a and b after the following segment is executed:
String s1 = "Computer", s2 = "Applications" a = (s1.compareTo(s2)); b = (s1.equals(s2));
What will the following code output:
String s = "malayalam"; System.out.println(s.indexOf('m')); System.out.println(s.lastIndexOf('m'));
If:
String x = "Computer"; String y = "Applications";
What do the following functions return?
(i) System.out.println(x.substring(1,5));
(ii) System.out.println(x.indexOf(x.charAt(4)));
(iii) System.out.println(y + x.substring(5));
(iv) System.out.println(x.equals(y));
Write the return type of the following library functions:
- isLetterOrDigit(char)
- replace(char, char)