It shows the front element of the queue without removing it, so you can see who is next without changing the queue.
Click to reveal answer
beginner
In a queue, which end is the 'front'?
The front is the end where elements are removed (dequeued). It's like the front of a line where the first person is served.
Click to reveal answer
intermediate
What happens if you peek an empty queue?
You get a special value (like null or exception) because there is no front element to show.
Click to reveal answer
beginner
Why is peek useful compared to dequeue?
Peek lets you check the next item without removing it, so the queue stays the same. Dequeue removes the item.
Click to reveal answer
intermediate
In Java's Queue interface, which method is used to peek the front element safely?
The method 'peek()' returns the front element or null if the queue is empty, without removing it.
Click to reveal answer
What does the peek() method do in a queue?
ARemoves and returns the front element
BAdds an element to the back
CReturns the front element without removing it
DChecks if the queue is empty
✗ Incorrect
peek() shows the front element without removing it.
If a queue is empty, what does peek() return in Java?
AReturns null
BReturns zero
CThrows an exception
DReturns the last element
✗ Incorrect
peek() returns null if the queue is empty.
Which end of the queue does peek() look at?
ARandom
BMiddle
CBack
DFront
✗ Incorrect
peek() shows the front element of the queue.
What is the difference between peek() and poll() in a queue?
Apeek() removes element, poll() does not
Bpeek() shows front without removing, poll() removes front
CBoth do the same
Dpeek() adds element, poll() removes element
✗ Incorrect
peek() shows front without removing; poll() removes and returns front.
Why might you use peek() instead of dequeue?
ATo check the next element without changing the queue
BTo add an element
CTo clear the queue
DTo reverse the queue
✗ Incorrect
peek() lets you see the next element without removing it.
Explain how the peek operation works in a queue and why it is useful.
Think about looking at the first person in line without asking them to leave.
You got /4 concepts.
Describe what happens when you peek an empty queue in Java.
What does peek() return if no one is in line?
You got /4 concepts.
Practice
(1/5)
1. What does the peek() method do in a Java Queue?
easy
A. Returns the front element without removing it
B. Removes and returns the front element
C. Adds an element to the end of the queue
D. Checks if the queue is empty
Solution
Step 1: Understand the purpose of peek()
The peek() method is designed to look at the front element of the queue without changing the queue.
Step 2: Compare with other queue methods
Unlike poll() which removes the front element, peek() only returns it without removal.
Final Answer:
Returns the front element without removing it -> Option A
Quick Check:
peek() returns front element without removal [OK]
Hint: peek() shows front without removing [OK]
Common Mistakes:
Confusing peek() with poll() which removes element
Thinking peek() adds elements
Assuming peek() checks if queue is empty
2. Which of the following is the correct syntax to peek the front element of a Java Queue named q?
easy
A. q.getFirst();
B. q.front();
C. q.peek();
D. q.pop();
Solution
Step 1: Recall Java Queue methods
The standard method to look at the front element without removing it is peek().
Step 2: Check other options
front() and pop() are not methods of Java Queue interface; getFirst() belongs to Deque, not Queue.
Final Answer:
q.peek(); -> Option C
Quick Check:
Use peek() to view front element [OK]
Hint: Use q.peek() to see front element [OK]
Common Mistakes:
Using pop() which is not in Queue interface
Using front() which doesn't exist in Java Queue
Confusing getFirst() with peek()
3. What will be the output of the following Java code?
import java.util.*;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(10);
q.add(20);
q.add(30);
System.out.println(q.peek());
q.poll();
System.out.println(q.peek());
}
}
medium
A. 10\n10
B. 30\n20
C. 20\n30
D. 10\n20
Solution
Step 1: Add elements to queue
Elements 10, 20, 30 are added in order. Queue is now [10, 20, 30].
Step 2: Peek and poll operations
First peek() returns 10 (front). Then poll() removes 10. Next peek() returns 20 (new front).
Final Answer:
10\n20 -> Option D
Quick Check:
peek() before poll = 10, after poll = 20 [OK]
Hint: peek() shows front; poll() removes front [OK]
Common Mistakes:
Assuming peek() removes element
Confusing poll() with peek()
Expecting peek() to return last element
4. Identify the error in the following code snippet that tries to peek the front element of a queue:
import java.util.*;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(5);
q.add(15);
int front = q.peek();
System.out.println(front);
}
}
medium
A. peek() returns Integer, cannot assign to int without unboxing
B. No error, code runs fine and prints 5
C. Queue interface does not have peek() method
D. Must cast peek() result to int explicitly
Solution
Step 1: Check peek() return type
peek() returns an Integer object, which Java auto-unboxes to int automatically.
Step 2: Verify code correctness
Code compiles and runs fine, printing 5 as expected.
Final Answer:
No error, code runs fine and prints 5 -> Option B
Quick Check:
Auto-unboxing allows Integer to int assignment [OK]
Hint: Java auto-unboxes Integer to int [OK]
Common Mistakes:
Thinking explicit cast needed from Integer to int
Assuming peek() returns primitive int
Believing Queue lacks peek() method
5. Given a queue q containing integers, write a Java method peekOrDefault that returns the front element if the queue is not empty, or returns -1 if it is empty. Which of the following implementations is correct?
hard
A. public int peekOrDefault(Queue<Integer> q) { return q.isEmpty() ? -1 : q.peek(); }
B. public int peekOrDefault(Queue<Integer> q) { return q.peek() == null ? -1 : q.poll(); }
C. public int peekOrDefault(Queue<Integer> q) { return q.poll() == null ? -1 : q.peek(); }
D. public int peekOrDefault(Queue<Integer> q) { if(q.peek() == null) return -1; else return q.poll(); }
Solution
Step 1: Check for empty queue safely
Use q.isEmpty() to check if queue has elements before peeking.
Step 2: Return front or default
If empty, return -1; else return q.peek() which does not remove element.
Step 3: Analyze other options
public int peekOrDefault(Queue<Integer> q) { return q.peek() == null ? -1 : q.poll(); }: public int peekOrDefault(Queue<Integer> q) { return q.peek() == null ? -1 : q.poll(); } peeks to check but polls to return, removing the front element—wrong. public int peekOrDefault(Queue<Integer> q) { return q.poll() == null ? -1 : q.peek(); }: public int peekOrDefault(Queue<Integer> q) { return q.poll() == null ? -1 : q.peek(); } polls first; if not empty, removes front then peeks new front—wrong. public int peekOrDefault(Queue<Integer> q) { if(q.peek() == null) return -1; else return q.poll(); }: public int peekOrDefault(Queue<Integer> q) { if(q.peek() == null) return -1; else return q.poll(); } returns poll() in else, removing element—wrong.
Final Answer:
public int peekOrDefault(Queue<Integer> q) { return q.isEmpty() ? -1 : q.peek(); } -> Option A
Quick Check:
Check empty first, then peek without removal [OK]
Hint: Check isEmpty() before peek() to avoid null [OK]
Common Mistakes:
Using poll() which removes element
Peeking to check null but then polling which removes
Not handling empty queue before peeking
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.