Page 383 - Computer science 868 Class 12
P. 383
10. This method is used to delete the leading and trailing spaces from the string of the current object.
a. remove() b. trim()
c. indexOf() d. substring()
Answers
1. c 2. b 3. d 4. c 5. a 6. a 7. b 8. b 9. c 10. b
B. Fill in the blanks:
1. All letters, digits, space and punctuators together known as a ………………… .
2. ………………… method returns true if the character passed in the argument is an alphabet or a digit, else returns false.
3. The range of the ASCII value is ………………… .
4. ………………… method converts the string of the current string object to capital letters.
5. ………………… method checks whether the string of the current string object whose method is called is same as the string str in the
parameter.
6. ………………… is used for reversing the characters in the String.
7. ………………… returns true if there is a token left in the StringTokenizer object else returns false.
8. StringTokenizer object = new StringTokenizer(String variable, “…………………”);
9. ………………… is used to set the length of a string buffered after a specified range.
10. ………………… checks whether the string in the current object is ending with the string in the parameter.
Answers
1. String 2. Character.isLetterOrDigit(char) 3. 0 to 255
4. String toUpperCase() 5. equals(String str) 6. reverse()
7. hasMoreTokens() 8. Delimiter 9. setLength()
10. boolean endsWith(String str)
C. Answer the following questions:
1. What is the use of substring()?
Ans. This method is used for extracting all the characters from the index position startindex till the endindex-1. It returns a String.
2. What is the difference between indexOf() and charAt() function?
Ans. indexOf(): In Java, the indexOf() method is used to find the index of a specified character or substring within a string. It returns
the index of the first occurrence of the specified character or substring, or -1 if the character or substring is not found.
charAt() : In Java, the charAt() method is used to retrieve the character at a specific index within a string. It returns the character
at the specified index, or it throws an IndexOutOfBoundsException if the index is out of range.
3. What is the difference between String and StringBuffer in Java?
Ans. a. The String class is immutable. The StringBuffer class is mutable.
b. String is slow and consumes more memory when we concatenate too many strings because every time it creates new instance.
StringBuffer is fast and consumes less memory when we concatenate too many strings.
c. String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.
StringBuffer class doesn’t override the equals() method of Object class.
d. String class is slower while performing concatenation operation. StringBuffer class is faster while performing concatenation
operation.
4. How will you convert String to int in Java?
Ans. The parseInt() method takes a String as input and returns the corresponding int value. If the String does not represent a valid
integer, a NumberFormatException will be thrown. For example:
String str = “12345”;
int number = Integer.parseInt(str);
5. How Wrapper class is used in String manipulation?
Ans. int number = 123;
String str = Integer.toString(number);
381
Strings 381

