Page 318 - ComputerScience_Class_11
P. 318
D. Higher Order Thinking Skills (HOTS)
1. A programmer is developing a password validation system. The system must check whether each character entered by the user
is a letter or a digit and also ensure that uppercase and lowercase letters are handled properly. Which class and methods should
the programmer use? Explain how this will help in proper validation.
Ans. The programmer should use the Character class. Methods such as Character.isLetter(), Character.isDigit(), Character.isUpperCase()
and Character.isLowerCase() should be used. The isLetter() method will check whether the character is an alphabet and isDigit()
will verify if it is a number. The isUpperCase() and isLowerCase() methods help in checking the case of the letter. Using these
methods ensures accurate validation of each character and helps in creating a secure password validation system.
2. A student writes the following code:
String str = "HELLO";
str.toLowerCase();
System.out.println(str);
However, the output printed is still "HELLO". Identify the reason and explain how the problem can be corrected.
Ans. The output remains "HELLO" because Strings are immutable. The toLowerCase() method does not change the original string,
instead, it returns a new String object with the converted value.
To correct the problem, the returned value must be stored in a variable, as shown below:
str = str.toLowerCase();
System.out.println(str);
E. Assertion and reasoning questions.
The following questions consist of two statements – Assertion (A) and Reason (R). Answer these questions by selecting the
appropriate option given below:
a. Both A and R are true and R is the correct explanation of A.
b. Both A and R are true but R is not the correct explanation of A.
c. A is true but R is false.
d. A is false but R is true.
1. Assertion (A): The length() function of the String class returns the total number of characters present in a string.
Reason (R): The length() function returns an integer value representing the size of the string.
Ans. a. Both A and R are true and R is the correct explanation of A.
2. Assertion (A): The toUpperCase() function modifies the original string permanently.
Reason (R): Strings are immutable, so any change results in the creation of a new String object.
Ans. d. A is false but R is true.
3. Assertion (A): The indexOf(char ch, int index) function starts searching for the character from the specified index value.
Reason (R): The function returns the index of the first occurrence of the character beginning the search from index 0 only.
Ans. c. A is true but R is false.
F. Case study-based questions.
Aman is developing a small text processing program in Java. He stores a sentence in a String variable as:
String str = "Java Programming Language";
First, he wants to find the total number of characters present in the string using the length() function. Then, he converts the entire
string into lowercase using the toLowerCase() function. After that, he searches for the position of the character 'a' starting from
index 5 using the indexOf(char ch, int index) function. Later, Aman decides to modify the string by deleting a few words using the
delete() method of the StringBuffer class. While working, he learns that the original String value does not change automatically after
using toLowerCase() because Strings are immutable.
Based on the given case, answer the following questions:
1. Which function is used to find the total number of characters in the string?
a. charAt() b. length()
c. indexOf() d. delete()
316 Touchpad Computer Science (Ver. 3.0)-XI

