Java - Interview Questions and Answers for 'Memory management' | 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 'Memory management' - 42 question(s) found - Order By Newest

 Q1. Which are the different segments of memory ?Core Java
Anonymous
Ans. 1. Stack Segment - Contains primitives, Class / Interface names and references.

2. Heap Segment - Contains all created objects in runtime, objects only plus their object attributes (instance variables), Static variables are also stored in heap.

3. Code Segment - The segment where the actual compiled Java bytecodes resides when loaded

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

   Like      Discuss      Correct / Improve     java   memory   memory management   stack memory   heap memory   code segment memory   advanced     NX Developer  Velocity  Commvault  eBizprise Taiwan  Tata Consultancy (TCS)      expert        frequent

Try 6 Question(s) Test


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


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


 Q4. Describe what happens when an object is created in Java ?Core Java
Anonymous
Ans. 1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. Implemenation-specific data includes pointers to class and method data.
2. The instance variables of the objects are initialized to their default values.
3. The constructor for the most derived class is invoked. The first thing a constructor does is call the constructor for its superclasses. This process continues until the constructor for java.lang.Object is called,as java.lang.Object is the base class for all objects in java.
4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.

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

   Like      Discuss      Correct / Improve     java   memory management   object creation   advanced   ebay


Advanced level question usually asked in High end product companies. Have been asked in Google and Amazon (Based on 1 Feedback)
  Q5. 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


Advanced level question. Recently asked in few Indian service companies ( Based on 3 inputs )
 Q6. What are various types of Class loaders used by JVM ?Core Java
Admin
info@buggybread.com
Ans. Bootstrap - Loads JDK internal classes, java.* packages.

Extensions - Loads jar files from JDK extensions directory - usually lib/ext directory of the JRE

System - Loads classes from system classpath.

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

   Like      Discuss      Correct / Improve     java   jvm   memory management   class loaders   bootstrap   extensions   system  classloaders   advanced   technical lead   technical architect     Deloitte  Mindtree  Accenture  Synechron  IBM


 Q7. Why two types of memory - stack and heap are required by Java ?Core Java2016-12-21 13:08:55

Ans. Because of the life cycle requirement for different type of values in java.

variables initialized and used in functions needs to be destructed with the execution of function and hence kept in stack. Same is applicable for the object references initialized within the method. If objects would have been created in stack, they wouldnt have been passed around across methods and hence they are created on heap.

So anything that is required beyond the scope of a method or function is kept on heap which is usually garbage collected by Java.

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

   Like      Discuss      Correct / Improve     memory management  heap  stack

Try 1 Question(s) Test


 Q8. Different types of memory used by JVM ?Core Java
Anonymous
Ans. Class , Heap , Stack , Register , Native Method Stack.

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

   Like      Discuss      Correct / Improve     java   jvm   memory management   memory   advanced   architecture   technical architect     Priceline.com      intermediate        frequent

Try 5 Question(s) Test


 Q9. What is a class loader ? What are the different class loaders used by JVM ?Core Java
Anonymous
Ans. Part of JVM which is used to load classes and interfaces.

Bootstrap , Extension and System are the class loaders used by JVM.

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

   Like      Discuss      Correct / Improve     java   class loaders  classloaders   jvm   java memory management   advanced



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



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


 Q11. Why do member variables have default values whereas local variables don't have any default value ?
Core Java
Anonymous
Ans. member variable are loaded into heap, so they are initialized with default values when an instance of a class is created. In case of local variables, they are stored in stack until they are being used.

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

   Like      Discuss      Correct / Improve     java   jvm   memory management   variables   stack memory   heap memory   default values      expert


 Q12. How are classes loaded by JVM ?Core Java
Admin
info@buggybread.com
Ans. Class loaders are hierarchical. The very first class is specially loaded with the help of static main() method declared in your class. All the subsequently loaded classes are loaded by the classes, which are already loaded and running.

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

   Like      Discuss      Correct / Improve     java   jvm   memory management   class loaders  classloaders     Synechron


 Q13. Difference between static vs. dynamic class loading?Core Java
Admin
info@buggybread.com
Ans. static loading - Classes are statically loaded with Java new operator.

dynamic class loading - Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time.

Class.forName (Test className);

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

   Like      Discuss      Correct / Improve     java   jvm   memory management   class loaders  classloaders   static class loading   static loading   dynamic class loading   dynamic loading     IBM  JDA      expert        frequent


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


 Q15. Which memory areas does instance and static variables use ?Core Java
Admin
info@buggybread.com
Ans. instance variables and objects are stored on heap and the references are stored on stack whereas static variables are stored in the method area of heap.

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

   Like      Discuss      Correct / Improve     java   jvm   memory management   memory   heap   stack

Try 2 Question(s) Test


 Q16. What is PermGen or Permanent Generation ?Core Java
Admin
info@buggybread.com
Ans. The memory pool containing all the reflective data of the java virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas. The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here.

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

   Like      Discuss      Correct / Improve     java   jvm   memory management   permgen   permanent generation   advanced   architecture     JP Morgan  SAP  Informatica  Credit Agricole  EXFO  EMC  Credit Agricole  Cognizant (CTS)  JP Morgan      expert


 Q17. What is metaspace ?Core Java
Admin
info@buggybread.com
Ans. The Permanent Generation (PermGen) space has completely been removed and is kind of replaced by a new space called Metaspace. The consequences of the PermGen removal is that obviously the PermSize and MaxPermSize JVM arguments are ignored and you will never get a java.lang.OutOfMemoryError: PermGen error.

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

   Like      Discuss      Correct / Improve     java   jvm   memory management   permgen   permanent generation   metaspace   advanced   architecture      expert


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


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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q20. What will happen if we don't have termination statement in recursion ?
Manoj
Ans. Function call allocates a stackframe in stack. Every stackframe will use some memory to store local variables, parameters and to remember return address. Without terminating condition stackframes will keep consuming memory from stack and eventually program will result in stackoverflow error.

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

   Like      Discuss      Correct / Improve     recursion   break statement  break  stackoverflow exception  stackoverflow  memory management  memory  exceptions


 Q21. Which memory segment loads the java code ?Core Java
Anonymous
Ans. Code segment.

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

   Like      Discuss      Correct / Improve     memory   java   memory management   code segment      expert        rare


 Q22. Which kind of memory is used for storing object member variables and function local variables ?
Anonymous
Ans. Local variables are stored in stack whereas object variables are stored in heap.

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

   Like      Discuss      Correct / Improve     java   jvm   memory management   stack memory   heap memory      expert


 Q23. Why Java don't use pointers ?
Anonymous
Ans. Pointers are vulnerable and slight carelessness in their use may result in memory problems and hence Java intrinsically manage their use.

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

   Like      Discuss      Correct / Improve     java   jvm   memory management   pointers      intermediate


 Q24. Name few tools for probing Java Memory Leaks ?
Admin
info@buggybread.com
Ans. JProbe, OptimizeIt

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

   Like      Discuss      Correct / Improve     java   tools   jvm   memory management   memory leaks   jprobe   optimeit   architecture   technical lead


 Q25. Name few classes java provides for memory management ?
Admin
info@buggybread.com
Ans. http://www.buggybread.com/2015/01/java-memory-classes-and-interfaces.html

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

   Like      Discuss      Correct / Improve     java   memory management


 Q26. Difference between Stack and Heap memory ?Core Java
Admin
info@buggybread.com
Ans. Stack memory areas is used to hold method and local variables while objects are always allocated memory in the heap. The heap memory is shared between multiple threads whereas Stack memory isn't.

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

   Like      Discuss      Correct / Improve     java   memory management   stack memory   heap memory   difference between   ebay     ABCD Corp  Society for Worldwide Interbank Financial Telecommunication (SWIFT)      intermediate        frequent

Try 1 Question(s) Test


 Q27. How can we find the memory usage of JVM from Java code?Core Java2016-11-17 11:36:33

Ans. Using Runtime object.

Runtime runtime = Runtime.getRuntime();

runtime.totalMemory();
runtime.freeMemory();

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

   Like      Discuss      Correct / Improve     jvm  memory management        rare


Frequently asked in Alibaba (Based on 2 feedback)
 Q28. What is the best Memory setting for JVM ?Core Java2016-12-13 10:18:56

 This question was recently asked at 'Alibaba'.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     jvm  memory management  jvm best memory setting     Alibaba      intermediate


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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q30. Which memory segment holds String Pool in Java ?Core Java2017-01-14 20:57:22

Ans. String Pool resides in runtime constant Pool which is a part of Heap memory segment.

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

   Like      Discuss      Correct / Improve     string pool  memory management      Expert        rare


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


 Q32. What is JVM ?Core Java2017-04-07 10:16:18

 This question was recently asked at 'ABS,Trimax IT Infrastructure & Services'.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  jvm     ABS  Trimax IT Infrastructure & Services      basic        frequent


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


 Q34. What is the purpose of String Pool ? Don't you think it's a performance overhead to have unnecessary check for string in string pool ?Core Java2017-05-13 09:05:03

Ans. String Pool makes Java more memory efficient by providing a reusable place for string literals. It might be a little performance inconvenience but results in good amount memory saving.

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

   Like      Discuss      Correct / Improve     string pool  memory management      Intermediate


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


 Q36. In which memory segment - heap or stack, the following stored by JVM

1. static members
2. objects
3. object references
4. local or method variables

Core Java2017-07-30 08:20:15

Ans. static members are stored in method area of heap

objects are stored on heap

object references are stored on stack

local or method variables are stored on stack

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

   Like      Discuss      Correct / Improve     jvm  memory management  stack  heap


 Q37. Function variables are kept in ..Core Java
a. Stack
b. Heap
c. Both Stack and Heap
d. None of them

Ans.a. Stack

 Q38. Which memory segment holds String Pool ?Core Java
a. Stack
b. Heap
c. Code Segment
d. Class Segment

Ans.b. Heap

 Q39. In this code

public class BuggyBread1{
   private static String staticVariable = "static Variable";
   
   private String instanceVariable = "instance Variable";
   
   public static void main (String args[]) {
String localVariable = "local Variable";
}
}

Where will the value 'static Variable' be stored ?
Core Java
a. Stack Memory Segment
b. Heap Memory Segment
c. Runtime Constant Pool within Heap Segment
d. Code Segment

Ans.c. Runtime Constant Pool within Heap Segment

 Q40. In this code

public class BuggyBread1{
   private static String staticVariable = "static Variable";
   
   private String instanceVariable = "instance Variable";
   
   public static void main (String args[]) {
String localVariable = new String("local Variable");
}
}

Where will the value 'local Variable' be stored ?
Core Java
a. Stack Memory Segment
b. Heap Memory Segment
c. Runtime Constant Pool within Heap Segment
d. Code Segment

Ans.a. Stack Memory Segment

 Q41. In this code

public class BuggyBread1{
   private static String staticVariable = new String("static Variable");
   
   private String instanceVariable = "instance Variable";
   
   public static void main (String args[]) {
String localVariable = "local Variable";
}
}

Where will the value 'static Variable' be stored ?
Core Java
a. Stack Memory Segment
b. Heap Memory Segment
c. Runtime Constant Pool within Heap Segment
d. Code Segment

Ans.b. Heap Memory Segment

 Q42. 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: