w3resource

Understanding Operator Precedence and Binding 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 priorities and binding. This diverse set of interactions enhances understanding and prepares for the PCEP-30-02 examination.

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

x = 3 + 2 * 2
print(x)
  1. 10
  2. 7
  3. 6
  4. 5

Answer: b) 7

Explanation: According to operator precedence, multiplication (*) has a higher priority than addition (+). Thus, 2 * 2 is evaluated first, resulting in 4, and then 3 + 4 is 7.

Question 2: Which of the following operators has the highest precedence?

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

Answer: d) **

Explanation: The exponentiation operator (**) has a higher precedence than multiplication (*), addition (+), and subtraction (-).

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

y = (3 + 2) * 2
print(y)
  1. 10
  2. 7
  3. 6
  4. 5

Answer: a) 10

Explanation: Parentheses have the highest precedence. Thus, 3 + 2 is evaluated first, resulting in 5, and then 5 * 2 is 10.

Question 4: Which of the following statements are true about operator precedence in Python? (Select all that apply).

  1. Multiplication has a higher precedence than addition.
  2. Exponentiation has the highest precedence among arithmetic operators
  3. Addition has a higher precedence than multiplication
  4. Parentheses can be used to change the order of evaluation

Answer: a) Multiplication has a higher precedence than addition., b) Exponentiation has the highest precedence among arithmetic operators., d) Parentheses can be used to change the order of evaluation

Explanation: Multiplication (*) has a higher precedence than addition (+), exponentiation (**) has the highest precedence among arithmetic operators, and parentheses can be used to change the default order of evaluation.

Question 5: Which operators have lower precedence than the multiplication operator (*)? (Select all that apply)

  1. Addition (+)
  2. Subtraction (-)
  3. Exponentiation (**)
  4. Division (/)

Answer: a) Addition (+), b) Subtraction (-)

Explanation: Addition (+) and subtraction (-) have lower precedence than multiplication (*). Exponentiation (**) and division (/) have higher or equal precedence.

Question 6: Place the following operations in the correct order of precedence: +, *, **, (), - (unary).

Answer:

  1. () (parentheses)
  2. - (unary)
  3. ** (exponentiation)
  4. * (multiplication)
  5. + (addition)

Explanation: Parentheses have the highest precedence, followed by unary operations, exponentiation, multiplication, and addition.

Question 7: Arrange the following code snippets to compute the value of z correctly: z = a + b, a = 2, b = 3 * 4, print(z).

Answer:

  1. a = 2
  2. b = 3 * 4
  3. z = a + b
  4. print(z)

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

Question 8: The operator with the highest precedence in Python is ______.

Answer: **

Explanation: The exponentiation operator (**) has the highest precedence among arithmetic operators.

Question 9: The expression a + b * c is evaluated by performing the ______ operation first.

Answer: *

Explanation: According to operator precedence, multiplication (*) is performed before addition (+).

Question 10: Sort the following operations to correctly evaluate x = 4 + 3 * 2 ** 2 // 4: 2 ** 2, 3 * 4, 4 + 12, 4 // 4.

Answer:

  • 2 ** 2 (result: 4)
  • 3 * 4 (result: 12)
  • 12 // 4 (result: 3)
  • 4 + 3 (result: 7)

Explanation: The results of the operations are evaluated in the order of their precedence: exponentiation, multiplication, floor division, and addition.

Question 11: Sort the following expressions in ascending order of their results: 5 + 3 * 2, 5 + (3 * 2), 5 * (3 + 2), (5 + 3) * 2.

Answer:

  • 5 + 3 * 2 (result: 11)
  • 5 + (3 * 2) (result: 11)
  • (5 + 3) * 2 (result: 16)
  • 5 * (3 + 2) (result: 25)

Explanation: The results of the operations are 11, 11, 16, and 25 respectively when evaluated correctly.

Question 12: Fill in the missing code to correctly compute the result of 3 ** 2 and add 5.

result = 3 ______ 2 + 5

Answer: **

Explanation: The ** operator is used for exponentiation. The correct code is result = 3 ** 2 + 5.

Question 13: Fill in the missing code to correctly compute the sum of 5 and 4 * 2.

result = 5 ______ 4 * 2

Answer: +

Explanation: The + operator is used for addition. The correct code is result = 5 + 4 * 2.

Question 14: Insert the correct operator to multiply a and b and then add c.

result = a ______ b + c

Answer: *

Explanation: The * operator is used for multiplication. The correct code is result = a * b + c.

Question 15: Insert the correct operator to first add d and e, then multiply the result by f.

result = (d ______ e) * f

Answer: +

Explanation: The + operator is used for addition. The correct code is result = (d + e) * f.

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

x = 2 ** 3 ** 2
print(x)
  • 64
  • 512
  • 8
  • 256

Answer: b) 512

Explanation: Exponentiation is right-associative, so 3 ** 2 is evaluated first to give 9, and then 2 ** 9 is 512.

Question 17: Which of the following expressions is evaluated first in 3 + 5 * 2 - 8 / 4?

  1. 3 + 5
  2. 5 * 2(unary)
  3. 8 / 4
  4. 2 - 8

Answer: b) 5 * 2

Explanation: According to operator precedence, multiplication (*) and division (/) are evaluated before addition (+) and subtraction (-).

Question 18: Which of the following statements about operator binding are true? (Select all that apply)

  1. Addition and subtraction are left-associative.
  2. Exponentiation is left-associative.
  3. Multiplication and division are left-associative.
  4. Exponentiation is right-associative.

Answer: a) Addition and subtraction are left-associative., c) Multiplication and division are left-associative., d) Exponentiation is right-associative.

Explanation: Addition, subtraction, multiplication, and division are left-associative, while exponentiation is right-associative.

Question 19: What are the results of the following code? (Select all that apply)

a = 2 + 3 * 4 ** 2
b = (2 + 3) * 4 ** 2
c = 2 + (3 * 4) ** 2
print(a, b, c)
  1. 50
  2. 80
  3. 50, 80, 146
  4. 50, 20, 200

Answer: c) 50, 80, 146

Explanation: The expression 4 ** 2 is evaluated first in each case. Therefore, a is 2 + 3 * 16 which is 50, b is (2 + 3) * 16 which is 80, and c is 2 + (3 * 4) ** 2 which is 146.

Question 20: The ______ operator is evaluated before the addition operator.

Answer: * or multiplication

Explanation: The multiplication operator (*) has higher precedence than the addition operator (+).

Question 21: In the expression a ** b ** c, the exponentiation is performed from ______ to ______.

Answer: right to left

Explanation: Exponentiation is right-associative, so the operation is performed from right to left.

Question 22: Sort the following operations to correctly evaluate 5 + 4 * 3 ** 2 - 6 // 3: 3 ** 2, 4 * 9, 5 + 36, 41 - 2.

Answer:

  1. 3 ** 2 (result: 9)
  2. 4 * 9 (result: 36)
  3. 5 + 36 (result: 41)
  4. 41 - 2 (result: 39)

Explanation: The results of the operations are evaluated in the order of their precedence: exponentiation, multiplication, addition, and subtraction.

Question 23: Sort the following expressions in descending order of their results:
10 - 5 * 2, (10 - 5) * 2, 10 - (5 * 2), 5 * (10 - 2).

Answer:

  1. 5 * (10 - 2) (result: 40)
  2. (10 - 5) * 2 (result: 10)
  3. 10 - (5 * 2) (result: 0)
  4. 10 - 5 * 2 (result: 0)

Explanation: The results of the operations are 40, 10, 0, and 0 respectively when evaluated correctly.

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-priorities-and-binding.php