Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an empty stack of integers.
DSA C#
Stack<int> stack = new Stack<int>[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] instead of parentheses.
Using curly braces {} which are for collection initializers.
Using angle brackets <> which define generic types.
✗ Incorrect
In C#, to create a new instance of a Stack, you use parentheses ().
2fill in blank
mediumComplete the code to add the number 10 to the stack.
DSA C#
stack.[1](10);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Add which is for lists, not stacks.
Using Insert which is for lists at specific positions.
Using Enqueue which is for queues, not stacks.
✗ Incorrect
The Push method adds an item to the top of the stack following LIFO.
3fill in blank
hardFix the error in the code to remove the top item from the stack.
DSA C#
int top = stack.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using Remove or Delete which are not stack methods.
Using Dequeue which is for queues.
✗ Incorrect
Pop removes and returns the top item from the stack following LIFO.
4fill in blank
hardFill both blanks to check if the stack is empty and print a message.
DSA C#
if (stack.[1] == [2]) { Console.WriteLine("Stack is empty"); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length which is not a Stack property.
Comparing to null which is not valid for Count.
✗ Incorrect
Stack.Count gives the number of items; if it equals 0, the stack is empty.
5fill in blank
hardFill all three blanks to create a stack, add 5, and then remove the top item.
DSA C#
Stack<int> stack = new Stack<int>[1]; stack.[2](5); int removed = stack.[3]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using Add instead of Push to add items.
Using Add or Dequeue instead of Pop to remove items.
✗ Incorrect
Create stack with (), add item with Push, remove top with Pop.
