Core Java - Interview Questions and Answers for 'String' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers - Order By Newest

   
 Q61. Write a Program to validate if a particular character occurs after another in a stringCore Java
Ans. public class Class{
   public static void main(String[] args){
      String str = "xyz123.co@m";
      
      if(str.indexOf('.') < str.indexOf('@')){
         System.out.println("Not a Valid Email Address");
      }
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  string.indexOf  coding  code


 Q62. Write a method / program that will determine if the parenthesis are balanced in a given string.Core Java
Ans. https://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  code  coding     Asked in 14 Companies


 Q63. Can StringBuffer and StringBuilder in Java be inherited?Core Java
Ans. No, they have been declared final and hence cannot be extended.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     StringBuffer  StringBuilder


 Q64. What will be the output of following code

Integer x = 1;
Integer y = 2;
System.out.println(x == y);

What if you change 1 to "1" and Integer to String?
Core Java
Ans. It will print "true" with integers as well as strings. The reason is "Integer constant pool" and "String pool"

String pool maintains pool of string literals. When a string literal is used for the first time, a new string object is created and is added to the pool. Upon it's subsequent usage , the reference for the same object is returned. Similarly java uses integer constant pool.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string pool  object equality  ==


 Q65. What will be result of following code and why

Integer int1 = 1;
Integer int2 = 1;
String str1 = new String("str");
String str2 = new String("str");
String str3 = "str";
String str4 = "str";

      
System.out.println(int1 == int2);
System.out.println(str1 == str2);
System.out.println(str3 == str4);
Core Java
Ans. true
false
true

Just like Strings, java maintains an integer constant pool too. So 1 will be maintained in integer constant pool and hence reference int2 will point to same integer in pool.

String pool is only for string literals ( defined by "" ) and not for newly created objects and hence str1 == str2 will return false as they are separate objects in memory.

String pool is used for storing string literals so that they can be reused and str3 and str4 will point to same string literal in string pool.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string pool  integer constant pool  string comparison  integer comparison


 Q66. What is the purpose of String Pool ? Don't you think it's a performance overhead to have unnecessary check for string in string pool ?Core Java
Ans. String Pool makes Java more memory efficient by providing a reusable place for string literals. It might be a little performance inconvenience but results in good amount memory saving.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string pool  memory management      Intermediate


 Q67. Is String a Data type in Java ?Core Java
Ans. String is not a primitive type in java. Anything defined within double quotes like "abc" is an object of String class.

  Sample Code for String

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  data type      Basic


 Q68. Are char,String and Char data types in java ? Core Java
Ans. char is a primitive data type. String is a class. Char is a wrapper class for primitive char.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     char  string  data type      Basic


 Q69. Why do we pass an array of strings to main method ?Core Java
Ans. Array of strings in the main method are the list of arguments or parameters which are sent to the application / program.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method   main method string array argument


 Q70. What is the difference between being final and being immutable ? Are string final or immutable in Java ?Core Java
Ans. Being final in the sense of objects or object reference means that the reference cannot be reassigned to a different object whereas being immutable means that the object contents cannot be changed.

Strings in Java are immutable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  immutable  immutability  final vs immutable  immutability


 Q71. Is a string an array of characters in Java ? Core Java
Ans. No, String is a class whose objects are called strings in Java.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     String


 Q72. Do we have a method to reverse a string in String class ?Core Java
Ans. No but we have it in StringBuilder.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     String  StringBuilder  Reverse a string


 Q73. How can we convert char into a String ?Core Java
Ans. We can use StringBuilder. StringBuilder accepts char as the argument for it's constructor.

new StringBuilder('').toString();

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     convert char to String  char  String


 Q74. How can we convert String into Char array and vice versa ?Core Java
Ans. There is a method toCharArray() within String class that can be used to convert string to char array.

string.toCharArray();

String class has an argument constructor that takes a char array and create a string

new String(charArray);

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  char array


 Q75. How can we convert a character or a character array into a String ?Core Java
Ans. String has an argument constructor that take char array as argument and creates a string.

There is no constructor available with String that takes in a character and creates a String. We can use StringBuilder which has a char argument constructor.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     character  char  char array  String  char to String  char array to String


 Q76. Why shouldn't we use string concatenation extensively or in a loop ?Core Java
Ans. Because String being an immutable object creates a new object upon each concatenation cycle. If there is any such need , we should use String Builder whose objects are mutable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  string immutable  immutability  for loop  control statements  loop statement     Asked in 1 Companies


 Q77. Suppose we have a string "Java is object oriented language" and we have to reverse every alternate word in string. How would we do that using Java program.Core Java
 This question was recently asked at 'datalake solutions'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string manipulation  code  coding     Asked in 1 Companies


 Q78. Given a String with letters , numbers and special characters , extract Tokens from it. The rules for Token extraction are ac follows

1. Should for token of all characters till a number or special character is found
2. Should form token of all numbers till a character or special character is found
3. Special character in itself is a token
4. Ignore white spaces

For example - Bob Said "He is here"

should result in

Bob A
Said A
" S
He A
is A
here A
" S

where a is specifying alphabet and S as special character
Design
 This question was recently asked at 'Bloomberg'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     code  coding  string     Asked in 1 Companies


 Q79. How to calculate string length without using inbuilt functionCore Java
 This question was recently asked at 'Softenger'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     code  coding  design  string length     Asked in 1 Companies      basic


 Q80. Why string pool concept has been introduced in string ?Core Java
Ans. Memory Sharing and Optimal memory utilization.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     String Pool     Asked in 1 Companies


 Q81. What is the use of final String when Strings are immutable ?Core Java
 This question was recently asked at 'Cyient'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string     Asked in 1 Companies


 Q82. If you have to pull a sub string out of a string, which method would you use - using String methods or Pattern / Matcher ?Design
Ans. It depends on how complex it is and if in future it would need any sort opf debugging. It's not easy to debug code if it's making heavy use of shortcuts like Lambda , Patterns etc.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     pattern matching  String  design


 Q83. Why is String a class in Java ?Core Java
Ans. 1. Creating a class helps in specifying operations on the Strings like length , sub string , concatenation etc.

2. It acts as a Wrapper class for "Array of Characters" and hence facilitates it's usage in collections, assignment to null.

3. Immutability of String objects facilitates in reuse , security and caching.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string class


 Q84. What will be the output of following code ?

String str1 = "String1";
String str2 = "String1";
System.out.print(str1 == str2);
System.out.print(str1.equals(str2));
str1 = str2;
System.out.print(str1 == str2);
Core Java
a. falsetruetrue
b. falsefalsetrue
c. truetruetrue
d. falsefalsefalse

Ans.c. truetruetrue

 Q85. Which of the following class creates immutable objects ?Core Java
a. String
b. StringBuffer
c. StringBuilder
d. None of these create immutable objects.

Ans.a. String

 Q86. Which of the following class is synchronized ?Core Java
a. String
b. StringBuffer
c. StringBuilder
d. None of these

Ans.b. StringBuffer

 Q87. Which memory segment holds String Pool ?Core Java
a. Stack
b. Heap
c. Code Segment
d. Class Segment

Ans.b. Heap

 Q88. Which of the following class cannot be extended or inherited ?

StringBuffer , StringBuilder
Core Java
a. StringBuffer
b. StringBuilder
c. Both cannot be extended
d. Both can be extended

Ans.c. Both cannot be extended

previous 30   

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: