4. Identify the error in the following code snippet that tries to enqueue an element into a queue:
Queue<int> q = new Queue<int>();
q.Enqueue(5);
q.Enqueue(10);
q.Enqueue(15);
q.Enqueue();
medium
A. Cannot enqueue integer values
B. Queue is not initialized
C. Enqueue method does not exist
D. Missing argument in the last Enqueue call
Solution
Step 1: Check method calls
The last call q.Enqueue(); has no argument, but Enqueue requires one.
Step 2: Confirm method signature
Enqueue needs a value to add, so missing argument causes error.
Final Answer:
Missing argument in the last Enqueue call -> Option D
Quick Check:
Enqueue requires one argument [OK]
Hint: Enqueue always needs one argument to add [OK]
Common Mistakes:
Calling Enqueue without argument
Assuming Enqueue can be empty
Confusing initialization errors
5. You have a queue q with elements [1, 2, 3]. You want to enqueue elements 4 and 5, but only if the queue size is less than 5. Which code snippet correctly performs this conditional enqueue operation in C#?
hard
A. if (q.Count == 5) { q.Enqueue(4); q.Enqueue(5); }
B. if (q.Count <= 5) { q.Enqueue(4); q.Enqueue(5); }
C. if (q.Count < 5) { q.Enqueue(4); q.Enqueue(5); }
D. if (q.Count > 5) { q.Enqueue(4); q.Enqueue(5); }
Solution
Step 1: Understand queue size condition
We want to add only if size is less than 5, so condition is q.Count < 5.
Step 2: Apply enqueue calls inside condition
If condition is true, enqueue 4 and 5 to add them safely.
Final Answer:
if (q.Count < 5) { q.Enqueue(4); q.Enqueue(5); } -> Option C
Quick Check:
Use < 5 to check size before enqueue [OK]
Hint: Check queue size with Count before enqueueing [OK]
Common Mistakes:
Using <= instead of < causing overflow
Using > or == which are wrong conditions
Not checking size before enqueue
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.