Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to peek the front element of the queue.
DSA C#
Queue<int> queue = new Queue<int>(); queue.Enqueue(10); queue.Enqueue(20); int front = queue.[1](); Console.WriteLine(front);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop() which is not a method of Queue in C#.
Using Front or Top which are not valid Queue methods.
✗ Incorrect
The Peek() method returns the front element of the queue without removing it.
2fill in blank
mediumComplete the code to check if the queue is not empty before peeking.
DSA C#
Queue<string> queue = new Queue<string>(); queue.Enqueue("apple"); if (queue.[1] > 0) { string front = queue.Peek(); Console.WriteLine(front); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length which is not a property of Queue.
Using Size or Capacity which are not correct for this check.
✗ Incorrect
The Count property gives the number of elements in the queue.
3fill in blank
hardFix the error in the code to correctly peek the front element of the queue.
DSA C#
Queue<double> queue = new Queue<double>(); queue.Enqueue(3.14); double front = queue.[1](); Console.WriteLine(front);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Peek without parentheses.
Using Front which is not a Queue method.
✗ Incorrect
Peek is a method and must be called with parentheses: Peek()
4fill in blank
hardFill both blanks to safely peek the front element only if the queue is not empty.
DSA C#
Queue<char> queue = new Queue<char>(); queue.Enqueue('A'); if (queue.[1] > 0) { char front = queue.[2](); Console.WriteLine(front); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length instead of Count.
Using Pop() which removes the element.
✗ Incorrect
Use Count to check if queue has elements and Peek() to get the front element.
5fill in blank
hardFill all three blanks to create a queue, add elements, and peek the front element safely.
DSA C#
Queue<string> queue = new Queue<string>(); queue.[1]("first"); queue.[2]("second"); if (queue.[3] > 0) { Console.WriteLine(queue.Peek()); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Add or Push which are not Queue methods in C#.
Using Length instead of Count.
✗ Incorrect
Use Enqueue to add elements and Count to check if queue is not empty before peeking.
