Data Structure Questions and Answers for Campus interviews on “Decimal to Binary Conversion using Recursion”.
1. Which of the following is the binary representation of 100? Answer: c 2. Consider the following iterative code used to convert a decimal number to its equivalent binary: Which of the following lines should be inserted to complete the above code? Answer: b 3. What is the output of the following code? a) 111111 Answer: a 4. What is the output of the following code? a) 0 Answer: a 5. What is the time complexity of the following code used to convert a decimal number to its binary equivalent? a) O(1) Answer: d 6. Consider the following recursive implementation used to convert a decimal number to its binary equivalent: Which of the following lines should be inserted to complete the above code? Answer: c 7. Consider the following code: Which of the following lines is the base case for the above code? Answer: c 8. What is the output of the following code? a) -1100100 Answer: d 9. What is the time complexity of the recursive implementation used to convert a decimal number to its binary equivalent? a) O(1) Answer: d 10. What is the space complexity of the recursive implementation used to convert a decimal number to its binary equivalent? a) O(1) Answer: d 11. What is the output of the following code? a) 1110111 Answer: c 12. How many times is the function recursive_dec_to_bin() called when the following code is executed? a) 7 Answer: b
a) 1010010
b) 1110000
c) 1100100
d) 1010101
Clarification: 100 = 64 + 32 + 4 = 26 + 25 + 22 = 1100100.#include
a) n–
b) n /= 2
c) n /= 10
d) n++
Clarification: The line “n /= 2” should be inserted to complete the above code.#include
b) 111011
c) 101101
d) 101010
Clarification: The program prints the binary equivalent of 63, which is 111111.#include
b) 1
c) Runtime error
d) Garbage value
Clarification: The program prints the binary equivalent of 0, which is 0.#include
b) O(n)
c) O(n2)
d) O(logn)
Clarification: The time complexity of the above code used to convert a decimal number to its binary equivalent is O(logn).#include
a) arr[len] = n
b) arr[len] = n % 2
c) arr[len++] = n % 2
d) arr[len++] = n
Clarification: The line “arr[len++] = n % 2” should be inserted to complete the above code.#include
a) if(n ==0 && len == 0)
b) if(n == 0)
c) if(n ==0 && len == 0) and if(n == 0)
d) if(n == 1)
Clarification: Both of the above mentioned lines are the base cases for the above code.#include
b) 1100100
c) 2’s complement of 1100100
d) Garbage value
Clarification: The program doesn’t handle negative inputs and so produces a garbage value.#include
b) O(n)
c) O(n2)
d) O(logn)
Clarification: The time complexity of the recursive implementation used to convert a decimal number to its binary equivalent is O(logn).#include
b) O(n)
c) O(n2)
d) O(logn)
Clarification: The space complexity of the recursive implementation used to convert a decimal number to its binary equivalent is O(logn).#include
b) 1001111
c) 1101111
d) 1010111
Clarification: The program prints the binary equivalent of 111, which is 1101111.#include
b) 8
c) 9
d) 10
Clarification: The function recursive_dec_to_bin() is called 8 times when the above code is executed.