1. To extract the last character of a word stored in a variable and replace a substring in a given string.
2. To calculate the sum of several mathematical series involving factorials and print the results.
3. To perform various string operations like extracting substrings, comparing strings, checking cases, and more.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
63 views
Computer
1. To extract the last character of a word stored in a variable and replace a substring in a given string.
2. To calculate the sum of several mathematical series involving factorials and print the results.
3. To perform various string operations like extracting substrings, comparing strings, checking cases, and more.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
To extract the last character of a word(wd) stored in the variable chr.
To replace the word "old" with the word "new" in a given String st = "old is always old"
i. Parameterised constructor to initialise the data members.
ii. ii. To calculate and print the sum of the following series: x+x/2!+x/3!+x/4!+...+x/n! iii. To calculate and print the sum of the following series: x/2!+x2/3!+x3/4!+x4/5!+…+xn/(n+1)! To calculate and print the sum of the following series: x/2! - x2/3!+x3/4! - x4/5!+…±xn/(n+1)! where the symbol ! stands for factorial eg. 5!=5*4*3*2*1, 3!=3*2*1 iv. Answer as directed 1. Give the output of the following program fragment: String s=new String(“He went to the market”); String r; r=s.replace(“went”,“is going”); System.out.println(r); Ans. Output: He is going to the market 2. Give the output of the following program fragment: String s=“String”; int a=12,b=45; System.out.println(s+a+b); System.out.println(a+s+b); System.out.println(a+b+s); Ans. Output: String1245 12String45 57String 3. Give the output of the following program fragment: String s=“india”,s1=“IndIA”,s2=s; System.out.println(s.equals(s1)); System.out.println(s.equalsIgnoreCase(s1)); System.out.println(s2==s); System.out.println(s.toUpperCase()==s1.toUpperCase()); System.out.println(s.startsWith(“IN”.toLowerCase())); System.out.println(s1.endsWith(“iA”.toUpperCase())); Ans. Output: false true true false true true Computer Applications – X (ICSE Course) Answers 296 4. What do the following functions return for: String x =“hello”; String y =“world” System.out.println(x + y); System.out.println(x.length(); System.out.println(x.charAt(3)); System.out.println(x.equals(y)); Ans. Output: helloworld 5 l false 5. What is the output of the following: (i) System.out.println (“four :” + 4 + 2); System.out.println (“four :”+(2+2)); (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 + “equalsIgnoreCase” +S4 + “→” + S1.equalsIgnoreCase(S4)); Ans. (i) four :42 four :4 (ii) Hi equals Hi→true Hi equals there→false Hi equals HI→false Hi equalsIgnoreCase HI→true 6. 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 297 String Manipulation 7. What will be the output for the following program segment? String s = new String(“abc”); System.out.println(s.toUpperCase()); Ans. ABC 8. What will be the output of the following code? char x = ‘A’; int m; m=(x= =’a’) ? ‘A’ : ‘a’; System.out.println(“m=”+m); Ans. m=97 9. Write statements to show how finding the length of a character array and char[] differs from finding the length of a String object str. Ans. chars.length str.length() 10. 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 Ans. (i) System.out.println(s.lastIndexOf(“ ”)); (ii) double d=Double.parseDouble(x); 11. Write a statement each to perform the following task on a string: (i) Extract the second last character of a word stored in the variable wd. (ii) Check if the second character of a string str is in uppercase. Ans. (i) char sl=wd.charAt(wd.length()-2); (ii) if(Character.isUpperCase(str.charAt(1))) 12. Give the output of the following string functions: (i) “ACHIEVEMENT”.replace(‘E’,‘A’) (ii) “DEDICATE”.compareTo(“DEVOTE”) Ans. (i) ACHIAVAMANT (ii) -18 13. Consider the following String array and give the output: String arr[]={“DELHI”, “CHENNAI”, “MUMBAI”, “LUCKNOW”, “JAIPUR”}; System.out.println(arr[0].length()>arr[3].length()); System.out.print(arr[4].substring(0,3));