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. |
|
| ||||
Interview Questions and Answers - Order By Newest | ||||
| ||||
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 | ||||
| ||||
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. | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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  == | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||