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. Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string   hashcode   hash code   string comparison  hashtable Asked in 17 Companies basic   frequent | ||||
| ||||
Ans. equal 1 | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string   string pool   code   coding basic   frequent | ||||
| ||||
Ans. BuggyBread | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference | ||||
| ||||
Ans. Buggy | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference | ||||
| ||||
Ans. Buggy | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference | ||||
| ||||
Ans. split() and macthes() | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   io   string class   string   pattern | ||||
| ||||
Ans. 1 111 2 TreeSet maintains the elements in the ascending order which is identified by the compareTo method. compareTo method in String has been defined such that it results in the natural alphabetic Order. Here the elements in the TreeSet are of String and not of Integer. In String Natural Order, 111 comes before 2 as ascii of 1st character first determines the order. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   code   coding   set   treeset   string   integer | ||||
| ||||
Ans. String str = new StringBuilder().append("Buggy").append("Bread").toString(); | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   builder pattern   design pattern   stringbuilder   string Asked in 3 Companies | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. No, Alternatively I have used String.split("\\s") for achieving the same function. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string   stringtokenizer | ||||
| ||||
Ans. http://www.buggybread.com/2015/01/java-string-classes-and-interfaces.html | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string | ||||
| ||||
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 | ||||