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

next 30
  Q1. Which are the different segments of memory ?Core Java
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     Asked in 9 Companies      expert        frequent

Try 6 Question(s) Test


 Q2. Does garbage collection guarantee that a program will not run out of memory?Core Java
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     Asked in 2 Companies

Try 1 Question(s) Test


 Q3. What are different ways to create String Object? Explain.Core Java
Ans. There are total six ways

1. literals

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.

2. new keyword

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.

3. string buffer
4. string builder
5. System.out.println
6. char to string




 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     Asked in 5 Companies      basic        frequent

Try 3 Question(s) Test


Frequently asked in Infosys India
  Q4. What is a String Pool ?Core Java
Ans. String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.

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

   Like         Discuss         Correct / Improve     java   oops   string   string class   string pool   heap memory     Asked in 31 Companies      intermediate        frequent

Try 2 Question(s) Test


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
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     Asked in 21 Companies      intermediate        frequent

Try 4 Question(s) Test


 Q6. Describe what happens when an object is created in Java ?Core Java
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. Recently asked in few Indian service companies ( Based on 3 inputs )
 Q7. What are various types of Class loaders used by JVM ?Core Java
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     Asked in 7 Companies


 Q8. Why two types of memory - stack and heap are required by Java ?Core Java
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


 Q9. When are static variables loaded in memory ?Core Java
Ans. They are loaded at runtime when the respective Class is loaded.

  Sample Code for static variable

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

   Like         Discuss         Correct / Improve     java   oops   static   static variable   memory      intermediate


 Q10. Explain java.lang.OutOfMemoryError ?Core Java
Ans. This Error is thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.

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

   Like         Discuss         Correct / Improve     java   exceptions   jvm   outofmemoryerror     Asked in 1 Companies


 Q11. Should we override finalize method ?
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


 Q12. What are strong, soft, weak and phantom references in Java ?Core Java
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     Asked in 2 Companies


 Q13. Which memory areas does instance and static variables use ?Core Java
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


 Q14. What are the disadvantages of using arrays ?Core Java
Ans. Arrays are of fixed size and have to reserve memory prior to use. Hence if we don't know size in advance arrays are not recommended to use.

Arrays can store only homogeneous elements.

Arrays store its values in contentious memory location. Not suitable if the content is too large and needs to be distributed in memory.

There is no underlying data structure for arrays and no ready made method support for arrays, for every requriment we need to code explicitly

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

   Like         Discuss         Correct / Improve     java   arrays   disadvantages of arrays   arrays memory   arrays vs variables


 Q15. Can we call the garbage collector explicitly ?Core Java
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     Asked in 4 Companies      expert

Try 1 Question(s) Test


 Q16. What will happen if we don't have termination statement in recursion ?
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


 Q17. Different types of memory used by JVM ?Core Java
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     Asked in 1 Companies      intermediate        frequent

Try 5 Question(s) Test


 Q18. What is a class loader ? What are the different class loaders used by JVM ?Core Java
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     Asked in 3 Companies


 Q19. Which kind of memory is used for storing object member variables and function local variables ?
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


 Q20. Why do member variables have default values whereas local variables don't have any default value ?
Core Java
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


 Q21. How are classes loaded by JVM ?Core Java
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     Asked in 1 Companies


 Q22. Difference between static vs. dynamic class loading?Core Java
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     Asked in 2 Companies      expert        frequent


 Q23. What is PermGen or Permanent Generation ?Core Java
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     Asked in 9 Companies      expert


 Q24. What is metaspace ?Core Java
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


 Q25. How substring() method of String class create memory leaks?Core Java
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


 Q26. Difference between Stack and Heap memory ?Core Java
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     Asked in 2 Companies      intermediate        frequent

Try 1 Question(s) Test


 Q27. Which type of memory is cleaned / recovered by garbage collection - stack or heap ?Core Java
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     Asked in 2 Companies


 Q28. Difference between Virtual Memory and Demand Paging?Operating System
 This question was recently asked at 'Veritas'.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     Virtual Memory vs Demand Paging     Asked in 1 Companies


 Q29. Java doesn't provide exclusive access to memory like C/C++ and other lower level languages ? What are the advantanges and disadvantages ?Core Java
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


 Q30. Which memory segment loads the java code ?Core Java
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  code segment memory      expert        rare


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: