Worksheet on String Handling
Ques1: State the purpose and return type of the following functions:
i) indexOf()
ii) compareTo() [ICSE 2010]
Ques2: Give the output of the following: [ICSE 2011]
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”));
Ques3: a) State the output of the following program segment.
String s=” Examination”; [ICSE 2012]
int n =s.length();
System.out.println(s.startsWith(s.substring(5,n)));
System.out.println(s.charAt(2)== s.charAt(6));
b) State the values stored in variable str1 and str2. [ICSE 2013]
String s1=”good”;
String s2= “world matters”;
String str1=s2.substring(5).replace(‘t’,’n’);
String str2=s1.concat(str1);
Ques4: What is the data type returned by the library functions?
[ICSE 2014]
i) compareTo()
ii) equals()
Ques5: State the difference between == operator and equals()
method. [ICSE 2016]
Ques6: Give the output of the following String functions: [ICSE 2016]
i. “MISSISSIPPI”. indexOf(‘S’)+ ”MISSISSIPPI”.lastIndexOf(‘I’)
ii. “CABLE”.compareTo(“CADET”)
Ques7: Give the output of the following statements:
i. System.out.println(“One”+ 1+ 1);
ii. System.out.println (2 + 2 + “Two”);
Ques8:
String x[]={“SAMSUNG”, “NOKIA”, “SONY”, MICROMAX”, “BLACKBERRY”};
Give the output of the following statements: [ICSE 2017]
i. System.out.println(x[1]);
ii. System.out.println(x[3].length());
Ques9: Write the return type of the following functions:
i. isLetterOrDigit(char) [ICSE 2016]
replace(char,char)
ii. compareTo() [ICSE 2014]
equals()
iii. endsWith() [ICSE 2018]
log()
Ques10: 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));
Ques11: Give the output of the following String functions:[ICSE 2018]
i. “ACHIEVEMENT”.replace(‘E’,’A’)
ii. “DEDICATE”.compareTo(“DEVOTE”)
Ques12: Consider the following String array and give the output:
[ICSE 2018]
String arr[]={“DELHI”, “CHENNAI”,”MUMBAI”,”LUCKNOW”,”JAIPUR”};
System.out.println(arr[0].length()> arr[3].length());
System.out.println(arr[4].substring (0,3));
Ques13: Write statements to show how finding the length of a character array
char[] differs from finding the length of a String
object str . [ICSE 2013]
Ques14: Write a statement to perform each of the following tasks on a String.
[ICSE 2012]
i. Find and display the position of the last space in a String s. ii.
Convert a number stored in a variable x to double data type.
Ques15: Write the output:
String st=” No pressure no diamonds”;
String str=” Your”;
System.out.println(st.substring(7,11)+”ly”);
System.out.println(str.compareTo(“you”));
Ques 16 :
Write a program to input a word and replace all vowels with * .
Ques 17:
Write a program to input a word in lower case and display the new word after
removing all the repeated letters.
Sample input : applications
Output: aplictons
Ques 18:
Write a program that encodes a word into Piglatin. To translate word, convert
the word into uppercase and then place the first vowel of the original word as
the start of the new word along with the remaining alphabets. The alphabets
present before the vowel being shifted towards the end followed by “AY”.
[2013]
Sample input : LONDON
Sample output: ONDONLAY
Sample input : SKY
Sample output: SKYAY
Ques 19:
Write a menu driven program to display the given pattern as per user choice:
[2018]
Pattern 1 for ABCDE Pattern2 for BLUE
ABCDE B
ABCD LL
ABC UUU
AB EEEE
A
Take the word to be used in pattern as input.
Ques 20:
Write a program to assign a full path and file name as given below. Using library
functions extract and print the file path, file name and file extension separately
as shown below: [2014]
Input : C:\Users\admin\Pictures\Flowers.jpg
Output: Path : C:\Users\admin\Pictures\
File name : Flowers
Extension : jpg
Ques 21:
Write a program to initialize the seven Wonders of the world along with their
location in two different arrays. Search for a name of the country input by the
user. If found, display the name of the country along with its Wonder, otherwise
display “Sorry not found”. [2016]
Seven wonders: CHICHEN ITZA, CHRIST THE RDEEMER, TAJ MAHAL, GREAT
WALL OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM
Locations: MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Ques 22:
Write a program to input two words (where length of second string is not more
than first) ,check whether second word is present in first word or not. SPrint
appropriate message for the same.
Example:
Input : S1: CATENATION
S2: TEN
Output: Present at 3 position.