Practice 70 Java Stack Programming Questions
Q1. Write a Java program to implement a stack using arrays.
- Input: Push(10), Push(20), Pop()
- Expected Output: 20
Q2. Write a Java program to implement a stack using linked list.
- Input: Push(10), Push(20), Push(30), Pop()
- Expected Output: 30
Q3. Write a Java program to check if a given string has balanced parentheses using a stack.
- Input: "(a+b)*(c+d)"
- Expected Output: True
Q4. Write a Java program to reverse a string using a stack.
- Input: "hello"
- Expected Output: "olleh"
Q5. Write a Java program to evaluate a postfix expression using a stack.
- Input: "5 6 + 7 *"
- Expected Output: 77
Q6. Write a Java program to evaluate a prefix expression using a stack.
- Input: "* + 5 6 7"
- Expected Output: 77
Q7. Write a Java program to implement a stack using Java's `Stack` class.
- Input: Push(10), Push(20), Push(30), Pop()
- Expected Output: 30
Q8. Write a Java program to check if a given expression has balanced brackets using a stack.
- Input: "{[()()]}"
- Expected Output: True
Q9. Write a Java program to implement a stack and perform push, pop, and peek operations.
- Input: Push(10), Push(20), Peek(), Pop()
- Expected Output: 20, 10
Q10. Write a Java program to implement a stack and find the minimum element in the stack.
- Input: Push(10), Push(5), Push(20)
- Expected Output: 5
Q11. Write a Java program to check if a given number is a palindrome using a stack.
- Input: "12321"
- Expected Output: True
Q12. Write a Java program to convert an infix expression to postfix using a stack.
- Input: "(a+b)*c"
- Expected Output: "ab+c*"
Q13. Write a Java program to convert an infix expression to prefix using a stack.
- Input: "(a+b)*c"
- Expected Output: "*+abc"
Q14. Write a Java program to implement a stack that supports min() in O(1) time.
- Input: Push(10), Push(5), Push(20)
- Expected Output: 5
Q15. Write a Java program to implement a stack and check if an array is a stack permutation of another array.
- Input: [1, 2, 3], [2, 1, 3]
- Expected Output: True
Q16. Write a Java program to reverse the elements of a stack.
- Input: Push(1), Push(2), Push(3), Pop()
- Expected Output: 1, 2, 3
Q17. Write a Java program to sort a stack using recursion.
- Input: Push(3), Push(1), Push(2)
- Expected Output: 1, 2, 3
Q18. Write a Java program to implement a stack using two queues.
- Input: Push(10), Push(20), Pop()
- Expected Output: 20
Q19. Write a Java program to check if a sequence of stack operations is valid.
- Input: Push(1), Push(2), Pop(), Pop()
- Expected Output: True
Q20. Write a Java program to implement a stack and find the maximum element in the stack.
- Input: Push(10), Push(20), Push(5)
- Expected Output: 20
Q21. Write a Java program to implement a stack using two stacks.
- Input: Push(10), Push(20), Pop()
- Expected Output: 20
Q22. Write a Java program to count the number of elements in a stack.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 3
Q23. Write a Java program to implement a stack using an array and find the middle element.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 20
Q24. Write a Java program to implement a stack and check if it is empty.
- Input: Push(10), Pop()
- Expected Output: True
Q25. Write a Java program to implement a stack and pop the first element.
- Input: Push(10), Push(20), Pop()
- Expected Output: 20
Q26. Write a Java program to implement a stack and find the top element.
- Input: Push(10), Push(20), Peek()
- Expected Output: 20
Q27. Write a Java program to implement a stack and perform operations of push, pop, and peek.
- Input: Push(5), Pop(), Peek()
- Expected Output: None, 5
Q28. Write a Java program to implement a stack that supports push, pop, peek, and size operations.
- Input: Push(10), Push(20), Size()
- Expected Output: 2
Q29. Write a Java program to implement a stack that supports push, pop, peek, size, and min operations.
- Input: Push(10), Push(20), Min()
- Expected Output: 10
Q30. Write a Java program to implement a stack and print elements in reverse order.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 30, 20, 10
Q31. Write a Java program to reverse the words in a sentence using a stack.
- Input: "Hello World"
- Expected Output: "World Hello"
Q32. Write a Java program to check if parentheses in an expression are balanced using a stack.
- Input: "[{()}]"
- Expected Output: True
Q33. Write a Java program to reverse a stack using recursion.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 30, 20, 10
Q34. Write a Java program to implement a stack and push an element at the bottom.
- Input: Push(10), Push(20), BottomPush(5)
- Expected Output: 5, 10, 20
Q35. Write a Java program to implement a stack and pop an element from the bottom.
- Input: Push(10), Push(20), BottomPop()
- Expected Output: 10
Q36. Write a Java program to implement a stack and perform push, pop, and display operations.
- Input: Push(5), Pop(), Display()
- Expected Output: Empty Stack
Q37. Write a Java program to find the sum of elements in a stack.
- Input: Push(5), Push(10), Push(15)
- Expected Output: 30
Q38. Write a Java program to find the product of elements in a stack.
- Input: Push(5), Push(10), Push(2)
- Expected Output: 100
Q39. Write a Java program to find the second largest element in a stack.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 20
Q40. Write a Java program to implement a stack that can hold only even numbers.
- Input: Push(2), Push(4), Push(3)
- Expected Output: 2, 4
Q41. Write a Java program to implement a stack and perform push and pop operations using user input.
- Input: Push(10), Pop()
- Expected Output: 10
Q42. Write a Java program to implement a stack and perform push and pop operations using an integer array.
- Input: Push(5), Push(10), Pop()
- Expected Output: 10
Q43. Write a Java program to implement a stack and find the sum of the first n elements.
- Input: Push(10), Push(20), Push(30), Sum()
- Expected Output: 60
Q44. Write a Java program to implement a stack and print the elements from top to bottom.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 30, 20, 10
Q45. Write a Java program to implement a stack and reverse the stack in-place.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 30, 20, 10
Q46. Write a Java program to implement a stack and display the stack after each operation.
- Input: Push(10), Push(20), Pop()
- Expected Output: 10, 20, 10
Q47. Write a Java program to implement a stack and remove all elements.
- Input: Push(10), Push(20), Clear()
- Expected Output: Empty Stack
Q48. Write a Java program to check if a stack is full.
- Input: Push(10), Push(20), Push(30)
- Expected Output: Full
Q49. Write a Java program to implement a stack with the ability to get the current size.
- Input: Push(10), Push(20), Size()
- Expected Output: 2
Q50. Write a Java program to implement a stack that supports the operation to get the top element.
- Input: Push(10), Push(20), Top()
- Expected Output: 20
Q51. Write a Java program to implement a stack and find the depth of a stack.
- Input: Push(10), Push(20), Depth()
- Expected Output: 2
Q52. Write a Java program to implement a stack and check if it is full or empty.
- Input: Push(10), Push(20), Full()
- Expected Output: False
Q53. Write a Java program to implement a stack and calculate the sum of elements at even positions.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 40
Q54. Write a Java program to implement a stack and calculate the sum of elements at odd positions.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 30
Q55. Write a Java program to implement a stack and find the frequency of each element.
- Input: Push(10), Push(10), Push(20)
- Expected Output: 10 -> 2, 20 -> 1
Q56. Write a Java program to implement a stack and calculate the product of elements at odd positions.
- Input: Push(5), Push(10), Push(15)
- Expected Output: 75
Q57. Write a Java program to implement a stack and perform multiple push and pop operations.
- Input: Push(10), Pop(), Push(20), Push(30), Pop()
- Expected Output: 10, 30
Q58. Write a Java program to implement a stack and check if the stack is a palindrome.
- Input: Push(1), Push(2), Push(1)
- Expected Output: True
Q59. Write a Java program to implement a stack and check if the stack is sorted.
- Input: Push(1), Push(2), Push(3)
- Expected Output: Sorted
Q60. Write a Java program to implement a stack and push elements in reverse order.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 30, 20, 10
Q61. Write a Java program to implement a stack and find the average of all elements.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 20
Q62. Write a Java program to implement a stack and perform pop until the stack is empty.
- Input: Push(10), Push(20), Pop(), Pop()
- Expected Output: 20, 10
Q63. Write a Java program to implement a stack and check if an element exists in the stack.
- Input: Push(10), Push(20), Contains(20)
- Expected Output: True
Q64. Write a Java program to implement a stack and swap the top two elements.
- Input: Push(10), Push(20), Swap()
- Expected Output: 20, 10
Q65. Write a Java program to implement a stack and push elements alternately.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 10, 20, 30
Q66. Write a Java program to implement a stack and pop elements alternately.
- Input: Push(10), Push(20), Pop(), Pop()
- Expected Output: 20, 10
Q67. Write a Java program to implement a stack and print elements using recursion.
- Input: Push(1), Push(2), Push(3)
- Expected Output: 3, 2, 1
Q68. Write a Java program to implement a stack and check if the stack is empty.
- Input: Push(10), Pop(), IsEmpty()
- Expected Output: True
Q69. Write a Java program to implement a stack and count the occurrences of a specific element.
- Input: Push(10), Push(20), Push(10), Count(10)
- Expected Output: 2
Q70. Write a Java program to implement a stack and find the median element.
- Input: Push(10), Push(20), Push(30)
- Expected Output: 20
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!