Understanding Unary and Binary 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 unary and binary operators. 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 = -5 print(+x)
- -5
- 5
- 0
- -1
Answer: a) -5
Explanation: The unary + operator does not change the sign of the operand. Therefore, +x is still -5.
Question 2: Which operator is a unary operator in the following list?
- +
- *
- //
- **
Answer: a) +
Explanation: The + operator can be used as a unary operator, while *, //, and ** are binary operators.
Question 3: What is the result of the following code?
y = 10 z = y ** 2 print(z)
- 10
- 20
- 100
- 1000
Answer: c) 100
Explanation: The ** operator is a binary operator that performs exponentiation. y ** 2 is 10 ** 2, which equals 100.
Question 4: Which of the following are valid binary operators in Python? (Select all that apply).
- +
- -
- //
- -(unary negation)
Answer: a) +, b) -, c) //
Explanation: +, -, and // are binary operators. Unary negation - is not a binary operator.
Question 5: What are the results of the following code? (Select all that apply)
a = 15 b = -a c = a * -1
- b is -15
- b is 15
- c is -15
- c is 15
Answer: a) b is -15, c) c is -15
Explanation:The unary negation - operator changes the sign of a. Therefore, b is -15 and c is also -15
Question 6: Rearrange the following expressions to evaluate the sum of two numbers and negate the result: neg_sum = -sum, sum = 10 + 5.
▼Answer:
- sum = 10 + 5
- neg_sum = -sum
Explanation: First, compute the sum of 10 and 5, then negate the result using the unary - operator.
Question 7: Rearrange the following steps to correctly compute and print the result of 10 * 2 ** 3: print(result), result = 10 * 2 ** 3.
▼Answer:
- result = 10 * 2 ** 3
- print(result)
Explanation: First, compute the expression 10 * 2 ** 3, then print the result.
Question 8: The operator used to increment a variable by 1 is the ______ operator.
▼Answer: +
Explanation: The + operator can be used to increment a variable, though there is no direct increment operator in Python like ++ in some other languages.
Question 9: The operator used to negate a value is the ______ operator.
▼Answer: -
Explanation: The unary - operator negates the value of its operand.
Question 10: Sort the following operations to correctly evaluate x = 5 and y = -x: print(y), y = -x, x = 5.
▼Answer:
- x = 5
- y = -x
- print(y)
Explanation: Assign 5 to x, negate x and assign to y, then print y.
Question 11: Sort the following expressions in ascending order of their results: 10 // 3, -10 // 3, -10 % 3, 10 % 3.
▼Answer:
- -10 // 3 (result: -4)
- 10 % 3 (result: 1)
- -10 % 3 (result: 2)
- 10 // 3 (result: 3)
Explanation: The results of the operations are -4, 1, 2, and 3 respectively.
- 10 // 3 10 // 3 = 3
- -10 // −10 // 3 = −4 (Floor division rounds towards negative infinity)
- -10 % 3 −10 % 3 = 2 (Modulo operation returns a positive remainder)
- 10 % 3 10 % 3 = 1
Question 12: Fill in the missing code to correctly negate the value of z.
z = 7 result = ______ z▼
Answer: -
Explanation: The unary - operator is used to negate z.
Question 13: Fill in the missing code to correctly compute the power of a to b.
a = 2 b = 3 result = a ______ b▼
Answer: **
Explanation: The ** operator is used for exponentiation.
Question 14: Insert the correct operator to divide c by d.
c = 18 d = 3 result = c ______ d▼
Answer: /
Explanation: The / operator is used to perform division.
Question 15: Insert the correct operator to find the remainder of m divided by n.
m = 10 n = 4 result = m ______ n▼
Answer: %
Explanation: The % operator is used to find the remainder of division.
Question 16: What is the result of the following code?
x = 4 y = -2 result = x * y print(result)
- -8
- 8
- -6
- 6
Answer: a) -8
Explanation: The * operator multiplies x and y. Since y is -2, the result is -8.
Question 17: Which of the following is a binary operator?
- ~
- - (unary)
- +
- not
Answer: c) +
Explanation: The + operator is a binary operator, whereas ~, unary -, and not are unary operators.
Question 18: Which of the following are valid unary operators in Python? (Select all that apply)
- +
- -
- ~
- *
Answer: a) +, b) -, c) ~
Explanation: +, -, and ~ are unary operators. The * operator is a binary operator.
Question 19: What are the results of the following code? (Select all that apply)
p = 4 q = -p r = ~p
- q is -4
- q is 4
- r is -5
- r is 5
Answer: a) q is -4, c) r is -5
Explanation: The unary negation - operator changes the sign of p to -4. The ~ operator inverts all the bits, resulting in -5 for ~4.
Question 20: Rearrange the following expressions to correctly compute x raised to the power of y and then negated: x = 2, result = x ** y, y = 3, result = -result.
▼Answer:
- x = 2
- y = 3
- result = x ** y
- result = -result
Explanation: Assign values to x and y, compute the power, then negate the result.
Question 21: Rearrange the following steps to correctly compute and print the result of -8 // 3: print(result), result = -8 // 3.
▼Answer:
- result = -8 // 3
- print(result)
Explanation: First, compute the expression -8 // 3, then print the result.
Question 22: The operator used to invert all bits of a number is the ______ operator.
▼Answer: ~
Explanation: The ~ operator inverts all the bits of its operand.
Question 23: The operator used to multiply two numbers is the ______ operator.
▼Answer: *
Explanation: The * operator multiplies the values of its operands.
Question 24: Sort the following operations to correctly evaluate x = 7 and y = ~x: print(y), y = ~x, x = 7.
▼Answer:
- x = 7
- y = ~x
- print(y)
Explanation: Assign 7 to x, invert x and assign to y, then print y.
Question 25: Sort the following expressions in ascending order of their results: -10 // 3, 10 // -3, 10 % -3, -10 % 3.
▼Answer:
- 10 // -3 (result: -4)
- -10 // 3 (result: -4)
- 10 % -3 (result: -2)
- -10 % 3 (result: 2)
Explanation: The results of the operations are -4, -4, -2, and 2 respectively.
Question 26: Fill in the missing code to correctly compute the floor division of a by b.
a = 9 b = 2 result = a ______ b▼
Answer: //
Explanation: The // operator is used for floor division.
Question 27: Fill in the missing code to correctly invert all bits of n.
n = 5 result = ______ n▼
Answer: ~
Explanation: The ~ operator inverts all the bits of n.
Question 28: Insert the correct operator to add d to e.
d = 7 e = 8 result = d ______ e▼
Answer: +
Explanation: The + operator is used to add d to e.
Question 29: Insert the correct operator to subtract q from r.
q = 3 r = 15 result = r ______ q▼
Answer: -
Explanation: The - operator is used to subtract q from r.
Question 30: What is the result of the following code?
a = 5 b = -a result = b + a print(result)
- 0
- 10
- -10
- 5 ▼
Answer: a) 0
Explanation: The value of b is -5, and a is 5. Therefore, b + a equals 0.
Test your Python skills with w3resource's quiz
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics