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.
Write the difference between length and length() functions. 2010
Ans.
length is a property of an array which gives the size of the array. length() is a function of the
String class which returns the size of the String.
Write a statement each to perform the following task on a string: 2010
(i) Extract the second last character of a word stored in the variable wd.
Ans.
char ch = wd.charAt(wd.length()-2);
(ii) Check if the second character of a string str is in uppercase.
Ans.
boolean result = Character.isUpperCase(str.charAt(1));
Give the output of the following:
String n = “Computer Knowledge”;
String m = “Computer Applications”;
System.out.println(n.substring (0,8). concat (m.substring(9)));
System.out.println(n.endsWith(“e”));
Ans. n.substring(0,8) gives “Computer”. m.substring(9) gives “Applications”. These two on
concatenation gives “ComputerApplications”. n ends with “e”. So, it gives true.
The output is:
1 ComputerApplications
2 true
Write the output of the following: 2011
(i) System.out.println (Character.isUpperCase(‘R’));
(ii) System.out.println(Character.toUpperCase(‘j’));
Ans
(i) true
(ii) J
Write a statement each to perform the following task on a string: 2011
(i) Find and display the position of the last space in a string s.
(ii) Convert a number stored in a string variable x to double data type
Ans.
(i) System.out.println(s.lastIndexOf(” “);
(ii) Double.parseDouble(x)
State the output of the following program segment.
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
State the method that:
(i) Converts a string to a primitive float data type
(ii) Determines if the specified character is an uppercase character
Ans.
(i) Float.parseFloat(String)
(ii) Character.isUpperCase(char)
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));
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
What will the following code output? [2]
String s = "malayalam";
System.out.println(s.indexOf('m'));
System.out.println(s.lastIndexOf('m'))
Ans.
0
8
State the values of n and ch
char c = 'A':
int n = c + 1;
Ans.
The ASCII value for ‘A’ is 65. Therefore, n will be 66.
State the values stored in variables str1 and str2 2013
String s1 = "good";
String s2="world matters";
String str1 = s2.substring(5).replace('t','n');
String str2 = s1.concat(str1);
Ans.
s2.substring(5) gives ” matters”. When ‘t’ is replaced with ‘n’, we get ” manners”. “good” when
concatenated with ” manners” gives “good manners”.
So, str1 = ” manners” and str2 = “good manners”.
What is the data type that the following library functions return?
i) isWhitespace(char ch)
ii) Math.random()
Ans.
i) boolean
ii) double
State the output of the following program segment: 2014
String str1= "great"; String str2= "minds";
System.out.println(str1.substring(0,2).concat(str2.substring(1)));
System.out.println(("WH"+(str1.substring(2).toUpperCase())));
Ans
grinds
WHEAT
What is the data type returned by the library functions 2014
(i) compareTo()
(ii) equals()
Ans
(i) int
(ii) boolean
State the value of characteristic and mantissa when the following code is executed. 2014
String s = "4.3756";
int n = s.indexOf('.');
int characteristic=Integer.parseInt(s.substring(0,n));
int mantissa=Integer.valueOf(s.substring(n+1));
Ans
4
3756
2015 (c) What is the value stored in variable res given below :
double res = Math.pow("345".indexOf('5'),3);
ans
(c) 8.0
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()
2016 (a) Give the output of the following string functions :
(i) "MISSISSIPPI".indexOf('S')+ "MISSISSIPPI".lastIndexOf('I')
(ii) "CABLE".compareTo("CADET")
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
2017 (f) Write the output for the following:
String s="Today is Test" ;
System.out.println(s.indexOf('T'));
System.out.println(s.substring(0,7) + " " +"Holiday");
ans
(f)
0
Today i Holiday [OR] Today Holiday
2017 (h) Give the output of the following code:
String A ="26", B="100";
String D=A+B+"200";
int x= Integer.parseInt(A);
int y = Integer.parseInt(B);
int d = x+y;
System.out.println("Result 1 = "+D);
System.out.println("Result 2 = " +d);
ans
(h)
Result 1= 26100200
Result 2=126
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
2019 (g) Write the output for the following:
String s1 = ''phoenix''; String s2 =''island'' ;
System.out.println (s1.substring(0).concat (s2.substring(2) ) );
System.out.println(s2.toUpperCase());
ans
g)
phoenixland
ISLAND