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

   next 30
 Q41. How many elements will be there in TreeSet after the last line and Why ?

TreeSet set = new TreeSet();
set .add(new String("abc"));
set .add(new String("abc"));
Core Java
Ans. One.

As we haven't specified the type of TreeSet, it being evaluated with the first element insertion. Once it's identified that it's of type String and as no comparator has been defined, the comparison is done using the String compareTo method. String compareTo method compares the elements by the content / value.

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

   Like         Discuss         Correct / Improve     java   set   treeset   string   compareto   coding   code


 Q42. Strings in switch were introduced in Which Java version ? a. Java 5 b. Java 6 c. Java 7 d. JavaCore Java
Ans. Java 7

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

   Like         Discuss         Correct / Improve     java   switch   string in switch


Rarely asked as it was introduced with Java 8.
 Q43. Which of the following has been introduced with Java 8 ? a. StringBuffer b. StringBuilder c. StringFilter d. StringJoinerCore Java
Ans. StringJoiner

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

   Like         Discuss         Correct / Improve     java   java8   java 8   string   stringjoiner


 Q44. Write a method to convert all elements of a set to lower case.Core Java
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?keyword=Method+to+convert+all+elements+of+a+collection&category=code

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

   Like         Discuss         Correct / Improve     code  coding  string  String.toLowerCase  set  collections


 Q45. Write java code to Get all words from a String and display themCore Java
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?keyword=+Get+all+words+from+a+String&category=code

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

   Like         Discuss         Correct / Improve     string   get all words from strings   string.split


 Q46. How does the String Class stores the string in memory ?Core Java
Ans. It stores the string as a character array with 2 bytes for each character.

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

   Like         Discuss         Correct / Improve     String  String Class


 Q47. Are Strings objects in Java ?Core Java
Ans. Yes, everything except primitive types are objects in Java.

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

   Like         Discuss         Correct / Improve     string


 Q48. How is String class unique ?Core Java
Ans. String class is immutable as well as final. Because of these properties , String objects offer many benefits

1. String Pool - When a string is created and if it exists in the pool, the reference of the existing string will be returned instead of creating a new object. If string is not immutable, changing the string with one reference will lead to the wrong value for the other references.

Example -

String str1 = "String1";
String str2 = "String1"; // It doesnt create a new String and rather reuses the string literal from pool

// Now both str1 and str2 pointing to same string object in pool, changing str1 will change it for str2 too

2. To Cache its Hashcode - If string is not immutable, One can change its hashcode and hence its not fit to be cached.

3. Security - 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.

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

   Like         Discuss         Correct / Improve     immutable  immutability  immutability  String  string class     Asked in 1 Companies


 Q49. Which method of String class is used to convert Boolean to String ?Core Java
Ans. toString() is an overloaded method of String class that is used to convert many data types to String, Boolean being one of them.

toString(Boolean bool)

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

   Like         Discuss         Correct / Improve     String  String class  Boolean     Asked in 1 Companies      Basic


 Q50. Which memory segment holds String Pool in Java ?Core Java
Ans. String Pool resides in runtime constant Pool which is a part of Heap memory segment.

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

   Like         Discuss         Correct / Improve     string pool  memory management      Expert        rare


 Q51. How and Why Strings are interned in Java ?Core Java
Ans. String class has a public method intern() that returns a canonical representation for the string object. String class privately maintains a pool of strings, where String literals are automatically interned.

these are automatically interned so as to have efficient String comparison using == operator instead of equals which is usually slower.

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

   Like         Discuss         Correct / Improve     string  string pool  string intern     Asked in 1 Companies


 Q52. Write a program to check is a string is substring of another ?Core Java
Ans. public class Class{
   public static void main(String[] args){
      String string1 = "Hello I am Jack. I live in United States. I live in california state.";

      String string2 = "I live in";

      if(string1.indexOf(string2) >= -1){
         System.out.println("string2 is sub string of string1");
      } else {
         System.out.println("string2 is not sub string of string1");
      }
   }
}

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

   Like         Discuss         Correct / Improve     string  coding  code


 Q53. Write a Program to check number of occurrences of one string within another ? Core Java
Ans. public class Class{
   public static void main(String[] args){
      String string1 = "Hello I am Jack. I live in United States. I live in california state.";

      String string2 = "I live in";

      int startIndex = 0;
      int endIndex = string1.length()-1;

      int countNoOfOccurences = 0;

      String remainingString = string1;

      while(startIndex < endIndex){
         if(remainingString.indexOf(string2) != -1){
            countNoOfOccurences++;
            startIndex = remainingString.indexOf(string2) + string2.length();
            remainingString = remainingString.substring(startIndex);
         } else {
            break;
         }
      }

      System.out.println(countNoOfOccurences);
   }
}

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

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


 Q54. Write a Program to check if string is a Colidrome ?
(Colidrome is a word that has n alphabets followed by the reverse of the n alphabets, for ex - mallom)
Core Java
Ans.
public class Class {
   public static void main(String[] args) {
      String str = "mallam";
      String firstHalf = str.substring(0, str.length() / 2);
      String secondHalf = str.substring(str.length() / 2);

      if (firstHalf.equals(new StringBuilder(secondHalf).reverse().toString())) {
         System.out.println("It's a Colidrome");
      } else {
         System.out.println("It's not a Colidrome");
      }
   }
}

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

   Like         Discuss         Correct / Improve     Colidrome  String  Code  Coding     Asked in 1 Companies


 Q55. Write a Program to print count of each character in a String.Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
Map<Character, Integer> countMap = new HashMap();
String str = "hello world";
for (char character : str.toCharArray()) {
if (countMap.containsKey(character)) {
countMap.put(character, countMap.get(character) + 1);
} else {
countMap.put(character, 1);
}
}
System.out.println(countMap);
}
}

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

   Like         Discuss         Correct / Improve     String  Code  Coding  Character     Asked in 1 Companies


 Q56. Write a Program to only print characters that have occured more than once in a StringCore Java
Ans. public class BuggyBread{
public static void main (String args[]) {
Set<Character> set = new HashSet();
Set<Character> setWithDuplicateChar = new HashSet();
String str = "hello world";
for(char character: str.toCharArray()){
if(set.contains(character)){
setWithDuplicateChar.add(character);
} else {
set.add(character);
}
}

System.out.println(setWithDuplicateChar);
}
}

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

   Like         Discuss         Correct / Improve     String  Code  Coding


 Q57. Write a Program to find 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);
       }
      
       for(String word:str2Words){
          if(str1WordsSet.contains(word)){
             System.out.println(word);
          }
       }
      
   }
}

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

   Like         Discuss         Correct / Improve     String  coding  code


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


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


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


 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


previous 30   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: