Page 317 - ComputerScience_Class_11
P. 317
2. Explain any three methods of the Character class.
Ans. The methods of Character class are as follows:
a. Character.isLetter(char): This function checks whether the given character is a letter or not and returns true if it is a letter.
b. Character.isDigit(char): This function checks whether the given character is a digit or not and returns true if it is a digit.
c. Character.isUpperCase(char): This function returns true if the alphabet is in capital letters.
3. What is a String class? Explain with suitable examples.
Ans. The String class is used to create and handle strings, which are a combination of characters written within double quotation
marks. A string can store a word, sentence, number (as text) or a combination of letters and symbols.
To assign a value to a String variable, the following syntax is used:
String variable = "String Literal";
4. Explain the length() function of the String class with an example.
Ans. The length() function returns the number of characters present in a string. It returns an integer type of value. The length() is the
only function where the counting starts from 1.
The syntax is:
int <variable> = String_datatype_Variable.length();
Example:
class LengthDemo
{
public static void main(String args[])
{
String str = "Computer";
int len = str.length();
System.out.println("Length of the string is: " + len);
}
}
5. What is the use of indexOf(char ch, int index) function? Also write its syntax.
Ans. This function is used to return the index value of the given character present in the string of the current String object, starting the
search from the index provided in the parameter. It returns an integer type value.
The Syntax is:
int <variable> = String_datatype_Variable.indexOf(char ch, int index);
6. Differentiate between toLowerCase() and toUpperCase() functions.
Ans. The following are the differences between toLowerCase() and toUpperCase() functions:
toLowerCase() FUNCTION toUpperCase() FUNCTION
This function converts all the characters of a string This function converts all the characters of a string
into lowercase letters. into uppercase letters.
If any letter is already in lowercase, there will be If any letter is already in uppercase, there will be
no change in the output. no change in the output.
7. What is the StringBuffer class? Explain its purpose.
Ans. The StringBuffer class is used to create sufficient space in the memory to store a string. This space in memory can be changed
as required. If the string is less than the size defined, the extra space will contain an extra value. Let us learn about some of the
commonly used methods of the StringBuffer class.
8. Write a program to delete a part of a string using the delete() method in the StringBuffer class.
Ans. class StringDelete
{
public static void main(String[] args)
{
StringBuffer s1 = new StringBuffer("I am not going to play");
s1.delete(5, 8);
System.out.println(s1);
}
}
Strings 315

