Interview Questions and Answers for 'Stack' | 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

   



Interview Questions and Answers for 'Stack' - 18 question(s) found - Order By Rating

 Q1. Implement a queue, using only stacksData Structure
 This question was recently asked at 'IBM India'.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     queue  stack     Asked in 1 Companies


 Q2. Implement a Stack with 2 threads in which one thread push data and another pop data.Data Structure
 This question was recently asked at 'HCL Technologies'.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     stack     Asked in 1 Companies


 Q3. Which memory segment is cleaned by Garbage Collection - stack or heap ?Core Java
Ans. Heap as objects are stored ink heap.

 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


 Q4. What is the difference between pop and peek function of stack ?Data Structure
Ans. pop method pulls the top element from the stack and then move the top to the next element whereas peek only get the top element but doesn't move the top.

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

   Like         Discuss         Correct / Improve     stack


 Q5. Find Max from Stack in O(1) complexityAlgorithm
Ans. Create one extra field called MAX O(1)

when you an element to the stack check these two condition
1. Stack is empty, then MAX = element
2. Stack is not empty then check if the element is greater than MAX then MAX = element


when getMax fuction is called, then return MAX

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

   Like         Discuss         Correct / Improve     complexity  stack  data structure     Asked in 1 Companies


 Q6. 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 Java
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


 Q7. What is the difference between Stack and Queue ?Data Structures
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


 Q8. Given a string and index for the opening bracket, find the index of matching closing bracket. Core Java
 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     Asked in 1 Companies


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


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


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


Very frequently asked. Among first few questions in almost all interviews. Among Top 5 frequently asked questions. Frequently asked in Indian service companies (HCL,TCS,Infosys,Capgemini etc based on multiple feedback ) and Epam Systems
  Q12. Difference between == and .equals() ?Core Java
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality, whereas "==" operator evaluate to see if the object handlers on the left and right are pointing to the same object in memory.

x.equals(y) means the references x and y are holding objects that are equal. x==y means that the references x and y have same object.

Sample code:

String x = new String("str");
String y = new String("str");

System.out.println(x == y); // prints false
System.out.println(x.equals(y)); // prints true

  Sample Code for equals

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

   Like         Discuss         Correct / Improve     java   string comparison   string   object class   ==    equals   object equality  operator   == vs equals   equals vs ==     Asked in 294 Companies      basic        frequent

Try 6 Question(s) Test


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


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


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


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


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


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


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

Ans.a. Stack


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: