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. |
|
| ||||
Core Java - Interview Questions and Answers for 'String' - 83 question(s) found - Order By Rating | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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. 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 | ||||
| ||||
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. 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. 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. 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. 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. 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. 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. 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. 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 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 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 | ||||