Java interview questions and answers on strings

 Part 1

String interview questions for experienced in java covered in this post:


    Java interview questions and answers on strings

        What is an immutable class?

    -    Immutable class means the value or content cannot be changed once an object is created.

    -   Best example of immutable in java is the String class. The String object cannot be changed once it is created.

    -   All wrapper classes are immutable classes. Wrapper classes are Integer, Float, Double, Boolean, Byte, Short etc.

     

        Why String is immutable?

    -  String objects are immutable cause string objects are cached in SCP(String Constant pool).

    -       Cached String objects are shared between multiple clients.

    -    If anyone changed the value of the String object then all clients get affected. That’s why String is immutable in java.


     

    Click here to Purchase on AmazonClick here to Purchase on Amazon

    Click here to Purchase on Amazon

    Note: Please click on Image to Purchase the books from Amazon.in

     

        Can we modify the existing String object? If I tried to change the existing String object then what will happen?

    -          No we cannot modify the existing String class object.

    -          If you are trying to modify an existing string object then internally new object will be created and if you pointing to a newly created object to reference variable then the old object will be eligible for garbage collection.

     

        What is the difference between StringBuilder and StringBuffer?    

     

    StringBuilder

    StringBuffer

    1.

    StringBuilder is not a thread-safe.

    StringBuffer is a thread-safe.

    2.

    The performance of StringBuilder is good as compare to StringBuffer.

    The performance of StringBuffer is lower as compare to StringBuilder cause StringBuffer is thread-safe.

    3.

    StringBuilder sb = new StringBuilder(“ac”);

    StringBuffer sb = StringBuffer(“ac”);



        What is the use of the intern() method?

    -          Intern() method returns a canonical representation of String.

    -          In simple word, intern() method return String object from SCP(String Constant Pool).

     

        What is SCP(String Constant Pool)?

    -          String constant pool is a separate place in heap memory where all the String objects are stored.

    -          If you create an object using a new keyword then the object gets created on heap memory and keep the same string object into string constant pool.

    -          If you create an object without a new keyword then the object doesn’t create on heap memory that object will be created on string constant pool.

    -          If an object exists then the reference variable pointing to an existing object instead of creating a new one.

      

        What is the difference between String s = "abc" and 
                String s = new String("abc")?

    -  String s=”abc”: On this line String object will not create on heap memory. ‘abc’ string will be directly stored on the string constant pool.

    -    String s = new String(“abc”): new String(“abc”): On this line String object will be created on heap memory and ‘abc’ will be stored in string constant pool.

     

        List out some inbuilt methods of String class?

    -          String of inbuilt method of String class.

    1.       length()

    2.       substring()

    3.       contains()

    4.       equals()

    5.       concat()

    6.       replace()

    7.       isEmpty()

    8.       intern()

    9.       indexOf()

    10.   trim()

    11.   toUpperCase()

    12.   toLowerCase()

     

        What is the use of trim() method in the String class?

    -          Trim() of String class is used to remove the blank spaces of String.

    -          Trim() method removes blank spaces before and after the String.

    -          Trim() method doesn’t remove the blank spaces in between String.

     

        How to create an immutable class or What are the steps to create immutable objects?

    -          Please refer to the below steps to create an immutable class.

    1.       Create a final class so a subclass is not possible.

    2.       Make all instance variables final so no one can change the value.

    3.       Do not provide any setter method just provide a getter method.

    -          For your reference, I have created a new immutable class.

    package simplifiedjava.crackedInterview;

     

    public final class ImmutableClassDemo {

     

          private final String empCode="E001";

     

          public String getEmpCode() {

                return empCode;

          }

    }

     

        What is the default size of StringBuffer and StringBuilder?

    -          The default size of StringBuffer and StringBuilder is 16.

     

        What will be the size of StringBuffer or Builder in the following example?
    String s = "abc"; StringBuffer sb = new StringBuffer(s); System.out.println(sb.capacity());

    package simplifiedjava.crackedInterview;

     

    public class StringBuilderSizeCalculationDemo {

     

          public static void main(String[] args) {

                String s = "abc";

                StringBuffer sb = new StringBuffer(s);

                System.out.println(sb.capacity());

          }

    }

    Output: 19

     

        What will happen if I am trying to keep adding new characters once the StringBuffer or StringBuilder gets fulled?

    -          The default size of StringBuffer and StringBuilder is 16.

    -    When StringBuilder or StringBuffer object gets full and we are still inserting new characters that time internally new StringBuilder or StringBuffer object gets created copy all content of the old object to new object.

    -          Old objects will be eligible for garbage collection.

     

        Can you specify the scenario where you can implement String, StringBuffer and StringBuilder?

    -   String: When content is fixed and never change throughout life then String is a good option.

    -    StringBuffer: When content is not fixed and the requirement is thread-safety then go for StringBuffer.

    -     StringBuilder: When content is not fixed and content would be changing multiple times and no need for thread safety then go for StringBuilder.


    Previous Post                                                                    Next Post


    • Java interview questions and answers all MNC - Click here
    • Basic core java interview questions and answers for freshers - Click here
    • Core java interview questions for 3 years experience - Click here
    • Core java interview questions and answers for 3-5 years exp - Click here
    • Core java interview questions and Answers for 5 - 7 Years exp - Click here
    • Basic Java Interview Questions and Answers - Click here
    • Java interview questions and answers on oops - Click here
    • Java interview questions and answers on Strings - Click here
    • Java interview questions on exception handling - Click here
    • Interview questions on multithreading in java for experienced - Click here
    • Interview questions on serialization in java for experienced - Click here

    • Interview questions on inner class in java for experienced - Click here
    • Interview questions on Collections in java for experienced - Click here



    Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. 

    I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. 

    You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. 

    Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions. 

    My total experience is 10. Initially I had worked on some support projects and then I moved to java projects. I had worked with many multi-national companies like R-Systems, Tata Consultancy Services, Cybage Softwares.  Fortunately, TCS and Cybage has given me an opportunity to take interviews for experienced candidates. I have conducted more than 1000 interviews by now.

    Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. 

    I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. 

    I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! 

    Please reach out to me for personal training for interview preparation. 

    You can reach out to me at mncjavainterview@gmail.com.

     




    Post a Comment

    0 Comments