Python Questions and Answers – Bitwise – 1

Q 1. What will be the output of the following Python code snippet if x=1?

x<<2

A. 8
B. 1
C. 2
D. 4

Show Answer Answer:-D. 4
Explanation Explanation:- The binary form of 1 is 0001. The expression x<<2 implies we are performing bitwise left shift on x. This shift yields the value: 0100, which is the binary form of the number 4.

Q 2. What will be the output of the following Python expression?

bin(29)

A. ‘0b10111’
B. ‘0b11101’
C. ‘0b11111’
D. ‘0b11011’

Show Answer Answer:-B. ‘0b11101’
Explanation Explanation:- The binary form of the number 29 is 11101. Hence the output of this expression is ‘0b11101’.

Q 3. What will be the value of x in the following Python expression, if the result of that expression is 2?

x>>2

A. 8
B. 4
C. 2
D. 1

Show Answer Answer:-A. 8
Explanation Explanation:- When the value of x is equal to 8 (1000), then x>>2 (bitwise right shift) yields the value 0010, which is equal to 2. Hence the value of x is 8.

Q 4. What will be the output of the following Python expression?

int(1011)?

A. 1011
B. 11
C. 13
D. 1101

Show Answer Answer:-A. 1011
Explanation Explanation:- The result of the expression shown will be 1011. This is because we have not specified the base in this expression. Hence it automatically takes the base as 10.

Q 5. To find the decimal value of 1111, that is 15, we can use the function:
A. int(1111,10)
B. int(‘1111’,10)
C. int(1111,2)
D. int(‘1111’,2)

Show Answer Answer:-D. int(‘1111’,2)
Explanation The expression int(‘1111’,2) gives the result 15. The expression int(‘1111’, 10) will give the result 1111.

Q 6. What will be the output of the following Python expression if x=15 and y=12?

x & y

A. b1101
B. 0b1101
C. 12
D. 1101

Show Answer Answer:-C. 12
Explanation Explanation:- The symbol ‘&’ represents bitwise AND. This gives 1 if both the bits are equal to 1, else it gives 0. The binary form of 15 is 1111 and that of 12 is 1100. Hence on performing the bitwise AND operation, we get 1100, which is equal to 12.

Q 7. Which of the following expressions results in an error?
A. int(1011)
B. int(‘1011’,23)
C. int(1011,2)
D. int(‘1011’)

Show Answer Answer:-C. int(1011,2)
Explanation Explanation:-The expression int(1011,2) results in an error. Had we written this expression as int(‘1011’,2), then there would not be an error.

Q 8. Which of the following represents the bitwise XOR operator?
A. &
B. ^
C. |
D. !

Show Answer Answer:-B. ^
Explanation Explanation:- The ^ operator represent bitwise XOR operation. &: bitwise AND, | : bitwise OR and ! represents bitwise NOT.

Q 9. What is the value of the following Python expression?

bin(0x8)

A. ‘0bx1000’
B. 8
C. 1000
D. ‘0b1000’

Show Answer Answer:-D. ‘0b1000’
Explanation Explanation:- The prefix 0x specifies that the value is hexadecimal in nature. When we convert this hexadecimal value to binary form, we get the result as: ‘0b1000’.

Q 10. What will be the output of the following Python expression?

0x35 | 0x75

A. 115
B. 116
C. 117
D. 118

Show Answer Answer:-C. 117
Explanation Explanation:- The binary value of 0x35 is 110101 and that of 0x75 is 1110101. On OR-ing these two values we get the output as: 1110101, which is equal to 117. Hence the result of the above expression is 117.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page