6 String Functions
6 String Functions
State the purpose and return data type of the following String functions: 2010
(i) indexOf ( )
(ii) compareTo ( )
Ans.
i) indexOf() returns the index of the character or String passed as the parameter in the string on
which is invoked.
Return type is int.
ii) compareTo() lexicographically compares the String passed as an argument to the String on
which it is invoked.
Return type is int.
String s = "Examination";
int n = s.length();
System.out.println(s.startsWith(s.substring(5, n)));
System.out.println(s.charAt(2) == s.charAt(6));
Ans.
false
true
Explanation : n = 11
s.startsWith(s.substring(5, n)) = s.startsWith ( “nation” ) = false
( s.charAt(2) == s.charAt(6) ) = ( ‘a’== ‘a’ ) = true
Ans.
Data type of a is int and b is boolean.
ASCII value of ‘C’ is 67 and ‘A’ is 65. So compare gives 67-65 = 2.
Therefore a = 2
b = false
Ans.
0
8
char c = 'A':
int n = c + 1;
Ans.
The ASCII value for ‘A’ is 65. Therefore, n will be 66.
What is the data type that the following library functions return?
i) isWhitespace(char ch)
ii) Math.random()
Ans.
i) boolean
ii) double
2015 (a) State the data type and value of y after the following is executed :
char x='7';
y= Character.isLetter(x);
ans
(a) Data type is boolean and y=false
2015 (c) State the output when the following program segment is executed :
String a="Smartphone", b="Graphic Art";
String h=a.substring(2,5);
String k= b.substring(8).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h)) ;
ans
(c)
art
true
2015 (ii) Name a string function which removes the blank spaces provided in the prefix
and suffix of a string .
ans
(ii) trim()
ans
(a)
(i) 2 +10 = 12
(ii) - 2
2016 (a) State the difference between == operator and equals ( ) method
ans
(a) == is an operator which is used to check the equality between primitive data type
equals () function checks the equality between Strings
2016
(j)
Write the return type of the following library functions:
(i) isLetterOrDigit(char)
(ii) replace(char, char)
ans
(j) boolean and String
2017 (c) State the data type and value of res after the following is executed:
char ch='t';
res= Character.toUpperCase(ch);
(c)
char T
2018 (c) Write a difference between the functions isUpperCase( ) and toUpperCase( ).
isUpperCase toUpperCase
CHECKS whether a given character is an CONVERTS the character to its is an
uppercase letter or not uppercase
Output of this function is boolean Output of this function is character
2018 (e)
Write the return data type of the following function.
(i)
endsWith()
(ii)
log()
ans
(e)
(i)
boolean
(ii)
double
2018 g) Give the output of the following string functions:
(i) "ACHIEVEMENT".replace('E', 'A')
(ii) "DEDICATE".compareTo("DEVOTE")
ans
(g)
(i) ACHIAVAMANT
(ii) -18
2019 (b) Write the return data type of the following functions:
(i) startsWith( )
(ii) random( )
(b)
(i) boolean
(ii) double
ans
g)
phoenixland
ISLAND