Select the correct answerWhat will be the output of the following Python code?l=[]def convert(b): if(b==0): return l dig=b%2 l.append(dig) convert(b//2)convert(6)l.reverse()for i in l: print(i,end="")Options 3110011Infinite loop
Question
Select the correct answerWhat will be the output of the following Python code?l=[]def convert(b): if(b==0): return l dig=b%2 l.append(dig) convert(b//2)convert(6)l.reverse()for i in l: print(i,end="")Options 3110011Infinite loop
Solution
The correct answer is 110.
Here's the step by step explanation:
-
The function
convert(b)is defined to convert a decimal number into binary. -
The base case of the recursion is when
bequals 0, in which case the function returns the listl. -
If
bis not 0, the function calculates the remainder ofbdivided by 2 (which is either 0 or 1, the next digit in the binary representation), appends it to the listl, and then calls itself withbdivided by 2 (ignoring any remainder). -
The function is initially called with
bequal to 6. The binary representation of 6 is 110. -
After the recursive calls are finished, the list
lcontains the binary digits in reverse order, [0, 1, 1]. -
The list
lis then reversed, resulting in the correct order of binary digits, [1, 1, 0]. -
Finally, the binary digits are printed out without any spaces in between, resulting in the output 110.
Similar Questions
Select the correct answerWhat will be the output of the following Python code?res=[]def change(x): if(x==0): return res temp=x%2 res.append(temp) change(x//2)change(6)res.reverse()for k in res: print(k,end="")Options1100113Infinite loop
nswerWhat will be the output of the following Python code?res=[]def change(x): if(x==0): return res temp=x%2 res.append(temp) change(x//2)change(6)res.reverse()for k in res: print(k,end="")
What will be the output of the following Python code? print(5, 6, 7, sep = "*", end = ' ' )Options: Pick one correct answer from below5 6 72105*6*7None of the Above
What will be the output of the following code?a=(1111,2)print(a)
Select the correct answerWhat will be the output of the following Python code?import rep = 'abc123 xyz789 lmn-22 def456'print(re.sub(r'\b([a-z]+)(\d+)', r'\2\1:', p))Options‘456def: lmn-22: 789xyz: 123abc’‘abc123: xyz789: lmn-22: def456’‘abc123:’, ‘xyz789:’, ‘lmn-22:’, ‘def456:’‘123abc: 789xyz: lmn-22 456def:’
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.