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 cla' - 22 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. 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 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. 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. FALSE. == operator compares object references, a and b are references to two different objects, hence the FALSE. .equals method is used to compare string object content. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  string   string class   java   ==   object references   coding Asked in 2 Companies basic   frequent | ||||
Try 1 Question(s) Test | ||||
Frequently asked in Infosys India | ||||
| ||||
Ans. String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   string   string class   string pool   heap memory Asked in 31 Companies intermediate   frequent | ||||
Try 2 Question(s) Test | ||||
Advanced level question. Frequently asked in High end product companies. Frequently asked in Google , Cognizant and Deloitte ( Based on 2 feedback ) | ||||
| ||||
Ans. 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 doesn't 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 it's 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  java   oops   string   string class   immutable  immutability   advanced Asked in 39 Companies expert   frequent | ||||
Try 4 Question(s) Test | ||||
Very Frequently asked. Usually asked along with String Class related questions. | ||||
| ||||
Ans. Class using which only immutable (objects that cannot be changed after initialization) objects can be created. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   immutable  immutability   immutable  immutability class   string class   basic interview question Asked in 18 Companies Basic   frequent | ||||
Try 2 Question(s) Test | ||||
Very frequently asked. Usually difference between String,StringBuffer and StringBuilder is asked in different variations. | ||||
| ||||
Ans. StringBuffer is synchronized whereas StringBuilder is not. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string   stringbuffer   string class   stringbuilder   synchronized   basic interview question   infosys technologies Asked in 17 Companies basic   frequent | ||||
Try 1 Question(s) Test | ||||
Very frequently asked in different variations. Frequently asked in Deloitte ( 2 feedback ) , HCL Tech ( 3 feedback ), TCS and Coginizant (CTS) | ||||
| ||||
Ans. If the Object value will not change, use String Class because a String object is immutable. If the Object value can change and will only be modified from a single thread, use StringBuilder because StringBuilder is unsynchronized(means faster). If the Object value may change, and can be modified by multiple threads, use a StringBuffer because StringBuffer is thread safe(synchronized). | ||||
Sample Code for String Sample Code for StringBuffer Sample Code for StringBuilder | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string   stringbuilder   stringbuffer   String vs StringBuffer   String vs StringBuilder   String vs StringBuilder vs StringBuffer   StringBuffer vs stringBuilder Asked in 29 Companies basic   frequent | ||||
Try 3 Question(s) Test | ||||
| ||||
Ans. There are total six ways 1. literals When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool. 2. new keyword When we use new operator, JVM creates the String object but dont store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool. 3. string buffer 4. string builder 5. System.out.println 6. char to string | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string   jvm   memory management   string pool Asked in 5 Companies basic   frequent | ||||
Try 3 Question(s) Test | ||||
| ||||
Ans. private static boolean isPalindrome(String str) { if (str == null) return false; StringBuilder strBuilder = new StringBuilder(str); strBuilder.reverse(); return strBuilder.toString().equals(str); } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string   stringbuilder   stringbuilder   string class   code   palindrome Asked in 38 Companies Basic   frequent | ||||
| ||||
Ans. private static String removeChar(String str, char c) { | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string   string class   code   write code | ||||
| ||||
Ans. toUpperCase and toLowerCase | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string | ||||
| ||||
Ans. We can use String getBytes() method to convert String to byte array and we can use String constructor new String(byte[] arr) to convert byte array to String. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string | ||||
Asked multiple times in Capgemini. | ||||
| ||||
Ans. String is immutable in java and stored in String pool. Once it's created it stays in the pool until unless garbage collected, so even though we are done with password it's available in memory for longer duration and there is no way to avoid it. It's a security risk because anyone having access to memory dump can find the password as clear text. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string   immutable  immutability   string pool   garbage collection   advanced Asked in 6 Companies Expert | ||||
| ||||
Ans. Since String is immutable, its hashcode is cached at the time of creation and it doesnt need to be calculated again. This makes it a great candidate for key in a Map and its processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string   immutable  immutability   hashmap   immutable  immutability   hashcode   hash code   advanced Asked in 2 Companies expert   frequent | ||||
| ||||
Ans. intern() method keeps the string in an internal cache that is usually not garbage collected. Moreover provide reference for scp object for corresponding string object present in heap memory. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string   intern method   garbage collection   advanced Asked in 2 Companies expert | ||||
| ||||
Ans. substring method would build a new String object keeping a reference to the whole char array, to avoid copying it. Hence you can inadvertently keep a reference to a very big character array with just a one character string. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string   substring   memory leaks   jvm   memory management   advanced   architecture   technical architect   technical lead expert | ||||
Very frequently asked in HCL Tech ( Based of 4 inputs ) | ||||
| ||||
Ans. Using String method - new StringBuffer(str).reverse().toString(); Iterative - public static String getReverseString(String str){ StringBuffer strBuffer = new StringBuffer(str.length); for(int counter=str.length -1 ; counter>=0;counter--){ strBuffer.append(str.charAt(counter)); } return strBuffer; } Recursive - public static String getReverseString(String str){ if(str.length <= 1){ return str; } return (getReverseString(str.subString(1)) + str.charAt(0); } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string   reverse   stringbuffer   string class   code Asked in 6 Companies   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. 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 | ||||