String Handling
String Handling
String Handling
a. CharAt(0 b. charAt()
c. CharAT) d. charat()
a. java.awt b. java.lang
C. java.applet d. java.string
3. Which of these methods is used to find the number of characters ina string?
a. sizeOf() b. Length()
C. length() d. lengthOf()
- Answerss
1. b 2. b 3. c 4. a 5. d 6. b 7. a 8. b 9. C
-Subjective Questions
Very Short/Short Answer Questions te
1. Differentiate between charAt() and indexOf().
Ans. charAt() returns the character at the given index whereas indexOf() returns the index of the first occurrence of
the given character.
2. Differentiate between equals() and compareTol).
Ans. equals() checks if two strings are identical or not, whereascompareTolcompares two strings lexicographically
and returns the difference //in the UNICODE values of the first pair of dissimilar characters.
5. From the table given below, write the Java function that has been used to generate the output shown:
Variable Output
() s="Payable" "able"
(i) t"feet" "foot"
"C:\Users\admin\Pictures\tree.jpg
Yenl. out.println("Path :"+ s.substring (0, s.lastindexot CT)+1)):
System.out.println ("File name:"+ s.substring (s. lastIndexor ("\")+1,
s.indexOf ('. ')));
System.out.println ("Extension "+ S.substring (s.indexof°.')+1)):
7. Using Java library functions, write statements with appropriate datatypes for the following:
Find the position of the first occurrence of the letter 'P" in string str and store it in n.
(i) Concatenate string City with string Pincode and store in string Address.
Ans. (i) int n=str.indexOf(P');
(i) String Address=City.concat(Pincode);
8. Give the output of the following Java statements:
11. State the value of characteristic and mantissa when the following code is executed:
String s ="4.3756"
int n = S.indexof ( ' . ' ) ;
int m= ch;
m=m + 5b;
Today is Test";
System.out.println (s.indexof ('T')):
System.out.println (s.substring (0, 7) +" + "Holiday")
Ans. 0
Today i Holiday
14. State the difference between == operator and equals() method. [ICSE, 2016]
Ans. The == operator compares two numeric or character values for equality.
The equals() method compares the contents of two String values for equality.
() "MISSISsIPPI".indexof"'s') + "MISSISSIPPI".lastindexOf('")
(i) isLetterOrDigit(char)
(i) replace(char, char) [ICSE, 2016]
Ans. (i) boolean (i) String
17. State the output when the following program segment is executed: [ICSE, 2015]
String a =
" Smartphone" , b
=
"Graphic Art";
String h =a.substring (2, 5);
();
String k =
b.substring(8) . toUpperCase
S y s t e m . o u t . p r i n t l n (h) ;
(k.equalsIgnoreCase (h) );
System.out.println
Ans. art
true
8. State the output of the following program segment:
str2 "minds";
String strl =
"great "; String
(0, 2) .concat (str2. substring (1))) :
S y s t e m . o u t . p r i n t l n (strl.substring
20. State the values stored in the variables str1 and str2:
String sl = "good";
String strl =
s2.substring (5) .replace ('t', 'n')i
String str2 = sl.concat (strl); [ICSE, 2013
Ans. str1:manners
str2 good manners
22. 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) ) ;
Ans.
Variable
Data type int boolean
Value 2 false
23. What will the following code output:
String s = "malayalam";
System.out.println (s.indexof ( 'm'));
[ICSE, 2013]
System. out.println (s. last Indexof ('m '));
Ans.0
8