w3resource

Understanding Assignment and Shortcut Operators for PCEP-30-02

PCEP Certification Practice Test - Questions, Answers and Explanations

This comprehensive set of questions and explanations covers the fundamental topic of choosing operators and data types adequate to the problem, focusing on assignment and shortcut operators. This diverse set of interactions enhances understanding and prepares for the PCEP-30-02

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

x = 5
x += 3
print(x)
  1. 3
  2. 5
  3. 8
  4. 15

Answer: c) 8

Explanation: The += operator adds the right operand to the left operand and assigns the result to the left operand. x += 3 is equivalent to x = x + 3.

Question 2: What does the x *= 2 operation do to x if x is initially 10?

  1. Multiplies x by 2
  2. Divides x by 2
  3. Adds 2 to x
  4. Subtracts 2 from x

Answer: a) Multiplies x by 2

Explanation: The *= operator multiplies the left operand by the right operand and assigns the result to the left operand. x *= 2 is equivalent to x = x * 2.

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

y = 15
y //= 4
print(y)
  1. 3
  2. 3.75
  3. 4
  4. 11

Answer: a) 3

Explanation: The //= operator performs floor division. y //= 4 is equivalent to y = y // 4.

Question 4: Which of the following are valid assignment and shortcut operators in Python? (Select all that apply).

  1. +=
  2. -=
  3. **=
  4. %%=

Answer: a) +=, b) -=, c) **=

Explanation: +=, -=, and **= are valid shortcut operators in Python. %%= is not a valid operator in Python.

Question 5: What is the result of the following code if a = 10 and b = 3? (Select all that apply)

  1. a += b
  2. a //= 2
  3. a -= 1
  1. a becomes 6
  2. a becomes 8
  3. a becomes 5
  4. a becomes 4

Answer: a) a becomes 6, c) a becomes 5

Explanation:

  • After a += b, a is 13.
  • After a //= 2, a is 6.
  • After a -= 1, a is 5.

Question 6: Arrange the following operations in order to achieve x = 8 starting from x = 1: x *= 2, x += 3, x += 1, x *= 2, x -= 6.

Answer:

  1. x += 1 (x = 2)
  2. x *= 2 (x = 4)
  3. x += 3 (x = 7)
  4. x *= 2 (x = 14)
  5. x -= 6 (x = 8)

Explanation: The operations should be arranged to first increment, then double, add, and double again, and then subtract 6 to achieve x = 8.

Question 7: The operation that adds the right operand to the left operand and assigns the result to the left operand is ______.

Answer: +=

Explanation: The += operator adds the right operand to the left operand and assigns the result to the left operand.

Question 8: The operation x //= 3 is equivalent to x = x ______ 3.

Answer: //

Explanation: The //= operator performs floor division and is equivalent to x = x // 3.

Question 9: Sort the following operations by the value of x after performing them on x = 2: x *= 3, x += 2, x -= 1, x //= 1.

Answer:

  • x -= 1 (x = 1)
  • x //= 1 (x = 2)
  • x += 2 (x = 4)
  • x *= 3 (x = 6)

Explanation: The results of the operations are 1, 2, 4, and 6 respectively when performed on x = 2.

Question 10: Sort the following expressions in ascending order of the final value of y starting from y = 10: y %= 3, y **= 2, y -= 5, y //= 3.

Answer:

  • y %= 3 (y = 1)
  • y //= 3 (y = 3)
  • y -= 5 (y = 5)
  • y **= 2 (y = 100))

Explanation: The results of the operations are 1, 3, 5, and 100 respectively when performed on y = 10.

Question 11: Fill in the missing code to correctly use the shortcut operator for multiplication.

z = 7
z ______= 4

Answer: *

Explanation: The *= operator is used for multiplication.

Question 12: Fill in the missing code to correctly use the shortcut operator for exponentiation.

a = 2
a ______= 5

Answer: **

Explanation: The **= operator is used for exponentiation.

Question 13: Insert the correct shortcut operator to subtract 3 from b.

b = 12
b ______ 3

Answer: -=

Explanation: The -= operator subtracts the right operand from the left operand and assigns the result to the left operand.

Question 14: Insert the correct shortcut operator to divide n by 2.

n = 14
n ______ 2

Answer: /=

Explanation: The /= operator divides the left operand by the right operand and assigns the result to the left operand.

Question 15: What is the result of the following code?.

c = 8
c %= 5
print(c)
  1. 3
  2. 5
  3. 8
  4. 0

Answer: a) 3

Explanation: The %= operator finds the remainder of the division of the left operand by the right operand and assigns the result to the left operand. c %= 5 is equivalent to c = c % 5.

Question 16: What does the x **= 3 operation do to x if x is initially 2?.

  1. Squares x
  2. Cubes x
  3. Multiplies x by 3
  4. Divides x by 3

Answer: b) Cubes x

Explanation: The **= operator raises the left operand to the power of the right operand and assigns the result to the left operand. x **= 3 is equivalent to x = x ** 3.

Question 17: Which of the following are valid shortcut operators in Python? (Select all that apply)

  1. *=
  2. /=
  3. %+=
  4. //=

Answer: a) *=, b) /=, d) //=

Explanation: *=, /=, and //= are valid shortcut operators in Python. %+= is not a valid operator in Python.

Question 18: What is the result of the following code if m = 9 and n = 4? (Select all that apply)

m -= n
m *= 2
m /= 3
  1. m becomes 5.0
  2. m becomes 6.0
  3. m becomes approximately 3.33
  4. m becomes 10.0

Answer: c) m becomes approximately 3.33

Explanation:

  • After m -= n, m is 5
  • After m *= 2, m is 10
  • After m /= 3, m is approximately 3.33, a valid option.

Question 19: Arrange the following operations in order to achieve z = 10 starting from z = 2: z **= 2, z += 1, z -= 4, z *= 2.

Answer:

  1. z += 1 (z = 3)
  2. z **= 2 (z = 9)
  3. z -= 4 (z = 5)
  4. z *= 2 (z = 10)

Explanation: The operations should be arranged to first add, then exponentiate, subtract, and finally multiply.

Question 20: Arrange the following operations in order to achieve y = 10 starting from y = 5: y -= 5, y *= 3, y //= 2, y %= 2.

Answer:

  1. y *= 3 (y = 15)
  2. y -= 5 (y = 10)
  3. y //= 2 (y = 5)
  4. y %= 2 (y = 1)

Explanation: The operations should be arranged to first multiply, then subtract, floor divide, and find the modulus to achieve the required final value.

Question 21: The operation that divides the left operand by the right operand and assigns the result to the left operand is ______.

Answer: /=

Explanation: The /= operator divides the left operand by the right operand and assigns the result to the left operand.

Question 22: The operation b %= 2 is equivalent to b = b ______ 2.

Answer: %

Explanation: The %= operator finds the remainder of the division of the left operand by the right operand and assigns the result to the left operand.

Question 23: Sort the following operations by the value of x in descending order after performing them on x = 4: x **= 2, x %= 3, x += 2, x //= 2.

Answer:

  • x **= 2 (x = 16)
  • x += 2 (x = 6)
  • x //= 2 (x = 2)
  • x %= 3 (x = 1)

Explanation: The results of the operations are 16, 6, 2, and 1 respectively when performed on x = 4.

Question 24: Sort the following expressions in ascending order of the final value of w starting from w = 8: w += 2, w *= 3, w %= 5, w **= 2.

Answer:

  • w %= 5 (w = 3)
  • w += 2 (w = 5)
  • w *= 3 (w = 15)
  • w **= 2 (w = 225)

Explanation: The results of the operations are 3, 5, 15, and 225 respectively when performed on w = 8.

Question 25: Fill in the missing code to correctly use the shortcut operator for division.

q = 16
q ______= 4

Answer: /

Explanation: The /= operator is used for division.

Question 26: Fill in the missing code to correctly use the shortcut operator for modulus.

r = 10
r ______= 3

Answer: %

Explanation: The %= operator is used for modulus.

Question 27: Insert the correct shortcut operator to subtract 7 from k.

k = 20
k ______ 7

Answer: -=

Explanation: The -= operator subtracts the right operand from the left operand and assigns the result to the left operand.

Question 28: Insert the correct shortcut operator to floor divide m by 3.

m = 18
m ______ 3

Answer: //=

Explanation: The //= operator performs floor division.

Test your Python skills with w3resource's quiz



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-assignment-and-shortcut-operators.php