Core Java - Interview Questions and Answers for 'Immutable' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

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.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Immutable' - 32 question(s) found - Order By Newest

next 30
Advanced level question. Frequently asked in High end product companies. Frequently asked in Google , Cognizant and Deloitte ( Based on 2 feedback )
  Q1. Why is String immutable in Java ?Core Java
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


Asked multiple times in Capgemini.
 Q2. Why Char array is preferred over String for storing password?Core Java
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


 Q3. How does making string as immutable helps with securing information ? How does String Pool pose a security threat ?Core Java
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


 Q4. How is string object immutable if we can concat a string to it ?Core Java
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


 Q5. How to implement an immutable class ?Core Java
Ans. We can make a class immutable by

1. Making all methods and variables as private.

2. Setting variables within constructor.

Public Class ImmutableClass{

private int member;
ImmutableClass(int var){
member=var;
}
}

and then we can initialize the object of the class as

ImmutableClass immutableObject = new ImmutableClass(5);

Now all members being private , you cant change the state of the object.

 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   technical lead     Asked in 5 Companies      intermediate        frequent

Try 2 Question(s) Test


Very Frequently asked. Usually asked along with String Class related questions.
  Q6. What is an immutable class ?Core Java
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 asked with questions related to String. Frequently asked at CTS / Cognizant
  Q7. What is an Immutable Object ?Core Java
Ans. Object that can't be changed after instantiation.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   immutable  immutability     Asked in 27 Companies      basic        frequent

Try 2 Question(s) Test


 Q8. Does Declaring an object "final" makes it immutable ?Core Java
Ans. Only declaring variables as final makes them immutable. Making objects final means that the object handler cannot be used to target some other object but the object is still mutable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   final   immutable  immutability   immutable  immutability object


 Q9. Why String is popular HashMap key in Java?Core Java
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


 Q10. Is String final in Java ?Core Java
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


 Q11. What are stateless objects ? How are they different from immutable objects ? Which of these two is thread safe ?Object Oriented Programming
Ans. Stateless objects are the objects without instance fields (instance variables). The class may have compile time constants i.e static final fields.Immutable objects are the objects which have state but the state cannot be changed after initialization. Both are Thread safe.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   stateless objects   immutable  immutability objects   objects   singleton scope


 Q12. What are advantages of stateless and / or immutable objects ?Object Oriented Programming
Ans. They can be shared across multiple threads as they are thread safe.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   stateless objects   immutable  immutability objects   objects


 Q13. Which of the following class creates mutable objects ?

a. Boolean
b. File
c. String
d. StringBuffer
Ans. StringBuffer

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   immutable  immutability


 Q14. Why do we use a copy constructor ?
Ans. Copy Constructor is used for creating a duplicate of another object. Duplicate object will have the same state of the copied object but will have independent values in different memory locations. Copy Constructor can be useful to create duplicates of immutable objects as the Original cannot be tampered. Moreover It can be useful if base copies are required for individual requests in threading.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     copy constructor  clone  cloning object   immutable  immutability  immutability


 Q15. Does it make sense to clone an object which is supposed to be immutable ?Design
Ans. It make sense only if we intend to modify either of the object and would like to preserve original state in other. Otherwise we can reuse the original object by making it singleton.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     clone  clone objects  prototype design pattern   immutable  immutability  immutability  cloning


 Q16. What is the difference between Collections.emptyList() and creating new instance of List using new ?Core Java
Ans. But Collections.emptyList() returns an Immutable list whereas new arraylist() creates a mutable list.

Advantage of getting an empty list using Collections.emptyList is that it returns a singleton list which can be shared among many references and hence made immutable. This is good fit for situations where we would like to initialize a list to basic minimum empty to avoid null pointer exception.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     collections  Collections.emptyList  immutable  immutability  immutability


 Q17. How is String class unique ?Core Java
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


 Q18. What are the benefits of creating immutable objects ?Core Java
Ans. 1. Security and Safety - They can be shared across multiple threads as they are thread safe. Moreover, it protects then from bad state due to interception by the other code segment. One such problem due to mutability and access by alternate code segment could be the change of hash code and then the impact on its search with hash collections.

2. Reuse - In some cases they can be reused as only one copy would exist and hence it can be relied upon. For example - String Pool

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     immutable  immutability objects     Asked in 1 Companies      Intermediate        frequent


 Q19. Make the following class immutable

public class Employee {

   private long employeeId;

   private String employeeName;

   public long getEmployeeId() {
      return employeeId;
   }

public void setEmployeeId(long employeeId) {
      this.employeeId = employeeId;
   }

   public String getEmployeeName() {
      return employeeName;
   }

public void setEmployeeName(String employeeName) {
      this.employeeName = employeeName;
   }
}
Core Java
Ans. public class Employee {

   private long employeeId;

   private String employeeName;

   public Employee(long employeeId,String employeeName){
      this.employeeId = employeeId;
      this.employeeName = employeeName;
   }

   public long getEmployeeId() {
      return employeeId;
   }

   public long getEmployeeName() {
      return employeeName;
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     immutable  immutability  immutable  immutability class


 Q20. What are the examples of immutable objects in Java ?Core Java
Ans. Following Classes in Java SE creates immutable objects

String class
Wrapper Classes like Integer, Float etc.
StackTraceElement
Most Enum classes
File Class
Locale Class

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     immutable  immutability objects  immutable  immutability     Asked in 1 Companies      Basic        frequent


 Q21. What is the difference between being final and being immutable ? Are string final or immutable in Java ?Core Java
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


 Q22. Does Wrapper classes produces immutable objects ? Core Java
Ans. Yes

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     wrapper classes  immutable  immutability


 Q23. Why shouldn't we use string concatenation extensively or in a loop ?Core Java
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


 Q24. Is immutability an advantage with Wrapper classes over primitive types ? Core Java
Ans. There is no concept of de referencing with primitive types and hence they are implicitly immutable. Having wrapper classes as mutable offers disadvantages compared to primitive types. Wrapper classes being immutable offer similar advantage as primitive types.It actually overshadows the disadvantage wrapper class could have if they are immutable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     immutable  immutability classes  wrapper classes


 Q25. Which immutable classes have you worked with ? Can you name some immutable Collections ? How to make an immutable collection ?Core Java
Ans. string and wrapper class objects

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     immutability  immutable  immutability classes   immutable  immutability collections     Asked in 3 Companies


 Q26. What are the benefits of immutability or Immutable classes ?Core Java
Ans. Immutable objects are thread-safe so you will not have any synchronization issues.Immutable objects are good for Map keys and Set elements, since these typically do not change once created.Immutability makes it easier to write, use and reason about the code (class invariant is established once and then unchanged)Immutability makes it easier to parallelize your program as there are no conflicts among objects.The internal state of your program will be consistent even if you have exceptions.References to immutable objects can be cached as they are not going to change.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     immutable  immutability classes   benefits of immutable  immutability classes     Asked in 1 Companies


 Q27. What are the drawbacks of creating immutable objects ? Why don't we create all object immutable if mutability possesses so much threat ? Core Java
Ans. Immutable objects relieves us from the problems of inconsistencies and security and helps with better read performance but at the same time are write performance and storage overheads. In case any modification is required , a new object is created and thus creating multiple copies of it.

This is the reason we use StringBuffer / StringBuilder when we have to append some text multiple times and then create a String out of it.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     immutable  immutability objects  immutability


 Q28. Does immutability facilitates better performance ?Core Java
Ans. Yes , but only the read performance in case we don't need to modify it much. In case we need to modify it a lot , it creates write performance overheads as we would need to create many news objects instead of just changing one.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     immutable  immutability objects  immutability


 Q29. What is the programming advantage of Immutability ?Design
 This question was recently asked at 'Overstock.com'.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     mmutable  immutability  immutabilit     Asked in 1 Companies


 Q30. What are the disadvantages of immutability ?Design
 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     mmutable  immutability  immutabilit


next 30

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: