## MCQs

Total Questions : 10
Question 1. Which is the predefined method available in Java to convert decimal to binary numbers?
1.    toBinaryInteger(int)
2.    toBinaryValue(int)
3.    toBinaryNumber(int)
4.    toBinaryString(int)

The method toBinaryString() takes an integer argument and is defined in java.lang package. Usage is java.lang.Integer.toBinaryString(int) this returns the string representation of the unsigned integer value.

Question 2. Write a piece of code which returns true if the string contains balanced parenthesis, false otherwise.
1.    public boolean isBalanced(String exp){
2.    public boolean isBalanced(String exp){
3.    public boolean isBalanced(String exp){
4.    public boolean isBalanced(String exp){
Answer: Option A. -> public boolean isBalanced(String exp){

Whenever a '(' is encountered, push it into the stack, and when a ')' is encountered check the top of the stack to see if there is a matching '(', if not return false, continue this till the entire string is processed and then return true.

Question 3. What is the time complexity of the above code?
1.    O(logn)
2.    O(n)
3.    O(1)
4.    O(nlogn)

All the characters in the string have to be processed, hence the complexity is O(n).

Question 4. What is the time complexity for converting decimal to binary numbers?
1.    O(1)
2.    O(n)
3.    O(logn)
4.    O(nlogn)

Since each time you are halving the number, it can be related to that of a binary search algorithm, hence the complexity is O(logn).

Question 5. Using stacks, how to obtain the binary representation of the number?
1.    public void convertBinary(int num){    Stack stack = new Stack();    while (num != 0)    {        int digit = num / 2;stack.push(digit);    }     System.out.print("\nBinary representation is:");    while (!(stack.isEmpty() ))    {        System.out.print(stack.pop());    } }
2.    public void convertBinary(int num){    Stack stack = new Stack();    while (num != 0)    {        int digit = num % 2;        stack.push(digit);    }     System.out.print("\nBinary representation is:");    while (!(stack.isEmpty() ))    {        System.out.print(stack.pop());    } }
3.    public void convertBinary(int num){    Stack stack = new Stack();    while (num != 0)    {        int digit = num % 2;        stack.push(digit);        num = num / 2;    }     System.out.print("\nBinary representation is:");    while (!(stack.isEmpty() ))    {        System.out.print(stack.pop());    } }
4.    None of the above
Answer: Option C. -> public void convertBinary(int num){    Stack stack = new Stack();    while (num != 0)    {        int digit = num % 2;        stack.push(digit);        num = num / 2;    }     System.out.print("\nBinary representation is:");    while (!(stack.isEmpty() ))    {        System.out.print(stack.pop());    } }

Here instead of adding the digits to an array, you push it into a stack and while printing, pop it from the stack.

Question 6. Which data structure can be used to test a palindrome?
1.    Tree
2.    Heap
3.    Stack
4.    Priority queue

Stack is a convenient option as it involves pushing and popping of characters.

Question 7. What is the number of moves required in the Tower of Hanoi problem for k disks?
1.    2k "“ 1
2.    2k + 1
3.    2k + 1
4.    2k "“ 1
Answer: Option A. -> 2k "“ 1

Tracing of the moves in the above ToH problem will prove this result, instead you can simply add a count for each recursive call to check the number of moves.

Question 8. Select the appropriate code which reverses a word.
1.    public String reverse(String input){
2.    public String reverse(String input){
3.    public String reverse(String input){
4.    public String reverse(String input){
Answer: Option B. -> public String reverse(String input){

Although, it is possible to reverse the string without using stack, it is done by looping through the string from the end character by character.
In Java, it is also possible to use the StringBuilder and StringBuffer classes which have a built-in method 'reverse'.
Note its similarity to PalindromeTest

Question 9. Select the appropriate code which tests for a palindrome.
1.    public static void main(String[] args) {
2.    public static void main(String[] args) {
3.    public static void main(String[] args) {
4.    None of the mentioned
Answer: Option A. -> public static void main(String[] args) {

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.

Question 10. Which among the following is not a palindrome?