# Data Structure Questions and Answers-Towers of Hanoi

## Data Structure Questions and Answers-Towers of Hanoi

 Question 1 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Which data structure can be used suitably to solve the Tower of Hanoi problem?
 A Tree B Heap C Priority queue D Stack
Question 1 Explanation:
The Tower of Hanoi involves moving of disks 'stacked' at one peg to another peg with respect to the size constraint, it is conveniently done using stacks,

although it is also possible using priority queues. Since stack approach is widely used, the more suitable option would be 'd' stack.

 Question 2 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Select the appropriate code for the recursive Tower of Hanoi problem.(n is the number of disks)
 A public void solve(int n, String start, String auxiliary, String end) { if (n == 1) { System.out.println(start + " -> " + end); B public void solve(int n, String start, String auxiliary, String end) { if (n == 1) { System.out.println(start + " -> " + end); C public void solve(int n, String start, String auxiliary, String end) { if (n == 1) { System.out.println(start + " -> " + end); D None of the mentioned
Question 2 Explanation:
First transfer all the diska to the auxiliary and then to the end peg, this is achieved by making auxiliary peg as the end peg in the first recursive call, in the second recursive call, the auxiliary becomes the start peg from where the disks are transferred to the end peg.

 Question 3 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Which among the following is not a palindrome?
Question 3 Explanation:
By definition, a palindrome is a string which is the same forward and backward, here, option d doesn't adhere to this definition.

 Question 4 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Which data structure can be used to test a palindrome?
 A Tree B Heap C Stack D Priority queue
Question 4 Explanation:
Stack is a convenient option as it involves pushing and popping of characters.

 Question 5 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Select the appropriate code which tests for a palindrome.
 A public static void main(String[] args) { System.out.print("Enter any string:"); Scanner in=new Scanner(System.in); String input < B public static void main(String[] args) { System.out.print("Enter any string:"); Scanner in=new Scanner(System.in); String input C public static void main(String[] args) { System.out.print("Enter any string:"); Scanner in=new Scanner(System.in); String input D None of the mentioned
Question 5 Explanation:
Push all the characters in the input string to a stack, now pop them and append to a new string which is checked for equality with the original string.

There are 5 questions to complete.