We have the best collection of the evaluation of a Towers of Hanoi MCQs and answer with FREE PDF. These Towers of Hanoi MCQs will help you to prepare for any competitive exams like: BCA, MCA, GATE, GRE, IES, PSC, UGC NET, DOEACC Exams at all levels – you just have to practice regularly.
Towers of Hanoi MCQs
1. The optimal data structure used to solve Tower of Hanoi is _________
a) Tree
b) Heap
c) Priority queue
d) Stack
Answer: Stack
2. 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);
}
else
{
solve(n - 1, start, end, auxiliary);
System.out.println(start + " -> " + end);
solve(n - 1, auxiliary, start, end);
}
}
b)
public void solve(int n, String start, String auxiliary, String end)
{
if (n == 1)
{
System.out.println(start + " -> " + end);
}
else
{
solve(n - 1, auxiliary, start, end);
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);
}
else
{
System.out.println(start + " -> " + end);
solve(n - 1, auxiliary, start, end);
}
}
d)
public void solve(int n, String start, String auxiliary, String end)
{
if (n == 1)
{
System.out.println(start + " -> " + end);
}
else
{
solve(n - 1, start, end, auxiliary);
System.out.println(start + " -> " + end);
}
}
Answer:
public void solve(int n, String start, String auxiliary, String end)
{
if (n == 1)
{
System.out.println(start + " -> " + end);
}
else
{
solve(n - 1, start, end, auxiliary);
System.out.println(start + " -> " + end);
solve(n - 1, auxiliary, start, end);
}
}
3. Which among the following is not a palindrome?
a) Madam
b) Dad
c) Malayalam
d) Maadam
Answer: Maadam
4. Which data structure can be used to test a palindrome?
a) Tree
b) Heap
c) Stack
d) Priority queue
Answer: Stack
5. 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 = in.nextLine();
Stack<Character> stk = new Stack<Character>();
for (int i = 0; i < input.length(); i++)
{
stk.push(input.charAt(i));
}
String reverse = "";
while (!stk.isEmpty())
{
reverse = reverse + stk.pop();
}
if (input.equals(reverse))
System.out.println("palindrome");
else
System.out.println("not a palindrome");
}
b)
public static void main(String[] args)
{
System.out.print("Enter any string:");
Scanner in=new Scanner(System.in);
String input = in.nextLine();
Stack<Character> stk = new Stack<Character>();
for (int i = 0; i < input.length(); i++)
{
stk.push(input.charAt(i));
}
String reverse = "";
while (!stk.isEmpty())
{
reverse = reverse + stk.peek();
}
if (input.equals(reverse))
System.out.println("palindrome");
else
System.out.println("not a palindrome");
}
c)
public static void main(String[] args)
{
System.out.print("Enter any string:");
Scanner in=new Scanner(System.in);
String input = in.nextLine();
Stack<Character> stk = new Stack<Character>();
for (int i = 0; i < input.length(); i++)
{
stk.push(input.charAt(i));
}
String reverse = "";
while (!stk.isEmpty())
{
reverse = reverse + stk.pop();
stk.pop();
}
if (input.equals(reverse))
System.out.println("palindrome");
else
System.out.println("not a palindrome");
}
d)
public static void main(String[] args)
{
System.out.print("Enter any string:");
Scanner in=new Scanner(System.in);
String input = in.nextLine();
Stack<Character> stk = new Stack<Character>();
for (int i = 0; i < input.length(); i++)
{
stk.push(input.charAt(i));
}
String reverse = "";
while (!stk.isEmpty())
{
reverse = reverse + stk.pop();
stk.pop();
}
if (!input.equals(reverse))
System.out.println("palindrome");
else
System.out.println("not a palindrome");
}
Answer:
public static void main(String[] args)
{
System.out.print("Enter any string:");
Scanner in=new Scanner(System.in);
String input = in.nextLine();
Stack<Character> stk = new Stack<Character>();
for (int i = 0; i < input.length(); i++)
{
stk.push(input.charAt(i));
}
String reverse = "";
while (!stk.isEmpty())
{
reverse = reverse + stk.pop();
}
if (input.equals(reverse))
System.out.println("palindrome");
else
System.out.println("not a palindrome");
}
6. What is the number of moves required to solve Tower of Hanoi problem for k disks?
a) 2k – 1
b) 2k + 1
c) 2k + 1
d) 2k – 1
Answer: 2k – 1
7. Select the appropriate code which reverses a word.
a)
public String reverse(String input)
{
for (int i = 0; i < input.length(); i++)
{
stk.push(input.charAt(i));
}
String rev = "";
while (!stk.isEmpty())
{
rev = rev + stk.peek();
}
return rev;
}
b)
public String reverse(String input)
{
for (int i = 0; i < input.length(); i++)
{
stk.push(input.charAt(i));
}
String rev = "";
while (!stk.isEmpty())
{
rev = rev + stk.pop();
}
return rev;
}
c)
public String reverse(String input)
{
for (int i = 0; i < input.length(); i++)
{
stk.push(input.charAt(i));
}
String rev = "";
while (!stk.isEmpty())
{
rev = rev + stk.pop();
}
}
d)
public String reverse(String input)
{
for (int i = 0; i < input.length(); i++)
{
stk.push(input.charAt(i));
}
String rev = "";
while (!stk.isEmpty())
{
rev = rev + stk.pop();
stk.pop();
}
return rev;
}
Answer:
public String reverse(String input)
{
for (int i = 0; i < input.length(); i++)
{
stk.push(input.charAt(i));
}
String rev = "";
while (!stk.isEmpty())
{
rev = rev + stk.pop();
}
return rev;
}
Download PDF (Towers of Hanoi MCQs)