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 pool' - 13 question(s) found - Order By Newest | |||||||||||
| |||||||||||
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 | |||||||||||
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 | |||||||||||
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 | |||||||||||
| |||||||||||
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. true, due to String Pool, both will point to a same String object. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   code   coding   tricky questions   interesting questions   string   string pool   .equal   == intermediate   frequent | |||||||||||
| |||||||||||
Ans. S3 and S4 are pointing to different memory location and hence Output 1 will be false. Hash code is generated to be used as hash key in some of the collections in Java and is calculated using string characters and its length. As they both are same string literals, and hence their hashcode is same.Output 2 will be true. | |||||||||||
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   string pool | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
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. 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. 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. 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 | |||||||||||
| |||||||||||
| |||||||||||