String Handling
Section A
2005
Question 3
(f) 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)); [4]
(i) helloworld
(ii) 5
(iii) l
(iv) false
(g) Differentiate between toLowerCase() and toUpperCase() methods. [2]
The toLowerCase() converts all the characters of calling String object to lower case.
E.g.: String a= “HELLO”;
System.out.println(a.toLowerCase());
The output is hello
The toUpperCase() converts all the characters of calling String object to upper case.
E.g.: String a= “hello”;
System.out.println(a.toUpperCase());
The output is HELLO
2006
Question 2
(e) What will be the output for the following program segment?
String s = new String("abc");
System.out.println(s.toUpperCase()); [2]
ABC
Question 3
(g) Differentiate between compareTo() and equals() methods.
[2]
The compareTo() method compares two strings lexicographically. Returns integer 0 if both are same.
Returns > 0 if first one is greater than second. Returns <0 if first one is less than second.
The equals() method checks whether two strings are equal or not, considering the uppercase and lowercase.
Returns boolean value true if both are equal else false.
2007
Question 3
(d) Write a statement for each of the following:-
(i) Store a number 275 as a String
(ii) Convert the string to a numeric value
(iii) Add it to the existing total of 1000 to update the total. [3]
(i) String s = “275”;
(ii) int n = Integer.parseInt(s);
(iii) total+=n; //It assumes that previously total=1000;
(f) (ii) String S1 = "Hi";
String S2 = "Hi";
String S3 = "there"; String S4 = "HI";
System.out.println(S1 + "equals" + S2 + "->" + S1.equals(S2));
System.out.println(S1 + "equals" + S3 + "->" + S1.equals(S3));
System.out.println(S1+ "equals" + S4 + "->"+ S1.equals(S4));
System.out.println(S1 + "equalslgnoreCase"+S4+ "->"+S1.equalsIgnoreCase(S4)); [2]
(f) (ii)
HiequalsHi->true
Hiequalsthere->false
HiequalsHI->false
HiequalslgnoreCaseHI->true
2008
Question 2
(b) State the difference between = = operator and equals() method.
[2]
The == is a relational operator that is used to check equality of two primitive data type operands.
The equals() method is a String library method that is used to check the equality of two String objects.
Both of them return boolean value either true or false.
Question 3
(e) If, String x= "Computer";
String y = "Applications";
What do the following functions return for:
(i) System.out.println(x.substring(1,5));
(ii) System.out.println(x.indexOf(y.charAt(4)));
(iii) System.out.println(y+x.substring(5));
(iv) System.out.println(x.equals(y)); [4]
(i) ompu
(ii) -1
(iii) Applicationster
(iv) false
(h) Write a Java statement to input / read the following from the user using the keyboard.
(i) Character (ii) String [2]
(i) char c = (char)br.read();
(ii) String s = br.readLine();
2010
Question 2
(a) State the purpose and return data type of the following String functions:
indexOf() and compareTo() [2]
The indexOf() function is used to find the index of a particular character in a string. Its return type is int. E.g.: “Nandana”.indexOf(2); It returns 4.
The compareTo() function is used to find which string is greater or lesser. Its return type is int. If the first
string is higher than second then its return value is greater than 0 (zero). If second string is higher than the first then
its return value is less than 0 (zero). If both strings are same then its return value is 0.
(d) Write the difference between length and length() functions. [2]
The length identifier is used to find the length of an array, that means the number of cells in an array.
The length() function is used to find the length of a String object, i.e., the number of characters in a string.
(f) Write a statement each to perform the following task on a string:
a. Extract the second last character of a word stored in the variable wd.
b. Check if the second character of a string str is in uppercase. [2]
a. char c=wd.charAt(wd.length()-2);
b. if(Character.isUpperCase(str.charAt(1)))
2011
Question 2
(c) 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”)); [2]
Computerapplications
false
(d) Write the output of the following:
(i) System.out.println(Character.isUpperCase(‘R’));
(ii) System.out.println(Character.toUpperCase(‘j’)); [2]
true
J
Question 3
(f) Write a statement each to perform the following task on a string:
(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. [2]
(i) System.out.println(s.lastIndexOf(‘ ’));
(ii) double n = Double.parseDouble(x)
2012
Question 3
(e) 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)); [2]
false
true
(g) 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)); [2]
a : Data type: int. Value: 2
b : Data type: boolean. Value: false
(h) What will be the following code output?
String s=“malayalam”;
System.out.println(s.indexOf(‘m’));
System.out.println(s.lastIndexOf(‘m’)); [2]
0
8
2013
Question 2
(c) Write statements to show how to find length of character array char[ ] differs from finding the length of a String
object str. [2]
int l = a.length - To find the length of a character array assuming that name of the array is a.
int n = str.length() – To find the length of the string object str.
Question 3
(b) State the values stored in the variable str1 and str2
String s1 = “good”;
String s2 = “world matters”;
String str1 = s2.substring(5).replace(‘t’,’n’);
String str2 = s1.concat(str1); [2]
str1 = manners
str2 = good manners
2014
Question 3
(b) State the output of the following program segment:
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())); [2]
grinds
WHEAT
(f) What is the data type returned by the library functions:
(i) compareTo()
(ii) equals() [2]
(i) int
(ii) boolean
(g) State the value of characteristic and mantissa when the following code is executed.
String s= “4.3756”;
int n=s.indexOf(‘.’);
int characteristic = Integer.parseInt(s.substring(0,n));
int mantissa = Integer.valueOf(s.substring(n+1)); [2]
characteristic = 4
mantissa = 3756
2015
Question 2
(c) What is the value stored in variable res given below:
double res = Math.pow(“345”.indexOf(‘5’),3); [2]
8.0
Question 3
(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)); [2]
art
true
(e) (ii) Name a string function which removes the blank spaces provided in the prefix and suffix of a string. [2]
(ii) trim()
2016
Question 2
(a) State the difference between = operator and equals() method.
[2]
The = is the assignment operator. It assigns a value to a variable.
The equals() is a String class method. It checks the equality of two strings.
Question 3
(a) Give the output of the following string functions:
(i) “MISSISSIPPI”.indexOf(‘S’)+ “MISSISSIPPI”.lastIndexOf(‘I’)
(ii) “CABLE”.compareTo(“CADET”) [2]
(i) 12
(ii) -2
2017
Question 3
(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”); [2]
0
Today i Holiday
2018
Question 2
(a) System.out.print(“BEST ”);
System.out.print(“OF LUCK”);
Choose the correct option for the output of the above statements
(i) BEST OF LUCK
(ii) BEST
OF LUCK [2]
BEST OF LUCK
Question 3
(g) Give the output of the following string functions:
(i) “ACHIEVEMENT”.replace(‘E’,’A’)
(ii) “DEDICATE”.compareTo(“DEVOTE”) [2]
(i) ACHIAVAMANT
(ii) -18
2019
Question 3
(b) Write return type of the following function:
(i} startswith() [1]
(i) boolean
(g) Write the output of the following:
String s1=”phoenix”, s2=”island”;
System.out.println(s1.substring(0).concat(s2.substring(2)));
System.out.println(s2.toUpperCase()); [2]
phoenixland
ISLAND
(i) String x={"Artificial intelligence”, "IOT”, "Machine Learning", "Big data"};
Give the output of the following statements:
System.out.println(x[3]);
System.out.println(x.length); [2]
Big data
4
2020
Question 2
(b) State the difference between length and length() in Java. [2]
length: The length is predefined variable to store number of cells in an array. int n=a.length stores the
number of cells of array named a to the variable n.
length(): The length() is a predefined String class method to store the number of characters in a string value.
Question 3
(c) Write the output for the following:
String s1 = "Life is Beautiful";
System.out.println ("Earth" + s1.substring(4));
System.out.println(s1.endsWith("L")); [2]
Earth is Beautiful
false