Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element to the top of a stack.
DSA Java
Stack<Integer> stack = new Stack<>(); stack.[1](10);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' which works for lists but is not the standard stack method.
Using 'offer' or 'enqueue' which are queue methods.
✗ Incorrect
The push method adds an element to the top of the stack.
2fill in blank
mediumComplete the code to remove an element from the front of a queue.
DSA Java
Queue<String> queue = new LinkedList<>(); queue.add("apple"); String item = queue.[1]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' which is for stacks, not queues.
Using 'peek' which only views the element without removing.
✗ Incorrect
The poll method removes and returns the front element of the queue.
3fill in blank
hardFix the error in the code to check if a stack is empty before popping.
DSA Java
Stack<Double> stack = new Stack<>(); if (!stack.[1]()) { stack.pop(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'empty()' which is a legacy method and less common.
Using 'size()' which returns a number, not a boolean.
✗ Incorrect
The isEmpty() method checks if the stack has no elements before popping.
4fill in blank
hardFill both blanks to create a queue and add an element to it.
DSA Java
Queue<String> queue = new [1]<>(); queue.[2]("banana");
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Stack' instead of 'LinkedList' for a queue.
Using 'push' which is for stacks, not queues.
✗ Incorrect
Use LinkedList to create a queue and add to insert an element.
5fill in blank
hardFill all three blanks to create a stack, push an element, and then pop it.
DSA Java
Stack<Integer> stack = new [1]<>(); stack.[2](5); int value = stack.[3]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'LinkedList' to create a stack instead of 'Stack'.
Using 'pop' before 'push' causing empty stack error.
✗ Incorrect
Create a stack with Stack, add with push, and remove with pop.
