In: Computer Science
How many moves are required to solve the Towers of Hanoi problem when we have 3 disks?
Group of answer choices
5
6
8
4
7
After the following statements execute, what item is at the front of the queue?
QueueInterface<String> zooDelivery = new
LinkedQueue<>();
zooDelivery.enqueue("lion");
zooDelivery.enqueue("tiger");
zooDelivery.enqueue("cheetah");
String next = zooDelivery.dequeue();
next = zooDelivery.dequeue();
zooDelivery.enqueue("jaguar");
zooDelivery.enqueue("cat");
Group of answer choices
"lion"
"tiger"
"cheetah"
"jaguar"
"cat"
When a recursive method does not include a base case, calling
the method results in
Group of answer choices
A. iterative recursion
B. stack overflow
C. infinite recursion
D. A and B
E. B and C
In StackInterface, the method pop()
Group of answer choices
removes and returns the item that was pushed last on the stack
returns the item that was pushed last on the stack but does not remove it
removes and returns the item that was pushed first on the stack
returns the item that was pushed first on the stack but does not remove it
None of the above
Question 1.
If we have 3 disks then the number of moves required to solve the Tower of Hanoi is 7.
Lets assume we have 3 rods: A, B, C and 3 disks marked 1, 2, 3 respectively.
Then, below are the step by step manipulation to solve Tower of Hanoi
Move disk 1 from rod A to rod B // step 1 Move disk 2 from rod A to rod C // step 2 Move disk 1 from rod B to rod C // step 3 Move disk 3 from rod A to rod B // step 4 Move disk 1 from rod C to rod A // step 5 Move disk 2 from rod C to rod B // step 6 Move disk 1 from rod A to rod B // step 7
Question 2.
Lion is the correct answer.
Now lets, line by line see what happened in the java code.
enqueue: adds an element to the end of linked list
dequeue: removes the most recently added element on the linked list
lion - > Null //zooDelivery.enqueue("lion");
lion -> tiger -> Null //zooDelivery.enqueue("tiger");
lion -> tiger -> cheetah - > Null //zooDelivery.enqueue("cheetah");
lion -> tiger -> Null //zooDelivery.dequeue();
lion - > Null //zooDelivery.dequeue();
lion -> jaguar -> Null //zooDelivery.enqueue("jaguar");
lion -> tiger -> cheetah - > Null //zooDelivery.enqueue("cat");
but we can see the front of the queue doesn't get affected from the above code and it still remains as lion.
Question 3.
Option E. B and C, is the correct answer
When a recursive call is made without a base case the code doesn't stop and infinite recursion happens until the heap memory is full and then stack overflow error occurs.
Question 4.
Option 1. removes and returns the item that was pushed last on the stack