Java - Interview Questions and Answers for 'Garbage collection' | 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

   



Universal Garbage Collection Log Analyser



Link: http://gceasy.io/
   




Interview Questions and Answers for 'Garbage collection' - 27 question(s) found - Order By Newest

 Q1. Does garbage collection guarantee that a program will not run out of memory?Core Java
Anonymous
Ans. Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection

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

   Like      Discuss      Correct / Improve     java   garbage collection   java memory management   advanced

Try 1 Question(s) Test


Advanced level question usually asked in High end product companies. Have been asked in Google and Amazon (Based on 1 Feedback)
  Q2. Describe, in general, how java's garbage collector works ?Core Java
Anonymous
Ans. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is known as garbage collection. The Java runtime environment supports a garbage collector that periodically frees the memory used by objects that are no longer needed. The Java garbage collector is a mark-sweep garbage collector that scans Java dynamic memory areas for objects, marking those that are referenced. After all possible paths to objects are investigated, those objects that are not marked (i.e. are not referenced) are known to be garbage and are collected.

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

   Like      Discuss      Correct / Improve     java   garbage collection   java memory management   advanced     LinkedIn  Infosys  Google  Netflix  Amazon  Huawei Technologies  JP Morgan  enGenius  CitiusTech  Goldman Sachs  Bank Of America India  Salesforce  Addteq  Criteo  Capgemini  Horizon Solutions      intermediate        frequent

Try 4 Question(s) Test


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


 Q4. Should we override finalize method ?
Anonymous
Ans. Finalize is used by Java for Garbage collection. It should not be done as we should leave the Garbage Collection to Java itself.

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

   Like      Discuss      Correct / Improve     java   garbage collection   java memory management   finalize

Try 2 Question(s) Test


 Q5. What are strong, soft, weak and phantom references in Java ?Core Java
Admin
info@buggybread.com
Ans. Garbage Collector wont remove a strong reference.

A soft reference will only get removed if memory is low.

A weak reference will get removed on the next garbage collection cycle.

A phantom reference will be finalized but the memory will not be reclaimed. Can be useful when you want to be notified that an object is about to be collected.

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

   Like      Discuss      Correct / Improve     java   memory management   jvm   garbage collections   references   strong reference   soft reference   weak reference   phantom reference   architecture     Travelocity  Patni Computers


 Q6. Can we call the garbage collector explicitly ?Core Java
Admin
info@buggybread.com
Ans. Yes, We can call garbage collector directly but it doesn't guarantees that the gc will start executing immediately. This gc( ) method appears in both Runtime and System classes of java.lang package.

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

   Like      Discuss      Correct / Improve     java   garbage collection   java memory management   jvm   gc   yes no     Hewlett Packard (HP)  eGain  Egen Solutions  Computer Sciences Corporation (CSC)      expert

Try 1 Question(s) Test


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


 Q8. Let's suppose we have an to destroy objects, Design a program to implement garbage collection ?Design2017-08-04 14:08:01

Ans. Step 1 - We can create a Registry Class having Map of all created objects as key and References list as value.

Step 2 - Whenever we create an object , we should update the Registry in the constructor to add the new object.

Step 3 - Whenever we assign a new reference to the object , we need to update the entry in Map. Similarly if the reference get's removed ( end of scope etc ), we need to remove the entry of reference from the list.

Step 4 - We can have threaded code to monitor the Map to see if any object looses all it's references and should call the method to destroy object and clean the memory.

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

   Like      Discuss      Correct / Improve     garbage collection


 Q9. What is a finalize method ?Core Java
Anonymous
Ans. finalize() method is called just before an object is destroyed.

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

   Like      Discuss      Correct / Improve     java   oops   finalize   garbage collection   basic interview question     Daffodil Software      basic        frequent

Try 1 Question(s) Test



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



Usually asked only to fresh graduates and less experienced developers.
  Q10. What is a daemon thread? Give an Example ?
Admin
info@buggybread.com
Ans. These are threads that normally run at a low priority and provide a basic service to a program or programs when activity on a machine is reduced. garbage collector thread is daemon thread.

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

   Like      Discuss      Correct / Improve     java   threads   multi threading   operating system   daemon thread   garbage collection     Finnet  ServiceNow  MarketAxess Holdings  JP Morgan  Utopia  Samsung  CGI Group  Auriel Technologies      intermediate        frequent

Try 2 Question(s) Test


 Q11. Difference between HashMap and WeakHashMap ?
Admin
info@buggybread.com
Ans. WeakHashMap uses weak reference for keys, which means if the key object doesn't have any reference then both key/value mapping will become eligible for garbage collection.

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

   Like      Discuss      Correct / Improve     java   collections   hashmap   weakhashmap   difference between   garbage collection   ebay


  Q12. What is a Destructor ? Do we have Destructors in Java ?Core Java

Ans. Destructor is used to de-allocate memory allocated by objects.

There are no destructors in Java. Alternatively, Java provides Automatic garbage collection i.e automatically releasing the un-referenced memory.

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

   Like      Discuss      Correct / Improve     destructor  garbage collection     Mahindra Comviva  Quinnox  MathWorks  Wipro  Publishers Clearing House (PCH)  Compro Technologies      Basic        frequent

Try 1 Question(s) Test


 Q13. Which type of memory is cleaned / recovered by garbage collection - stack or heap ?Core Java2016-12-21 13:09:29

Ans. Heap memory.

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

   Like      Discuss      Correct / Improve     memory management  stack  heap  garbage collection


 Q14. What kind of thread is garbage collection thread ?Core Java2017-01-12 14:15:59

Ans. Daemon thread

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

   Like      Discuss      Correct / Improve     garbage collection      intermediate

Try 2 Question(s) Test


 Q15. What is memory leak ? How Java helps countering memory leaks compared to C++ ?Core Java2017-01-19 14:04:02

Ans. Memory Leak is a situation wherein we have a reserved memory location but the program has lost its reference and hence has no way to access it.

Java doesn't have concept of Pointers, Moreover Java has concept of Garbage collection that frees up space in heap that has lost its references.

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

   Like      Discuss      Correct / Improve     memory leak  garbage collection      intermediate        frequent


 Q16. Why don't Java objects have destructors ?Core Java2017-02-03 08:27:51

 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     destructor  garbage collection


 Q17. Why do we need Garbage collection ?Core Java2017-03-27 08:44:06

Ans. Garbage collection mechanism frees up the memory that is no longer needed. In languages like C / C++ the deallocation needs to be done explicitly by the programmer and hence any leniency may result in memory leak. Garbage collection in java ensures that all unused memory is reclaimed and hence there are no memory leaks.Moreover it relieves the programmer from the hassle of carefully releasing all memory.

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

   Like      Discuss      Correct / Improve     Garbage Collection  memory management     Compro Technologies


 Q18. How is garbage collection in Java 8 different from Java 7 ? Core Java2017-04-30 09:51:26

 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     memory management  garbage collection


 Q19. How can we destroy an object ? Core Java2017-05-07 20:39:20

Ans. We cannot explicitly initiate destruction of an object as no destroy method is available in java. Only Garbage collection can destroy an object.

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

   Like      Discuss      Correct / Improve     Garbage collection  object destruction     Sabre



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q20. When does an object cleaned or destroyed by garbage collection ?Core Java2017-05-07 20:41:46

Ans. When it is no longer referenced or it looses all references which were earlier pointing to it.

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

   Like      Discuss      Correct / Improve     garbage collection  object destruction     Sabre


 Q21. Java doesn't provide exclusive access to memory like C/C++ and other lower level languages ? What are the advantanges and disadvantages ?Core Java2017-05-20 16:22:05

Ans. Yes, doesn't provide exclusive access as we cannot allocate and deallocate memory exclusively as Java internally manages it. The advantage of this is that it relieves the coder for such tasks and helps protect from many bugs that may get introduced with imperfect coding. Moreover as java garbage collector collects all unclaimed memory or objects, it helps the application from memory leaks.

On the flip side , as coder doesn't have extensive excess to memory , it is upto java to decide on the state for programming construct and data storage and hence may introduce some security risks. For example - Java keeps string literals in string pool and there is no exclusive way to remove it and hence may stay and sensitive data in string pool may introduce security issues. Moreover when we overwrite a value or object for a variable / reference, it is upto java to purge those values and hence it may stay in memory for a while till java decide that it is no longer referenced and hence should be removed and hence makes it vulnerable for inappropriate access.

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

   Like      Discuss      Correct / Improve     disadvantages of garbage collection  advantages and disadvantages of java memory management  java for security applications  java with sensitive data  memory management


 Q22. What happens when you assign a pre initialized reference some other object ?Core Java2017-05-22 08:31:02

Ans. Now the reference points to a new object in memory. If that was the only reference for the previous object , it will be marked for garbage collection.

Foe example -

Object obj = new Object();
obj = new Object();

object created in first line will be eligible for garbage collection after line 2 as it looses all it's handlers.

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

   Like      Discuss      Correct / Improve     references  objects  garbage collection     UST Global


 Q23. In Java, would you like to see an explicit destructor?Core Java2017-06-23 13:17:35

 This question was recently asked at 'Jim Beam Luxury'.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     destructor  garbage collection     Jim Beam Luxury


 Q24. Which of the following methods are used by Java Garbage Collection Mechanism ?Core Java
a. final
b. finally
c. finalize
d. All of the above

Ans.c. finalize

 Q25. Which of the following about Garbage collection is false ?Core Java
a. We can call Garbage collection explicitly
b. Garbage Collection guarantees that the application will not run out of memory
c. finalize method is used by Java for Garbage Collection
d. Garbage Collection Mechanism delete unclaimed objects that are no longer required

Ans.b. Garbage Collection guarantees that the application will not run out of memory

 Q26. What kind of thread is Garbage collection thread ?Core Java
a. Daemon Thread
b. User Thread
c. System Thread
d. Active Thread

Ans.a. Daemon Thread

 Q27. Which of following memory segment is cleaned by Garbage Collection Mechanism ?Core Java
a. Stack
b. Heap
c. Code
d. Cache

Ans.b. Heap



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: