w3resource

PCEP-30-02 Exam Prep: Mastering Bitwise Operators and Data Types

PCEP Certification Practice Test - Questions, Answers and Explanations

This comprehensive set of questions and explanations covers the topic of choosing operators and data types with a focus on bitwise operators. These exercises are designed to enhance understanding and prepare you for the PCEP-30-02 examination.

Question 1: What is the result of the following expression?

result = 5 & 3
print(result)
  1. 7
  2. 3
  3. 1
  4. 0

Answer: c) 1

Explanation: The & (bitwise AND) operator compares each bit of its operands. The binary representation of 5 is 101, and 3 is 011. Performing a bitwise AND results in 001, which equals 1 in decimal.

Question 2: Evaluate the following expression:

result = 12 | 5
print(result)
  1. 15
  2. 13
  3. 9
  4. 7

Answer: a) 13

Explanation: The | (bitwise OR) operator compares each bit of its operands. The binary representation of 12 is 1100, and 5 is 0101. Performing a bitwise OR results in 1101, which equals 13 in decimal.

Question 3: What is the result of the following expression?

result = ~10
print(result)
  1. -11
  2. 10
  3. -10
  4. 11

Answer: a) -11

Explanation: The ~ (bitwise NOT) operator inverts all the bits of its operand. The binary representation of 10 is 1010, and inverting the bits gives ...11110101, which represents -11 in two's complement form.

Question 4: What is the result of the following expression?.

result = 8 >> 2
print(result)
  1. 2
  2. 4
  3. 8
  4. 1

Answer: b) 2

Explanation: The >> (right shift) operator shifts the bits of 8 (1000 in binary) two positions to the right, resulting in 10, which is 2 in decimal.

Question 5: What will be the result of the following operation?

result = 4 << 1
print(result)
  1. a
  2. 2
  3. 4
  4. 16

Answer:a) 8

Explanation: The << (left shift) operator shifts the bits of 4 (100 in binary) one position to the left, resulting in 1000, which is 8 in decimal.

Question 6: Which of the following expressions will result in 7? (Select all that apply).

  1. 5 | 2
  2. 6 & 7
  3. 4 ^ 3
  4. 8 >> 1

Answer:a) 5 | 2, c) 4 ^ 3

Explanation:

  • 5 | 2 gives 7 (binary 101 | 010 = 111).
  • 4 ^ 3 also gives 7 (binary 100 ^ 011 = 111).
  • 6 & 7 results in 6 and 8 >> 1 results in 4.

Question 7: Which of the following operations will result in 0? (Select all that apply).

  1. 7 & 8
  2. 3 ^ 3
  3. ~1
  4. 4 << 2

Answer: a) 7 & 8, b) 3 ^ 3

Explanation: First, assign values to a and b, then compute z and print it.

  • 7 & 8 results in 0 since there are no common set bits.
  • 3 ^ 3 results in 0 because XORing the same number gives 0.
  • ~1 results in -2 and 4 << 2 results in 16.

Question 8: Which expressions use the bitwise XOR operator? (Select all that apply).

  1. result = a ^ b
  2. result = a | b
  3. result = a & b
  4. result = ~a

Answer: a) result = a ^ b

Explanation: The XOR operator is represented by ^ in Python.

Question 9: Fill in the blank with the correct bitwise operator to toggle the bits of a number.

result = ~x

Answer: ~

Explanation: The ~ operator is used to toggle (invert) the bits of a number.

Question 10: To combine two binary numbers using bitwise AND, you would use the ______ operator.

Answer: &

Explanation: The & operator is used to perform a bitwise AND operation.

Question 11: Arrange the following steps in the correct order to perform a bitwise left shift operation:

  • Shift the bits to the left.
  • Append zeros to the right.
  • Determine the number of positions to shift.

Answer:

  • Determine the number of positions to shift.
  • Shift the bits to the left.
  • Append zeros to the right.

Explanation: A left shift operation involves determining the shift count, shifting the bits left, and appending zeros.

Question 12: Fill in the missing code to perform a bitwise OR operation on a and b.

result = a ______ b

Answer: |

Explanation: The | operator is used to perform a bitwise OR operation between a and b.

Question 13: Fill in the missing code to perform a bitwise XOR operation on a and b.

result = a ______ b

Answer: ^

Explanation: The ^ operator is used to perform a bitwise XOR operation between a and b.

Question 14: Sort the following operations to correctly perform a bitwise right shift by 3 positions:

  • Shift the bits to the right.
  • Remove the last three bits.
  • Determine the number of positions to shift.

Answer:

  • Determine the number of positions to shift.
  • Shift the bits to the right.
  • Remove the last three bits.

Explanation: A right shift involves shifting bits right by the specified number of positions, effectively removing the specified number of least significant bits.

Question 15: Insert the appropriate bitwise operator to check if the second bit of x is set.

if x & 0b10 ______ 0b10:
    print("Second bit is set")

Answer: ==

Explanation: The & operator is used to isolate the second bit, and == is used to compare it with 0b10.

Question 16: Insert the appropriate bitwise operation to double the value of y.

y = y ______ 1

Answer: <<

Explanation:The << operator shifts the bits left by one position, effectively doubling the value.

Question 17: What is the result of the following operation?

result = 10 & 7
print(result)
  1. 3
  2. 2
  3. 7
  4. 10

Answer: b) 2

Explanation: The bitwise AND operator (&) compares each bit of the numbers 10 (1010 in binary) and 7 (0111 in binary) and returns 0010 in binary, which is 2 in decimal.

Question 18: What will be the output of this code?

x = 4
y = 1
result = x << y
print(result)
  • 8
  • 5
  • 9
  • 2

Answer: a) 8

Explanation: The left shift operator (<<) shifts the bits of x (which is 4, or 0100 in binary) one place to the left, resulting in 1000, which is 8 in decimal.

Question 19: What is the result of this operation?

result = ~5
print(result)
  • 5
  • -4
  • -6
  • 6

Answer: c) -6

Explanation: The bitwise NOT operator (~) inverts all bits of 5 (which is 0101 in binary), resulting in ...11111010 in binary, which is -6 in decimal (due to two's complement representation).

Question 20: Which of the following results are correct for the bitwise OR (|) operation? (Select all that apply)

a = 3
b = 5
result = a | b
  • 7
  • 8
  • 5
  • 3

Answer: a) 7

Explanation: The bitwise OR (|) compares each bit of 3 (0011 in binary) and 5 (0101 in binary) and results in 0111, which is 7 in decimal.

Question 21: Which of the following outputs can be generated by this code? (Select all that apply)

a = 6
b = 3
result = a ^ b
print(result)
  • 1
  • 5
  • 7
  • 0

Answer: b) 5

Explanation: The bitwise XOR (^) compares each bit of 6 (0110 in binary) and 3 (0011 in binary), resulting in 0101, which is 5 in decimal.

Question 22: Arrange the following binary representations in the correct order to calculate the result of 9 & 5:

  • 0101
  • 1001
  • 0001

Answer:

  • 1001 (binary for 9)
  • 0101 (binary for 5)
  • 0001 (binary result of 9 & 5)

Explanation: The bitwise AND operator (&) compares each bit of 9 and 5, resulting in 0001, which is 1 in decimal.

Question 23: The bitwise left shift operator (<<) shifts bits to the ______, multiplying the number by ______.

Answer: left, 2

Explanation: The left shift operator (<<) shifts the bits of the number to the left and multiplies the number by 2 for each shift.

Question 24: The bitwise OR operator (|) compares each bit and returns 1 if either of the bits is ______.

Answer: 1

Explanation: The OR operator (|) returns 1 if at least one of the corresponding bits is 1.

Question 25: Sort the following steps to correctly calculate the bitwise XOR (^) of 7 and 4:

  • 0100
  • 0111
  • 0011

Answer:

  • 0111 (binary for 7)
  • 0100 (binary for 4)
  • 0011 (binary result of 7 ^ 4)

Explanation: The XOR operator (^) compares each bit of 7 and 4, resulting in 0011, which is 3 in decimal.

Question 26: Fill in the missing code to calculate the result of 15 OR 9.

result = 15 _____ 9
print(result)

Answer: |

Explanation: The bitwise OR (|) compares each bit of 15 (1111 in binary) and 9 (1001 in binary), resulting in 1111, which is 15 in decimal.

Question 27: Fill in the missing code to perform a right shift on 32 by 2 bits.

result = 32 _____ 2
print(result)

Answer: >>

Explanation: The right shift operator (>>) shifts the bits of 32 (100000 in binary) two places to the right, resulting in 1000, which is 8 in decimal.

Question 28: Insert the correct operation to invert all bits of the number 12.

num = 12
result = ______ num
print(result)

Answer: ~

Explanation: The bitwise NOT operator (~) inverts all bits of 12, resulting in ...11110011 in binary, which is -13 in decimal.

Question 29: Insert the appropriate operation to compute the bitwise AND of 7 and 10.

a = 7
b = 10
result = a ______ b
print(result)

Answer: &

Explanation: The bitwise AND operator (&) compares each bit of 7 (0111 in binary) and 10 (1010 in binary), resulting in 0010, which is 2 in decimal.

Question 30: What will be the output of the following code?

x = 12
y = 25
result = x ^ y
print(result)
  • 5
  • 17
  • 21
  • 29

Answer: c) 21

Explanation: The bitwise XOR operator (^) compares each bit of 12 (1100 in binary) and 25 (11001 in binary), resulting in 10101, which is 21 in decimal.

Question 31: What will be the output of this code snippet?

num = 14
result = num >> 1
print(result)
  • 7
  • 28
  • 12
  • 10

Answer: a) 7



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python/certificate/operators-and-data-types-bitwise-operators.php