Page 1 :
Saturday, June 05, 2021, 8:10 PM, , COMBINED ICSE 246 Page 1
Page 7 :
iii. System.out.println(s2.replace(„p‟,„s‟));, iv.System.out.println(s1.charAt(s1.indexOf(„R‟)+ s2.indexOf(„R)));, , Practice questions with Answers:, 1. 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));, Ans: (i) helloworld (ii) 5 (iii) l (iv) false, 2. What will be the output for the following program segment?, String s = new String("abc");, System.out.println(s.toUpperCase());, Ans: ABC, 3. Write a statement for each of the following:, 1. Store a number 275 as a String, 2. Convert the String to a numeric value, 3. Add it to the existing total of 1000 to update the field, Ans:, 1. String s = “” + 275;, 2. String n = Integer.parseInt(s);, 3. int field = 1000; field = field + n;, , 4. 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(x.charAt(4)));, iii) System.out.println(y+x.substring(5));, iv) System.out.println(x.equals(y));, Ans :, i) ompu, ii) 4, iii) Applicationster, iv) false, 5. Write a statement each to perform the following task on a string:, i) Extract the second last character of a word stored in a variable wd., ii) Check if the second character of a string str is in uppercase., Ans:, i) char c = wd.charAt( wd.length() – 2 );, ii) if( Character.isUpperCase( str.charAt(1) ) ), 6. Write the output of the following:, i) System.out.println(Character.isUpperCase(„R‟));, ii) System.out.println(Character.toUpperCase(„j‟));, Ans:, i) true, ii) J (Note: This is the uppercase J and not the lowercase j), 7. 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, 8. State the values stored in variables str1 and str2, String s1 = "good"; String s2 = "world matters";, String str1 = s2.substring(5).replace('t','n');, String str2 = s1.concat(str1);, Ans:, , COMBINED ICSE 246 Page 7
Page 8 :
str1 = ” manners” (this is a space followed by manners), str2 = “good manners”, , 9. 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( ))));, Ans., grinds, WHEAT, 10. What is the value stored in variable res given below :, res = Math.pow("345".indexOf('5'),3);, Ans :, The value of res will be 8.0 in double data type, , double, , 11. Give the output of the following string functions :, (i) “MISSISSIPPI”.indexOf(„S‟) + “MISSISSIPPI”.lastIndexOf(„I‟), (ii) “CABLE”.compareTo(“CADET”), Ans., (i) “MISSISSIPPI”.indexOf(„S‟) + “MISSISSIPPI”.lastIndexOf(„I‟), = 2 + 10, = 12, (ii) String‟s compareTo method compares ASCII values of characters starting from the left., “CABLE”.compareTo(“CADET”), The first and seconds characters are the same in both the strings. So, we compare the, third characters., Compare B and D = ASCII value of B – ASCII value of D, = 66 – 68, = -2, 12. 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., s.indexOf(„T‟) = 0, s.substring(0,7)+ ” “+ “Holiday”, = “Today is Test”.substring(0,7) + ” ” + “Holiday”, = “Today i” + ” ” + “Holiday”, = “Today i Holiday”, Output is, 0, Today i Holiday, 13. Give the output of the following string functions:, (i) “ACHIEVEMENT”.replace(„E‟, „A‟), (ii) “DEDICATE”.compareTo(“DEVOTE”), Ans., i) ACHIAVEMANT, ii) The first two characters are same. So, we take the ASCII values of the third characters, and subtract to get the result., D – V = 68 – 86 = 18, Question 2: State the difference between the string objects created using String and, StringBuffer classes., Question 3: Write the purpose and return type of following functions:, (a) endswith( ) and lastIndexOf( ), (b) charAt( ) and indexOf( ), (c) compareTo( ) equals( ), (d) equalsIqnoreCase( ) and equals( ), , COMBINED ICSE 246 Page 8