Data Structures | Queue | Question 11
Consider the following pseudo code. Assume that IntQueue is an integer queue. What does the function fun do?
C
void fun( int n)
{
IntQueue q = new IntQueue();
q.enqueue(0);
q.enqueue(1);
for ( int i = 0; i < n; i++)
{
int a = q.dequeue();
int b = q.dequeue();
q.enqueue(b);
q.enqueue(a + b);
print(a);
}
}
|
(A)
Prints numbers from 0 to n-1
(B)
Prints numbers from n-1 to 0
(C)
Prints first n Fibonacci numbers
(D)
Prints first n Fibonacci numbers in reverse order.
Answer: (C)
Explanation:
The function prints first n Fibonacci Numbers. Note that 0 and 1 are initially there in q. In every iteration of the loop sum of the two queue items is enqueued and the front item is dequeued.
Hence option (C) is the correct answer.
Quiz of this Question
Please comment below if you find anything wrong in the above post
Last Updated :
28 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...