String Handling STD X
String Handling STD X
Question 1
A string is:
1. A set of letters
2. A set of letters and digits
3. A set of letters, digits and special characters
4. All the above
Answer
Reason — A string literal may consist of a set of letters, a set of letters and digits or a set of
letters, digits and special characters.
Question 2
1. An integer array
2. A numeric array
3. A character array
4. A double type array
Answer
A character array
Reason — String data type creates a character array to store a set of characters in different
cells.
Question 3
If a string contains 12 characters, what will be the index of the last character?
1. 12
2. 11
3. 10
4. 0
Answer
11
Reason — A String data type manages character array for storing a string character-wise
starting from 0th subscript. Hence, the index number of the characters starts from 0 (zero) and
ends with (k-1), if k is length of the string. Hence, the last index will be 11 (12-1), as 12 is the
size of the string.
Question 4
What will be the value stored in the variable 'c' if the following statement is executed?
c = "COMPUTER".charAt("COMPUTER ".indexOf('P'))
1. 3
2. 4
3. P
4. M
Answer
Reason — charAt(int index) returns a character from the given index of the string and
indexOf(char ch) returns the index of first occurrence of a character in the string.
Since P is at 3rd position so indexOf('P') will return 3 and charAt(3) function will return the
character at the third index i.e., 'P'. So, the given statement is solved as follows:
c = "COMPUTER".charAt("COMPUTER ".indexOf('P'))
⇒ c = "COMPUTER".charAt(3)
⇒ c = 'P'
Question 5
Which of the following functions is used to remove leading and trailing white spaces from
the string?
1. trim( )
2. trail( )
3. truncate( )
4. slice( )
Answer
trim( )
Reason — trim( ) function is used to remove leading and trailing white spaces from the
string.
Question 6
Answer
False
Reason — equals() method treats uppercase and lowercase characters differently. So, the two
strings will not be considered equal and the boolean value 'false' will be returned.
Question 7
1. 8
2. 0
3. -8
4. 2
Answer
-8
Reason — compareTo() returns a negative value if the first string is smaller than the second
string in terms of the ASCII values of the corresponding characters.
"AMAN" and "AMIT" differ at the third character with 'A' and 'I', respectively. So, output
of compareTo() method will be ASCII Code of 'A' - ASCII Code of 'I' ⇒ 65 - 73 ⇒ -8.
Question 8
1. java.awt
2. java.string
3. java.math
4. java.lang
Answer
java.lang
Reason — Java Class Library contains a class called String which is available under
java.lang package.
Question 2
Question 3
Question 4
Question 5
Question 6
Question 7
Question 8
Question 1
Answer
Question 2
Answer
Answer
Question 4
Answer
Question 5
To replace the word "old" with the word "new" in a given String st = "old is always old"
Answer
Question 6
Answer
Question 1
Output
UnderstandingComputer Applications10
Explanation
In the first line, + operator concatenates 1 and 0 to the end of "Computer Applications"
so str becomes "Computer Applications10". Next line prints "UnderstandingComputer
Applications10" to the console.
Question 2
String n1 = "46", n2 = "64";
int total = Integer.parseInt(n1) + Integer.parseInt(n2);
System.out.println("The sum of " + "46 " + "and" + " 64" + " is " +
total);
Answer
Output
Explanation
Integer.parseInt() method will convert the strings n1 and n2 to their corresponding numerical
integers. So, 46 and 64 are added as numerical values and the result 110 is stored in int
variable total.
Question 3
boolean p;
p = ("BLUEJ".length() > "bluej".length()) ? true: false;
Answer
Output
false
Explanation
Both "BLUEJ" and "bluej" have the same length 5. So, condition of ternary operator is false
and false is assigned to boolean variable p.
Question 4
Output
Explanation
str.indexOf('n') will return the first index of n in str which is 1. So, the output of this
program is 1.
Question 5
String str1 = "Information Technology";
String str2 = "information technology";
boolean p = str1.equalsIgnoreCase(str2);
System.out.println("The result is " + p);
Answer
Output
Explanation
Question 6
(b) System.out.println(x.length());
(c) System.out.println(x.charAt(3));
(d) System.out.println(x.equals(y));
Answer
Output
Vision2020
Explanation
Output
Explanation
Output
i
Explanation
Output
false
Explanation
Question 7
Output
Explanation
As the strings S1, S2, S3 and S4 differ in the case of the characters so equals() method will
return false but equalsIgnoreCase() method will return true.
Question 8
If:
String x = "Computer";
String y = "Applications";
(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));
Answer
(i) System.out.println(x.substring(1,5));
Output
ompu
Explanation
x.substring(1,5) will return a substring of x starting at index 1 till index 4 (i.e. 5 - 1 = 4).
(ii) System.out.println(x.indexOf(x.charAt(4)));
Output
Explanation
x.charAt(4) returns the character at index 4 of string x which is 'u'. First index of 'u' in x is 4
so output is 4.
Output
Applicationster
Explanation
x.substring(5) will return the substring of x starting at index 5 till the end of the string. It is
"ter". This is added to the end of string y and printed to the console as output.
(iv) System.out.println(x.equals(y));
Output
false
Explanation
Question 9
Output
ComputerApplications
true
Explanation
Question 10
(ii) System.out.println(x[3].length());
Answer
(i) System.out.println(x[1]);
Output
NOKIA
Explanation
Output
Explanation
x[3].length() gives the number of characters in the fourth element of the array x.
Question 11
(ii) "DEDICATE".compareTo("DEVOTE")
Answer
(i) "ACHIEVEMENT".replace('E', 'A')
Output
ACHIAVAMANT
Explanation
(ii) "DEDICATE".compareTo("DEVOTE")
Output
-18
Explanation
"DEDICATE" and "DEVOTE" differ at the third character with 'D' and 'V', respectively. So,
output of compareTo() method will be ASCII Code of 'D' - ASCII Code of 'V' ⇒ 68 - 86 ⇒ -
18.
Question 12
Output
false
JAI
Explanation
As length of the string "DELHI" is less than that of "LUCKNOW" so first output is false.
arr[4].substring(0,3) will return the substring of "JAIPUR" starting at index 0 till index 2
(i.e. 3 - 1 = 2).
Question 1
equals() and ==
Answer
equals() ==
It is used to check if the contents of two strings are It is used to check if two variables refer to the same obje
same or not. in memory.
Example: Example:
String s1 = new String("hello"); String s1 = new String("hello");
String s2 = new String("hello"); String s2 = new String("hello");
boolean res = s1.equals(s2); boolean res = s1 == s2;
System.out.println(res); System.out.println(res);
The output of this code snippet is true as contents of The output of this code snippet is false as s1 and s2 poin
s1 and s2 are the same. to different String objects.
Question 2
Answer
compareTo() equals()
The result is a negative, positive or zero integer value depending on whether The result is true if the contents
the String object precedes, follows or is equal to the String argument. are same otherwise it is false.
Question 3
Answer
toLowerCase() toUpperCase()
Converts all characters of the String object to lower Converts all characters of the String object to upper
case. case.
Example: Example:
String str = "HELLO"; String str = "hello";
System.out.println(str.toLowerCase()); System.out.println(str.toUpperCase());
toLowerCase() toUpperCase()
Output of this code snippet will be hello. Output of this code snippet will be HELLO.
Question 4
Answer
charAt() substring()
It returns a character from the string at the It extracts a part of the string as specified by its arguments and
index specified as its argument. returns the extracted part as a new string.
Example: Example:
String str = "Hello"; String str = "Hello";
char ch = str.charAt(1); String subStr = str.substring(1);
System.out.println(ch); System.out.println(subStr);
Question 1
Answer
An exception is an event, which occurs during the execution of a program, that disrupts the
normal flow of the program's instructions. Two exception handling blocks are try and catch.
Question 2
State the purpose and return data type of the following String functions:
(a) indexOf( )
(b) compareTo( )
Answer
(a) indexOf( ) — It returns the index within the string of the first occurrence of the specified
character or -1 if the character is not present. Its return type is int.
(b) compareTo( ) — It compares two strings lexicographically. It results in the difference of
the ASCII codes of the corresponding characters. Its return type is int.
Question 3
(i) Extract the second last character of a word stored in the variable wd.
Answer
(i) Extract the second last character of a word stored in the variable wd.
Question 4
(i) Find and display the position of the last space in a string s.
Answer
(i) Find and display the position of the last space in a string s.
System.out.println(s.lastIndexOf(' '));
(ii) Convert a number stored in a string variable x to double data type.
double a = Double.parseDouble(x);
Question 5
Answer
endsWith() tests if the string object ends with the string specified as its argument.
startsWith() tests if the string object starts with the string specified as its argument. Consider
the below example:
Question 1
toUpperCase()
Answer
It converts a string into upper case characters. If any character is already in uppercase or is a
special character then it will remain same.
Syntax:
Question 2
trim()
Answer
Syntax:
Question 3
toLowerCase()
Answer
Syntax:
Question 4
length()
Answer
It returns the length of the string i.e. the number of characters present in the string.
Syntax:
Question 5
replace()
Answer
It replaces a character with another character or a substring with another substring at all its
occurrences in the given string.
Syntax:
Question 6
compareTo()
Answer
It compares two strings lexicographically. It results in the difference of the ASCII codes of
the corresponding characters. Its return type is int.
Syntax:
Question 7
reverse()
Answer
Syntax:
<StringBuffer-Variable>.reverse();
Question 8
indexOf()
Answer
It returns the index of the first occurrence of the specified character within the string or -1 if
the character is not present.
Syntax:
int <variable-name> = <string-variable>.indexOf(<character>);
Question 9
startWith()
Answer
It tests if the string object starts with the string specified as its argument.
Syntax:
Question 10
equalsIgnoreCase()
Answer
It ignores the case of the characters and checks if the contents of two strings are same or not.
Syntax: