String Handling
String Handling
Question 1
Answer
True
Reason — The string objects of Java are immutable i.e., once created, they cannot be
changed. If any change occurs in a string object, then original string remains unchanged and a
new string is created with the changed string.
Question 2
The length of a String object s1 can be obtained using the expression s1 length. (T/F)
Answer
False
Reason — The length of a String object s1 can be obtained using the expression s1.length()
as length() method returns the length of the String object.
Question 3
1. length()
2. compareTo()
3. equals()
4. substring()
5. All of these
6. None of them
Answer
All of these
Question 4
String s1 = "yes" ;
String s2 = "yes" ;
String s3 = new String(s1) ;
Which of the following would equate to False ?
1. s1 == s2
2. s3 == s1
3. s1.equals(s2)
4. s3.equals(s1)
Answer
s3 == s1
1. The first comparison uses the == operator to compare s1 and s2. == operator checks
for reference equality, i.e., whether both variables refer to the same object in memory.
Since s1 and s2 have the same value and were created using the same string literal,
they will refer to the same object in memory. Therefore, this comparison returns true.
2. The second comparison uses the == operator to compare s3 and s1. Since s3 was
created using the new keyword and is therefore a different object in memory than s1,
this comparison returns false.
3. The third comparison uses the equals method to compare s1 and s2. This method
checks for value equality, i.e., whether both objects have the same value regardless of
their memory location. Since s1 and s2 have the same value, this comparison
returns true.
4. The fourth comparison uses the equals method to compare s3 and s1. This method
also checks for value equality, so it returns true because s3 and s1 have the same
value, even though they are different objects in memory.
Question 5
Suppose that s1 and s2 are two strings. Which of the statements or expressions are incorrect ?
1. String s3 = s1 + s2;
2. String s3 = s1 - s2;
3. s1.compareTo(s2);
4. int m = s1.length( );
Answer
String = s1 - s2;
Reason — - operator cannot be used with String objects, hence the statement String s3 =
s1 - s2; is incorrect.
Question 6
1. s.trim( )
2. s.replace('a', 'A')
3. s.substring(3)
4. s.toUpperCase( )
5. s.setCharAt(1, 'A')
Answer
s.setCharAt(1, 'A')
Question 7
1. java.util
2. java.lang
3. ArrayList
4. None of the mentioned
Answer
java.lang
Question 8
Which of these methods of class String is used to obtain length of String object?
1. get( )
2. Sizeof( )
3. lengthof( )
4. length( )
Answer
length( )
Question 9
Which of these methods of class String is used to extract a single character from a String
object?
1. CHARAT( )
2. chatat( )
3. charAt( )
4. ChatAt( )
Answer
charAt( )
Reason — charAt( ) returns the character at the given index in a String object.
Question 10
1. String( )
2. String(void)
3. String(0)
4. None of the mentioned
Answer
String( )
Reason — String( ) constructor is used to create an empty String object as it does not specify
any String value.
Question 11
Answer
Reason — String are classes in Java. Array of characters is one of the data members of String
class.
Assignment Questions
Question 1
Answer
In Java, we can work with characters by using String and StringBuffer class.
String class — The instances of String class can hold unchanging string, which once
initialized cannot be modified. For example,
Answer
The string objects of Java are immutable i.e., once created, they cannot be changed. If any
change occurs in a string object, then original string remains unchanged and a new string is
created with the changed string.
On the other hand, StringBuffer objects are mutable. Thus, these objects can be manipulated
and modified as desired.
Question 3
Answer
We can convert a numeric value enclosed in a string format into a numeric value by using
valueOf() method.
For example, the given statement will return the numeric value of the String argument "15".
int num = Integer.valueOf("15");
Question 4
1. isLetterOrDigit(char)
2. replace(char, char)
Answer
1. boolean
2. String
Question 5
State the data type and values of a and b after the following segment is executed.
Data type of a is int and value is 2. Data type of b is boolean and value is false.
Explanation
compareTo() method compares two strings lexicographically and returns the difference
between the ASCII values of the first differing characters in the strings. Thus, a will be
of int data type and store the value 2 as the difference between ASCII values of 'A' and 'C' is
2.
equals() method compares two String objects and returns true if the strings are same, else it
returns false. Thus, b will be of boolean data type and result in false as both the strings are
not equal.
Question 6
String x = "hello";
String y = "world";
(i) System.out.println(x + y);
(iii) System.out.println(x.charAt(3));
(iv) System.out.println(x.equals(y));
Answer
Output
helloworld
Explanation
Output
Explanation
length() returns the length of the String object. Since x has 5 characters, the length of the
string is 5.
(iii) System.out.println(x.charAt(3));
Output
Explanation
chatAt() returns the character at the given index. Index starts from 0 and continues till length -
1. Thus, the character at index 3 is l.
(iv) System.out.println(x.equals(y));
Output
false
Explanation
equals() method checks for value equality, i.e., whether both objects have the same value
regardless of their memory location. As values of x and y are not the same, so the method
returns false.
Question 7
String s = "malayalam" ;
System.out.println(s.indexOf('m'));
System.out.println(s.lastIndexOf('m'));
Answer
Output
0
8
Explanation
indexOf() returns the index of the first occurrence of the specified character within the current
String object. The first occurrence of 'm' is at index 0, thus 0 is printed first.
lastIndexOf() returns the index of the last occurrence of the specified character within the
String object. The last occurrence of 'm' is at index 8, thus 8 is printed next.
Question 8
If:
String x = "Computer";
String y = "Applications";
What do the following functions return?
(i) System.out.println(x.substring(1,5));
(ii) System.out.println(x.indexOf(x.charAt(4)));
(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
charAt() method returns the character at a given index in a String. Thus, x.charAt(4) will
return the character at the 4th index i.e., u. indexOf() method returns the index of the first
occurrence of a given character in a string. Thus, it returns the index of u, which is 4.
(iii) System.out.println(y + x.substring(5));
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
equals() method checks for value equality, i.e., whether both objects have the same value
regardless of their memory location. As the values of both x and y are different so it
returns false.
Question 9
Answer
(i) Float.parseFloat()
(ii) Character.isUpperCase()
Question 10
What will be the output of the following code snippet when combined with suitable declarations
and run?
Output
Central Madras
Explanation
Statement Remark
Question 11
Output
ABC
Explanation
String s stores "abc".
toUpperCase( ) method converts all of the characters in the String to upper case. Thus, "abc"
is converted to "ABC" and printed on the output screen.
Question 12
class MainString
{
public static void main(String[ ] args)
{
StringBuffer s = new StringBuffer("String");
if((s.length( ) > 5) &&
(s.append("Buffer").equals("X")))
; // empty statement
System.out.println(s);
}
}
Answer
Output
StringBuffer
Explanation
The expression s.length( ) > 5 will result in true because the length of String s is 6 which
is > 5.
In the expression s.append("Buffer".equals("X")), first the value of String s will be
modified from String to StringBuffer and then it will be compared to "X". The result will
be false as both the values are not equal.
Now, the condition of if statement can be solved as follows:
Question 13
(c) To print out "Good" and then stop, if the answer is correct.
import java.util.Scanner;
System.out.println(q);
if(i == 4)
{
System.out.println("Correct Answer: " + a);
}
}
}
Output
Question 14
Write a program to extract a portion of a character string and print the extracted string. Assume
that m characters are extracted, starting with the nth character.
import java.util.Scanner;
int substrlen = n + m;
if( m <= 0 || substrlen > len)
{
System.out.println("Invalid no. of characters");
System.exit(1);
}
Output
Question 15
Write a program, which will get text string and count all occurrences of a particular word.
import java.util.Scanner;
if (word.equalsIgnoreCase(ipWord))
count++ ;
word = "";
}
else {
word += str.charAt(i);
}
}
if (count > 0) {
System.out.println(ipWord + " is present " +
count + " times.");
}
else {
System.out.println(ipWord + " is not present in
sentence.");
}
}
}
Output
Question 16
Write a program to accept a string. Convert the string to uppercase. Count and output the
number of double letter sequences that exist in the string.
Sample Input : "SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE"
Sample Output : 4
import java.util.Scanner;
}
}
Output
Question 17
(i) void check(String str, char ch) - to find and print the frequency of a character in a string.
Example:
Input :
str = "success" ch= 's'
Output :
number of s present is = 3
(ii) void check (String s1) - to display only vowels from string s1, after converting it to lower
case.
Example:
Input:
sl = "computer"
Output : o u e
Output