Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element onto the stack.
DSA Java
stack.[1](10);
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push will remove an element instead of adding.
Using peek does not add an element, it only shows the top element.
✗ Incorrect
The push method adds an element to the top of the stack.
2fill in blank
mediumComplete the code to remove the top element from the stack.
DSA Java
int top = stack.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using push will add an element instead of removing.
Using peek only shows the top element without removing it.
✗ Incorrect
The pop method removes and returns the top element of the stack.
3fill in blank
hardFix the error in the code to check the top element without removing it.
DSA Java
int top = stack.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes the element, which is not desired here.
Using push or remove are incorrect for this purpose.
✗ Incorrect
The peek method returns the top element without removing it.
4fill in blank
hardFill both blanks to create a stack and add an element 5.
DSA Java
Stack<Integer> stack = new [1]<>(); stack.[2](5);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Queue instead of Stack for the object.
Using pop instead of push to add an element.
✗ Incorrect
We create a new Stack object and use push to add 5.
5fill in blank
hardFill all three blanks to check if the stack is empty and print a message.
DSA Java
if (stack.[1]()) { System.out.println("Stack is [2]."); } else { System.out.println("Stack is [3]."); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'empty()' method which does not exist in Java Stack.
Swapping the printed messages.
✗ Incorrect
The isEmpty() method checks if the stack has no elements. We print "empty" if true, otherwise "not empty".
