Java - Interview Questions and Answers for 'String class' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2098 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'String class' - 20 question(s) found - Order By Newest

Advanced level question. Frequently asked in High end product companies. Frequently asked in Cognizant ( Based on 2 feedbacks )
  Q1. Why is String immutable in Java ?Core Java
Anonymous
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   advanced     Barclays  Jungo  Cloudera  Allston  Man Group  Prokarma  Optimus  Gamesys  ABS  TransPerfect  Fidelity  Deloitte  Zalando  Cognizant (CTS)  Zeta Interactive  SuccessFactors   EchoStar  InterGlobe Technologies  HCL Technologies  Intuit  JP Morgan  Optimus Information  Faichi Solutions  Blip  Adobe  Subex Limited  Remo Software  Allstate  IBM  Bizmatics  Bytecode Cyber Security  Calsoft Systems  IDS Infotech      expert        frequent

Try 4 Question(s) Test


Very frequently asked in different variations. Frequently asked in Deloitte ( 2 feedback ) , HCL Tech ( 3 feedback ) and Coginizant (CTS)
  Q2. Explain the scenerios to choose between String , StringBuilder and StringBuffer ?

or

What is the difference between String , StringBuilder and StringBuffer ?
Core Java
Anonymous
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     Cognizant (CTS)   San Jose State University (SJSU)  HCL Technologies   Deloitte  Sophos  Hitachi  JPMorgan Chase  IBM  Kony Labs  Keystone  ebay      basic        frequent

Try 3 Question(s) Test


 Q3. What are different ways to create String Object? Explain.Core Java
Admin
info@buggybread.com
Ans.
String str = new String("abc");
String str1 = "abc";


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.

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.

 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     GuideWire  NTT Data  Fractal Analytics      basic        frequent

Try 3 Question(s) Test


 Q4. What will this code print ?

String a = new String ("TEST");
String b = new String ("TEST");
if(a == b) {
System.out.println ("TRUE");
} else {
System.out.println ("FALSE");
}
Core Java
Anonymous
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      basic        frequent

Try 1 Question(s) Test


 Q5. Why Char array is preferred over String for storing password?Core Java
Admin
info@buggybread.com
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   string pool   garbage collection   advanced     General Motors  Amura Marketing Technologies  Bloomberg  Innovive      Expert


  Q6. What is a String Pool ?Core Java
Anonymous
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     SAS Institute  Ariba  GuideWire  Harbinger  Oracle  Virtusa  Knoa  Polaris  GuideWire  HCL Technologies  Tata Consultancy (TCS)  Infosys  Technologent  MphasiS  Harbinger Group  Kony  Evergent  NTT Data  VMware  Markit  Ariba  Tech Mahindra  Bureau Veritas  InfoStretch  PTC Software  Mindtree  Centre for Internet and Society (CIS)  Trimax IT Infrastructure & Services      intermediate        frequent

Try 2 Question(s) Test


Very Frequently asked. Usually asked along with String Class related questions.
 Q7. What is an immutable class ?Core Java
Anonymous
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   immutable class   string class   basic interview question   ebay     Cognizant (CTS)  Hybris  YuMe  ebizAutos      Basic        frequent

Try 2 Question(s) Test


Very frequently asked. Usually difference between String,StringBuffer and StringBuilder is asked in different variations.
  Q8. Difference between StringBuffer and StringBuilder ?Core Java
Anonymous
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     Thomson Reuters  Ludia  QualiTest  United Health Group (UHG)  Sophos  JPMorgan Chase  ebay  Infosys Technologies  Experian India      basic        frequent

Try 1 Question(s) Test


  Q9. Write a method to check if input String is Palindrome?Core Java
Admin
info@buggybread.com
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     HCL Technologies  Hootsuite  ebay  Mobile Apps Company  TripAdvisor  Wallet Hub  Groupon  OpenText  Airbnb  Vantiv  Minted LLC  Boston Analytics  mint.com  Arista Networks  Dealer.com  the Zappos Family  Sony PlayStation  Taser  Wolters Kluwer  Wize Commerce  Hitachi Consulting  Quickr  SmartBrief  Liaison International  Monitise  Exilant Technologies  Enova  Wayfair  Amadeus  MakeMyTrip  Naggaro  Vudu  OnDeck Capital  HighRadius   thePlatform  Trimax IT Infrastructure & Services      Basic        frequent



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. Write a method that will remove given character from the String?Core Java
Admin
info@buggybread.com
Ans.
private static String removeChar(String str, char c) {    
if (str == null)
return null;

return str.replaceAll(Character.toString(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


 Q11. What does String intern() method do?Core Java
Admin
info@buggybread.com
Ans. intern() method keeps the string in an internal cache that is usually not garbage collected.

 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     Polaris  Netflix      expert


 Q12. How substring() method of String class create memory leaks?Core Java
Admin
info@buggybread.com
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 )
 Q13. Write a program to reverse a string iteratively and recursively ?Core Java
Admin
info@buggybread.com
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     HCL Technologies  Exelis  Mahindra Comviva  Fidelity National Information Services (FIS)        frequent


 Q14. Which String class methods are used to make string upper case or lower case?Core Java
Admin
info@buggybread.com
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


 Q15. How to convert String to byte array and vice versa?Core Java
Admin
info@buggybread.com
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


 Q16. Why String is popular HashMap key in Java?Core Java
Admin
info@buggybread.com
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   hashmap   immutable   hashcode   hash code   advanced     TD Bank  HCL Technologies      expert        frequent


 Q17. What will be the output of the following code ?

String s1 = "Buggy Bread";
String s2 = "Buggy Bread";
if(s1 == s2)
   System.out.println("equal 1");
String n1 = new String("Buggy Bread");
String n2 = new String("Buggy Bread");
if(n1 == n2)
   System.out.println("equal 2");
Core Java
Admin
info@buggybread.com
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


 Q18. Which methods of the Pattern class have equivalent methods in the String class? Core Java
Admin
info@buggybread.com
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


 Q19. How does the String Class stores the string in memory ?Core Java2016-06-14 14:33:03

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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q20. Which method of String class is used to convert Boolean to String ?Core Java2016-12-17 11:00:44

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     Techurate Systems      Basic




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: