← Quiz: Stack and Queue in C: all 10 questions
Quiz: Stack and Queue in C · Question 7 of 10
What does this print? (code)
What does this print?
#include <stdio.h>
#define MAX 3
int q[MAX], front = 0, rear = -1, count = 0;
void enq(int x) { if (count < MAX) { rear = (rear + 1) % MAX; q[rear] = x; count++; } }
int deq(void) { int v = q[front]; front = (front + 1) % MAX; count--; return v; }
int main(void) {
enq(1); enq(2); enq(3);
deq();
enq(4);
printf("%d %d", rear, deq());
return 0;
}
- A.3 2
- B.0 1
- C.0 2
- D.2 2
Answer & explanation
Correct answer: 0 2
After 1, 2, 3 rear is 2. Dequeue removes 1; enqueue 4 wraps rear to 0. The next dequeue returns 2.
Can you get the rest right without peeking?
Take the timed Quiz: Stack and Queue in C quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →