This is the java programming questions and answers section on "Java Strings" with an explanation for various interview, competitive examination and entrance test. Solved examples with detailed answer description, explanation are given and it would be easy to understand.
Java Strings Questions
Online Test Name | Java Strings |
Exam Type | Multiple Choice Questions |
Category | Computer Science Engineering Quiz |
Number of Questions | 26 |
21. Which of the following statement is correct?
- A. reverse[] method reverses all characters.
- B. reverseall[] method reverses all characters.
- C. replace[] method replaces first occurrence of a character in invoking string with another character.
- D. replace[] method replaces last occurrence of a character in invoking string with another character
- View Answer
- Workspace
- Report
- Discuss
Answer & Explanation
Answer: Option A
Explanation:
reverse[] method reverses all characters. It returns the reversed object on which it was called.
Workspace
Report Error
22. Which of these method of class StringBuffer is used to extract a substring from a String object?
- A. substring[]
- B. Substring[]
- C. SubString[]
- D. None of the mentioned
- View Answer
- Workspace
- Report
- Discuss
Workspace
Report Error
23.
What will s2 contain after following lines of code?
StringBuffer s1 = "one;
StringBuffer s2 = s1.append["two]
- A. one
- B. two
- C. onetwo
- D. twoone
- View Answer
- Workspace
- Report
- Discuss
Answer & Explanation
Answer: Option C
Explanation:
Two strings can be concatenated by using append[] method.
Workspace
Report Error
24. Which of these method of class StringBuffer is used to reverse sequence of characters?
- A. reverse[]
- B. reverseall[]
- C. Reverse[]
- D. reverseAll[]
- View Answer
- Workspace
- Report
- Discuss
Answer & Explanation
Answer: Option A
Explanation:
reverse[] method reverses all characters. It returns the reversed object on which it was called.
Workspace
Report Error
25. Which of these method of class StringBuffer is used to get the length of sequence of characters?
- A. length[]
- B. capacity[]
- C. Length[]
- D. Capacity[]
- View Answer
- Workspace
- Report
- Discuss
Answer & Explanation
Answer: Option A
Explanation:
length[]- returns the length of String the StringBuffer would create whereas capacity[] returns total number of characters that can be supported before it is grown.
Workspace
Report Error
Mock Tests & Online Quizzes
A part of String is called substring. In other words, substring is a subset of another String. Java String class provides the built-in substring[] method that extract a substring from the given string by using the index values passed as an argument. In case of substring[] method startIndex is inclusive and endIndex is exclusive.
Suppose the string is "computer", then the substring will be com, compu, ter, etc.
Note: Index starts from 0.
You can get substring from the given String object by one of the two methods:
- public String substring[int startIndex]:
This method returns new String object containing the substring of the given string from specified startIndex [inclusive]. The method throws an IndexOutOfBoundException when the startIndex is larger than the length of String or less than zero. - public String substring[int startIndex, int endIndex]:
This method returns new String object containing the substring of the given string from specified startIndex to endIndex. The method throws an IndexOutOfBoundException when the startIndex is less than zero or startIndex is greater than endIndex or endIndex is greater than length of String.
In case of String:
- startIndex: inclusive
- endIndex: exclusive
Let's understand the startIndex and endIndex by the code given below.
In the above substring, 0 points the first letter and 2 points the second letter i.e., e [because end index is exclusive].
Example of Java substring[] method
TestSubstring.java
Output:
Original String: SachinTendulkar Substring starting from index 6: Tendulkar Substring starting from index 0 to 6: Sachin
The above Java programs, demonstrates variants of the substring[] method of String class. The startindex is inclusive and endindex is exclusive.
Using String.split[] method:
The split[] method of String class can be used to extract a substring from a sentence. It accepts arguments in the form of a regular expression.
TestSubstring2.java
Output:
[Hello, My name is Sachin]
In the above program, we have used the split[] method. It accepts an argument \\. that checks a in the sentence and splits the string into another string. It is stored in an array of String objects sentences.