Core Java - Interview Questions and Answers for 'Cloning' | 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 'Cloning' - 15 question(s) found - Order By Rating

 Q1. Explain CloneNotSupportedException ?Core Java
Ans. If we do not mention, Cloneable Interface to the Class which we want to Clone then we get this exception, only if we try to clone an object

Like:
public class TestClone{
@Override
   protected Object clone() throws CloneNotSupportedException {
      return super.clone();
   }
}

In Main, You try to do:
TestClone clone = new TestClone();
      
      TestClone clone2 = (TestClone) clone.clone();

You will get CloneNotSupportedException.

Just add -> public class TestClone implements Cloneable {

and things are fixed.

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

   Like         Discuss         Correct / Improve     exceptions handling  cloning


 Q2. How to avoid cloning, serialization in the singleton class ?Design
Ans. For Cloning-exception,For deserialization-read.resolve()

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

   Like         Discuss         Correct / Improve     singleton  cloneable  serializable  serialization  cloning     Asked in 1 Companies


 Q3. Design a Program to clone an object and all it's children.
Design
 This question was recently asked at 'Bloomberg'.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     code  coding  cloning  design pattern   cloning design pattern     Asked in 1 Companies


 Q4. Tell about shallow and deep cloning ?Core Java
 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     cloning


 Q5. Why the singleton class should never implement Cloneable interface ?Design
Ans. Because Singleton implementation doesn't restrict it from cloning and hence we can have multiple objects when we actually don't intend it to have multiple objects.

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

   Like         Discuss         Correct / Improve     singleton  cloning


Frequently asked in Genpact.
 Q6. What is object cloning ?Core Java
Ans. Its is the process of creating exact copy of an object being cloned. In Object class one native method called clone() is there which is meant for Shallow Cloning of Object. Shallow cloning means bitwise copy of an object.In case of primitive data type it will create an exact copy of primitive values as well as variables but if the object contains any reference of an object then it will not copy the referenced object rather it will create the copy of reference variable and assigned it to the old object.

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

   Like         Discuss         Correct / Improve     cloning     Asked in 3 Companies      basic        frequent


 Q7. What is cloning in GIT ?GIT
Ans. Cloning is the process of making a copy. It could be creating local copy of another local repository or local copy of remote repository.

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

   Like         Discuss         Correct / Improve     GIT  Configuration Management  GIT Cloning


 Q8. Difference between shallow copy and object cloning ?Core Java
Ans. Shallow copy is one of the way for object cloning, other being deep copy.

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

   Like         Discuss         Correct / Improve     cloning  shallow copy  object cloning      basic


Frequently asked Design Pattern interview question.
 Q9. What is a prototype design pattern ?Design
Ans. The prototype pattern is a creational design pattern. It is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. Prototype is used when we need duplicate copies of objects.

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

   Like         Discuss         Correct / Improve     design pattern  prototype design pattern  cloning     Asked in 11 Companies      intermediate


 Q10. Why Collection interface does not extend Cloneable interface ?Core Java
Ans. Here is the list of classes that implements Collections Interface - http://www.buggybread.com/2015/02/java-collections-classes-that-implement.html

Having Collection interface to extend Cloneable interface would mean necessarily implement clone method by all implementing classes. As not all collection classes allow duplicate elements, it makes no sense to clone elements for them.

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

   Like         Discuss         Correct / Improve     cloneable  collections  collection interface  cloning


 Q11. 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


 Q12. 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


 Q13. What is a cloneable interface and what all methods does it contain?Core Java
Ans. Cloneable is a declaration that the class implementing it allows cloning or bitwise copy of it's object state. It is not having any method because it is a MARKER interface.

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

   Like         Discuss         Correct / Improve     java   oops   cloneable   marker interface  cloning     Asked in 2 Companies


Frequently asked in Infosys and HCL Technologies ( Based on 2 feedback )
 Q14. What are different ways of object creation in Java ?Core Java
Ans. Using new operator - new xyzClass()

Using factory methods - xyzFactory.getInstance( )

Using newInstance( ) method - (Class.forName(xyzClass))emp.newInstance( )

By cloning an already available object - (xyzClass)obj1.clone( )

  Sample Code for object initialization using clone

  Sample Code for object initialization using getInstance

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

   Like         Discuss         Correct / Improve     java   oops   object creation   new operator   class.forname   cloning   ebay     Asked in 9 Companies      expert


 Q15. Name few classes that implement Cloneable interface ?
Ans. http://www.buggybread.com/2015/02/java-collections-classes-that-implement_58.html

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

   Like         Discuss         Correct / Improve     java   cloneable  cloning



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: