Question 31
MediumWhat is the output of this code?
a, b = 5, 10
a ^= b
b ^= a
a ^= b
print(a, b)
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the output of this code?
a, b = 5, 10
a ^= b
b ^= a
a ^= b
print(a, b)
10 10
5 10
10 5
5 5
Sign up free to see the answer and a step-by-step explanation.
How do you set the k-th bit (0-indexed) of n?
n & (1 << k)
n ^ k
n >> k
n | (1 << k)
Sign up free to see the answer and a step-by-step explanation.
How do you clear the k-th bit of n?
n | (1 << k)
n & ~(1 << k)
n ^ (1 << k) always
n << k
Sign up free to see the answer and a step-by-step explanation.
In two's complement with 8 bits, how is −1 represented?
01111111
10000001
11111111
00000001
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def bits(n):
c = 0
while n:
n &= n - 1
c += 1
return c
print(bits(29), bits(255), bits(1024))
4 8 1
4 7 1
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [4, 1, 2, 1, 2]
r = 0
for x in a:
r ^= x
print(r)
1
4
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
n = 6
print([bin(i).count("1") for i in range(n + 1)])
[0, 1, 1, 1, 2, 2, 2]
[0, 1, 1, 2, 1, 2, 2]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
x = 0b10110
print(x >> 2, x << 1, x & 0b111, x | 1)
5 44 7 23
5 22 6 23
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
s = [1, 2, 3]
out = []
for mask in range(1 << len(s)):
out.append([s[i] for i in range(len(s)) if mask >> i & 1])
print(out[5], len(out))
[1, 2] 8
[1, 3] 8
Sign up free to see the answer and a step-by-step explanation.
Which expression checks whether n > 0 is a power of 2?
n | 1 == n
n ^ 1 == 0
n % 2 == 0
n & (n − 1) == 0
Sign up free to see the answer and a step-by-step explanation.
4 8 10
5 8 1
0
10
[0, 1, 2, 2, 3, 3, 4]
[1, 1, 2, 1, 2, 2, 3]
11 44 6 22
5 44 6 23
[1, 3] 7
[2, 3] 8