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

Search Java Interview Questions


 2137 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 'Stack' - 14 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. 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


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


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


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


 Q6. Write a Program to implement stack using an array2017-07-30 14:26:25

Ans. public class Stack{
   static int top = 0;
   static Element[] stack = new Element[10];
   
   static class Element {
      int body;
      
      Element(int value){
         body = value;
      }
   }
   
   public static void main(String[] args){
      push(5);
      System.out.println(top);
      push(7);
      System.out.println(top);
      poll();
      System.out.println(top);
      poll();
      System.out.println(top);   
   }
   
   private static void push(int value){
      Element newElement = new Element(value);
      top++;
   }
   
   private static Element poll(){
      Element topElement = stack[top];
      top--;
      return topElement;
   }
}

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

   Like      Discuss      Correct / Improve     stack


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


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


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



 Q10. What will happen if we don't have termination statement in recursion ?Core Java2017-04-18 20:13:15

Ans. It would result in endless function calls and hence eventually would result in stackoverflow exception.

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

   Like      Discuss      Correct / Improve     recursion  stackoverflowexception      Basic        frequent


 Q11. Given a string and index for the opening bracket, find the index of matching closing bracket. Core Java2017-05-07 20:11:06

 This question was recently asked at 'Amazon'.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     matching bracket  stack     Amazon


 Q12. What is the difference between Stack and Queue ?Data Structures2017-06-18 18:10:36

Ans. Stack is based on Last in First out (LIFO) principle while a queue is based on FIFO (First In First Out) principle.

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

   Like      Discuss      Correct / Improve     data structure  stack vs queue  stack  queue      Basic


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


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

Ans.a. Stack



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

StackOverflow Jobs: I like stackoverflow and linkedin specifically for ... James

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: