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

   



Core Java - Interview Questions and Answers for 'String' - 83 question(s) found - Order By Rating

next 30
 Q1. 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


 Q2. 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


 Q3. 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


 Q4. 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


 Q5. 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


 Q6. 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


 Q7. Find different combination of given String and trace the output

Core Java
Ans. private void permutation(String prefix, String sufix)
{
int ln = sufix.length();
if(ln == 0) {
System.out.println(prefix);
} else {
IntStream.range(0, ln).forEach(i->permutation(prefix sufix.charAt(i), sufix.substring(0,i) sufix.substring(i 1, ln)));
}
}

call:permutation("", "abcdef");

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

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


 Q8. 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


 Q9. 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


 Q10. How is string object immutable if we can concat a string to it ?Core Java
Ans. Because it doesn't make the change in the existing string but would create a new string by concatenating the new string to previous string. So Original string won't get changed but a new string will be created. That is why when we say

str1.concat("Hello");

It means nothing because we haven't specified the reference to the new string and we have no way to access the new concatenated string. Accessing str1 with the above code will still give the original string.

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

   Like         Discuss         Correct / Improve     string  string immutable  immutability  string concat


 Q11. 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


 Q12. 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


 Q13. 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


 Q14. 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


 Q15. 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


 Q16. Is String final in Java ?Core Java
Ans. Strings are immutable in Java and not final.

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

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


 Q17. 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


 Q18. 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


 Q19. How does making string as immutable helps with securing information ? How does String Pool pose a security threat ?Core Java
Ans. String is widely used as parameter for many java classes, e.g. network connection, opening files, etc. Making it mutable might possess threats due to interception by the other code segment or hacker over internet.

Once a String constant is created in Java , it stays in string constant pool until garbage collected and hence stays there much longer than what's needed. Any unauthorized access to string Pool pose a threat of exposing these values.


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

   Like         Discuss         Correct / Improve     Security  String pool   string immutable  immutability      expert        rare


 Q20. 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


 Q21. 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


 Q22. 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


 Q23. 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


 Q24. 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  ==


 Q25. 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


 Q26. 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


 Q27. 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


 Q28. Write a Program to display the words of a String in alpahbetical or Dictionary OrderCore Java
Ans. public class BuggyBread {
   public static void main(String args[]) {
       String str = "we are what we repeatedly do excellence then is not an act but a haBit";
       Set<String> wordSet = new TreeSet(); // Using Linked Hash Set as we would like to retrieve words in the insertion order
      
       for(String word: str.split(" ")){
          wordSet.add(word);
       }
      
       for(String word: wordSet){
          System.out.print(word);
          System.out.print(" ");
       }
   }
}

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

   Like         Discuss         Correct / Improve     String  coding  code


 Q29. Write a Program to remove duplicate words from the StringCore Java
Ans.
public class BuggyBread {
public static void main(String args[]) {
String str = "we are what we repeatedly Do excellence, then, is not an act but a haBit";
Set<String> wordSet = new LinkedHashSet(); // Using Linked Hash Set as we would like to retrieve words in the insertion order

for(String word: str.split(" ")){
wordSet.add(word);
}

for(String word: wordSet){
System.out.print(word);
System.out.print(" ");
}
}
}

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

   Like         Discuss         Correct / Improve     String  coding  code     Asked in 1 Companies      basic


 Q30. Write a program to print count / no of common words in 2 separate stringsCore Java
Ans. public class BuggyBread {
   public static void main(String args[]) {
       String str1 = "we are what we repeatedly Do excellence, then, is not an act but a haBit";
       String str2 = "we are what we repeatedly Do is";
       String[] str1Words = str1.split(" ");
       String[] str2Words = str2.split(" ");
      
       Set str1WordsSet = new HashSet();
      
       for(String word:str1Words){
          str1WordsSet.add(word);
       }
      
       int commonWordsCount = 0;
      
       for(String word:str2Words){
          if(str1WordsSet.contains(word)){
             commonWordsCount++;
          }
       }
      
       System.out.println(commonWordsCount);
   }
}

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

   Like         Discuss         Correct / Improve     String  coding  code


next 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: