Strings Inbuilt
Methods
Question :01
// Predict the output
public class TestSubstring
{
public static void main(String args[]){
String s = "Hardik Pandya";
System.out.println(s.substring(7));
System.out.println(s.substring(0,7));
}
}
For above question correct explanation is given
below.
Output:
Pandya
Hardik
public String substring(int startIndex): This method returns new String object containing
the substring of the given string from specified startIndex (inclusive).
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.
Question :02
// Predict the output
public class ReplaceExample1
{
public static void main(String args[]){
String s1 = "Negative thoughts";
String replaceString = s1.replace('e','a');
System.out.println(replaceString);
}
}
For above question correct explanation is given
below.
Output:
Nagativa thoughts
The java string replace() method returns a string replacing all the old char or
CharSequence to new char or CharSequence.
Question :03
// Predict the output
public class ReplaceExample1
{
public static void main(String args[]){
String s1 = "Negative thoughts";
String replaceString =
s1.replace("Negative","Positive");
System.out.println(replaceString);
}
}
For above question correct explanation is given
below.
Output:
Positive thoughts
Question :04
// Predict the output
public class StartsWithExample{
public static void main(String args[]){
String s1 = "You must be the change you wish to see
in the world";
System.out.println(s1.startsWith("Y"));
System.out.println(s1.startsWith("You must"));
System.out.println(s1.startsWith("a"));
System.out.println(s1.startsWith("o", 1));
}
}
For above question correct explanation is given
below.
Output:
true
true
false
true
public boolean startsWith(String prefix)
public boolean startsWith(String prefix, int offset)
Question :05
// Predict the output
public class ContainsExampleMain1 {
public static void main(String args[]){
String name = "what do you know about me";
System.out.println(name.contains("do you
know"));
System.out.println(name.contains("about"));
System.out.println(name.contains("hello"));
}
}
For above question correct explanation is given
below.
Output:
true
true
false
The java string contains() method searches the sequence of characters in this
string. It returns true if sequence of char values are found in this string otherwise
returns false.
Question :01
// Predict the output
public class ContainsExampleMain2 {
public static void main(String[] args) {
String str = "Hello F#A#C#E@2.0 readers";
boolean isContains = str.contains("F#A#C#E");
System.out.println(isContains);
System.out.println(str.contains("FACm"));
}
}
For above question correct explanation is given
below.
Output:
true
false
The java string contains() method searches the sequence of characters in this string. It
returns true if sequence of char values are found in this string otherwise returns false.
Question :02
// Predict the output
public class ContainsExampleMain3 {
public static void main(String[] args) {
String str = "To learn Java visit focusacademy.in";
if(str.contains("focusAcademy.in.com")) {
System.out.println("This string contains
focusacademy.in");
}
else
System.out.println("Result not found");
}
For above question correct explanation is given
below.
Output:
Result not found
Question :03
// Predict the output
public class EndsWithExample{
public static void main(String args[]){
String s1 = "Beauty is in the eye of the beholder";
System.out.println(s1.endsWith("r"));
System.out.println(s1.endsWith("holder"));
System.out.println(s1.endsWith("eye"));
}
}
For above question correct explanation is given
below.
Output:
true
true
false
The java string endsWith() method checks if this string ends with given suffix. It
returns true if this string ends with given suffix else returns false.
Question :04
// Predict the output
public class EqualsIgnoreCaseExample{
public static void main(String args[]){
String s1 = "The walking Dead";
String s2 = "The walking Dead";
String s3 = "THE WALKING DEAD";
String s4 = "The WEST WEEDS";
System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s1.equalsIgnoreCase(s3));
System.out.println(s1.equalsIgnoreCase(s4));
}
}
For above question correct explanation is given
below.
Output:
true
true
false
The String equalsIgnoreCase() method compares the two given strings on the
basis of content of the string irrespective of case of the string. It is like equals()
method but doesn't check case. If any character is not matched, it returns false
otherwise it returns true.
Question :05
// Predict the output
public class IndexOfExample{
public static void main(String args[]){
String s1 = "This is the world";
int index4 = s1.indexOf('s');
System.out.println(index4);
int index1 = s1.indexOf("is");
int index2 = s1.indexOf("world");
System.out.println(index1);
System.out.println(index2);
int index3 = s1.indexOf("is",4);
System.out.println(index3);
}
}
For above question correct explanation is given
below.
Output:
3
2
12
5
The java string indexOf() method returns index of given character value or
substring. If it is not found, it returns -1. The index counter starts from zero.
int indexOf(int ch)returns index position for the given char value
int indexOf(int ch, int fromIndex)returns index position for the given char value
and from index
int indexOf(String substring)returns index position for the given substring
int indexOf(String substring, int fromIndex)returns index position for the given
substring and from index
A
// Predict the output
public class IndexOfExample2 {
public static void main(String[] args) {
String s1 = "This is the example";
int index = s1.indexOf("example", 10);
System.out.println(index);
index = s1.indexOf("example", 20);
System.out.println(index);
}
}
For above question correct explanation is given
below.
Output:
12
-1
Question :01
// Predict the output
public class IndexOfExample3 {
public static void main(String[] args) {
String s1 = "This is indexOf method";
int index = s1.indexOf('O', 12);
System.out.println(index);
}
}
For above question correct explanation is given
below.
Output:
13
Question :02
// Predict the output
public class StringTrimExample{
public static void main(String args[]){
String s1 = " Game of ";
System.out.println(s1 + "thrones");
System.out.println(s1.trim() + "thrones");
}
}
For above question correct explanation is given
below.
Output:
Game of thrones
Game ofthrones
The trim() method
The Java string trim() method is used to eliminate leading and trailing blank spaces.
The Unicode value of 'space' character is 20.
This method checks the Unicode value before and after the string. If it exists, it removes the
spaces and returns the remaining string.
Question :03
// Predict the output
public class StringToCharArrayExample{
public static void main(String args[]){
String s1= "Twilight Saga";
char[] ch = s1.toCharArray();
for(int i = 0; i < ch.length; i++){
System.out.print(ch[i]);
}
}
}
For above question correct explanation is given
below.
Output:
Twilight Saga
The java string toCharArray() method converts this string into character array. It
returns a newly created character array, its length is similar to this string and its
contents are initialized with the characters of this string.
For above question correct explanation is given
below.
Output:
18
Welcome to Jumanji
The java string toCharArray() method converts this string into character array. It
returns a newly created character array, its length is similar to this string and its
contents are initialized with the characters of this string.
For above question correct explanation is given
below.
Output:
CSK
32.334340
32.334340000000 //returns 12 char fractional part filling with 0. The output should
contain 16 charcaters, here it is 15, one space is printed
The java string format() method returns the formatted string by given locale,
format and arguments.
If you don't specify the locale in String.format() method, it uses default locale by
calling Locale.getDefault() method.
The format() method of java language is like sprintf() function in c language
and printf() method of java language
Question :01
// Predict the output
public class InternExample{
public static void main(String args[]){
String s1 = new String("Dravid");
String s2 = "Dravid";
String s3 = s1.intern();
System.out.println(s1 == s2);
System.out.println(s2 == s3);
}
}
For above question correct explanation is given
below.
Output:
false
true
String Interning is a method of storing only one copy of each distinct String Value, which
must be immutable.
By applying String.intern() on a couple of strings will ensure that all strings having the same
contents share the same memory. For example, if a name ‘Amy’ appears 100 times, by
interning you ensure only one ‘Amy’ is actually allocated memory.
This can be very useful to reduce the memory requirements of your program. But be aware
that the cache is maintained by JVM in permanent memory pool which is usually limited in
size compared to heap so you should not use intern if you don’t have too many duplicate
values
For above question correct explanation is given
below.
● intern() method : In Java, when we perform any operation using intern() method, it
returns a canonical representation for the string object. A pool is managed by String
class.
● When the intern() method is executed then it checks whether the String equals to this
String Object is in the pool or not.
● If it is available, then the string from the pool is returned. Otherwise, this String object is
added to the pool and a reference to this String object is returned.
● It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if
s.equals(t) is true.
● It is advised to use equals(), not ==, to compare two strings. This is because ==
operator compares memory locations, while equals() method compares the content
stored in two objects.
Question :02
// Predict the output
public class InternExample2 {
public static void main(String[] args) {
String s1 = "Game starts";
String s2 = s1.intern();
String s3 = new String("Game starts");
String s4 = s3.intern();
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1 == s4);
System.out.println(s2 == s3);
System.out.println(s2 == s4);
System.out.println(s3 == s4);
}
}
For above question correct explanation is given
below.
Output:
true
false
true
false
true
false
Question :03
// Predict the output
public class IsEmptyExample{
public static void main(String args[]){
String s1 = "";
String s2 = "java";
System.out.println(s1.isEmpty());
System.out.println(s2.isEmpty());
}
}
For above question correct explanation is given
below.
Output:
true
false
The java string isEmpty() method checks if this string is empty or not. It
returns true, if length of string is 0 otherwise false. In other words, true is returned
if string is empty otherwise it returns false.
Question :04
// Predict the output
public class IsEmptyExample2 {
public static void main(String[] args) {
String s1 = "";
String s2 = "Wonderla";
if(s1.length()==0 || s1.isEmpty())
System.out.println("s1 is empty");
else System.out.println("s1");
if(s2.length() == 0 || s2.isEmpty())
System.out.println("s2 is empty");
else System.out.println(s2);
}
}
For above question correct explanation is given
below.
Output:
s1 is empty
Wonderla
Question :05
// Predict the output
public class StringJoinExample{
public static void main(String args[]){
String
joinString1=String.join("","welcome","to","jurassic","world
");
System.out.println(joinString1);
}
}
For above question correct explanation is given
below.
Output:
welcome-to-jurassic-world
Question :01
// Predict the output
public class StringValueOfExample{
public static void main(String args[]){
int value = 30;
String s1 = String.valueOf(value);
System.out.println(s1 + 10);
}
}
For above question correct explanation is given
below.
Output:
3010
The java string valueOf() method converts different types of values into string. By the help
of string valueOf() method, you can convert int to string, long to string, boolean to string,
character to string, float to string, double to string, object to string and char array to string.
Question :02
// Predict the output
public class StringValueOfExample{
public static void main(String[] args) {
float f = 10.05645f;
double d = 10.02;
String s1 = String.valueOf(f);
String s2 = String.valueOf(d);
System.out.println(s1);
System.out.println(s2);
}
}
For above question correct explanation is given
below.
Output:
10.05645
10.02
The java string valueOf() method converts different types of values into string. By the help
of string valueOf() method, you can convert int to string, long to string, boolean to string,
character to string, float to string, double to string, object to string and char array to string.
Question :03
// Predict the output
public class LastIndexOfExample{
public static void main(String args[]){
String s1 = "this is the world";
int index1 = s1.lastIndexOf('s');
System.out.println(index1);
}
}
For above question correct explanation is given
below.
Output:
6
The java string lastIndexOf() method returns last index of the given character
value or substring. If it is not found, it returns -1. The index counter starts from
zero.
1) int lastIndexOf(int ch)returns last index position for the given char value
2) int lastIndexOf(int ch, int fromIndex)returns last index position for the given char
value and from index
3) int lastIndexOf(String substring)returns last index position for the given substring
4) int lastIndexOf(String substring, int fromIndex)returns last index position for the
given substring and from index
Question :04
// Predict the output
public class LastIndexOfExample{
public static void main(String args[]){
String s1 = "this is the world";
int index1 = s1.lastIndexOf('s', 5);
System.out.println(index1);
}
}
For above question correct explanation is given
below.
Output:
3
The java string lastIndexOf() method returns last index of the given character
value or substring. If it is not found, it returns -1. The index counter starts from
zero.
1) int lastIndexOf(int ch)returns last index position for the given char value
2) int lastIndexOf(int ch, int fromIndex)returns last index position for the
given char value and from index
3) int lastIndexOf(String substring)returns last index position for the given substring
4) int lastIndexOf(String substring, int fromIndex)returns last index position for the
given substring and from index
Question :05
// Predict the output
public class LastIndexOfExample{
public static void main(String[] args) {
String str = "This is last index of example";
int index = str.lastIndexOf("last");
System.out.println(index);
index = str.lastIndexOf("of", 25);
System.out.println(index);
index = str.lastIndexOf("of", 10);
System.out.println(index);
}
}
For above question correct explanation is given
below.
Output:
8
19
-1
The java string lastIndexOf() method returns last index of the given character
value or substring. If it is not found, it returns -1. The index counter starts from
zero.
1) int lastIndexOf(int ch)returns last index position for the given char value
2) int lastIndexOf(int ch, int fromIndex)returns last index position for the given char
value and from index
3) int lastIndexOf(String substring)returns last index position for the given
substring
4) int lastIndexOf(String substring, int fromIndex)returns last index position
for the given substring and from index
StringBuilder Inbuilt
Methods
Question :01
// Predict the output
public class StringBuilderExample{
public static void main(String args[]){
StringBuilder sb = new StringBuilder("Rhit man");
sb.insert(1,"Sharma");
System.out.println(sb);
}
}
For above question correct explanation is given
below.
Output:
RSharmahit man
The StringBuilder insert() method inserts the given string with this string at the given
position.
Question :02
// Predict the output
public class StringBuilderExample{
public static void main(String args[]){
StringBuilder sb = new StringBuilder("Ravindra
Jadeja");
sb.delete(4,8);
System.out.println(sb);
}
}
For above question correct explanation is given
below.
Output:
Ravi Jadeja
The delete() method of StringBuilder class deletes the string from the specified beginIndex to
endIndex.
Question :03
// Predict the output
public class StringBuilderExample{
public static void main(String args[]){
StringBuilder sb = new StringBuilder("Ravindraa
Jadeja");
sb.deleteCharAt(7);
System.out.println(sb);
}
}
For above question correct explanation is given
below.
Output:
Ravindra Jadeja
This method removes the char at the specified position in this sequence.
Question :04
// Predict the output
public class StringBuilderExample{
public static void main(String args[]){
StringBuilder sb = new StringBuilder("Mark");
sb.replace(1, 3, "Zukerberg");
System.out.println(sb);
}
}
For above question correct explanation is given
below.
Output:
MZukerbergk
The StringBuilder replace() method replaces the given string from the specified
beginIndex and endIndex.
Question :05
// Predict the output
public class Main{
public static void main(String[] args) {
StringBuffer s = new
StringBuffer("Programmer");
s.insert(5, "xyz");
System.out.println(s);
s.insert(0, 5);
System.out.println(s);
s.insert(3, true);
System.out.println(s);
s.insert(5, 41.35d);
System.out.println(s);
s.insert(8, 41.35f);
System.out.println(s);
char arr[] = {'h','e','l','l','o'};
s.insert(2, arr);
System.out.println(s);
}
}
For above question correct explanation is given
below.
Output:
Progrxyzammer
5Progrxyzammer
5Prtrueogrxyzammer
5Prtr41.35ueogrxyzammer
5Prtr41.41.3535ueogrxyzammer
5Phellortr41.41.3535ueogrxyzammer
THANK YOU