w3resource

PCEP-30-02 Exam Prep: Comprehensive Practice on Boolean Expressions in Python

PCEP Certification Practice Test - Questions, Answers and Explanations

These questions thoroughly cover the topic of Boolean expressions in Python, providing a comprehensive preparation for the PCEP examination.

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

True and False
  1. True
  2. False
  3. None
  4. SyntaxError

Answer: B) False

Explanation: The and operator returns True only if both operands are True. In this case, one operand is False, so the expression evaluates to False.

Question 2: Which of the following Boolean expressions evaluate to True? (Choose all that apply)

  1. True or False
  2. not False
  3. True and not False
  4. False and True

Answer: A) True or False B) not False C) True and not False

Explanation: The or operator returns True if either operand is True. The not operator inverts the Boolean value. The and operator returns True only if both operands are True.

Question 3: Complete the following code to print "Yes" if the Boolean expression x > 0 and y < 0 is True.

x = 5
y = -3
if x > 0 and y < 0:
    print("______")

Answer: Yes

Explanation: The expression x > 0 and y < 0 evaluates to True because x is positive and y is negative. Therefore, "Yes" is printed.

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

a = 10
b = 20
print(a > b or b > a)
  1. True
  2. False
  3. None
  4. SyntaxError

Answer: A) True

Explanation: The or operator returns True if either operand is True. Since b > a is True, the expression evaluates to True.

Question 5: Insert the appropriate Boolean expression to check if both x and y are even numbers.

x = 4
y = 8
if ______:
    print("Both are even")

Answer:x % 2 == 0 and y % 2 == 0

Explanation: The expression checks if x and y are divisible by 2 with no remainder, which means they are both even numbers.

Question 6: What does the following Boolean expression evaluate to?

not (5 > 3 and 2 < 4)
  1. True
  2. False
  3. None
  4. SyntaxError

Answer: B) False

Explanation: The expression inside the parentheses evaluates to True (5 > 3 and 2 < 4 are both True). The not operator inverts it to False.

Question 7: Which of the following are valid Boolean expressions in Python? (Choose all that apply)

  1. 5 == 5
  2. 3 < 1 or 4 == 4
  3. True or False
  4. 10 > 2 and 8 <= 8

Answer:

  1. 5 == 5
  2. True or False
  3. 10 > 2 and 8 <= 8
  4. 3 < 1 or 4 == 4

Explanation: All these expressions are valid Boolean expressions in Python. They compare values or combine Boolean values using logical operators.

Question 8: Arrange the steps to create a Boolean expression that checks if a number is within the range 1 to 10 (inclusive).

  1. Use the and operator to combine the comparisons.
  2. Compare if the number is less than or equal to 10.
  3. Compare if the number is greater than or equal to 1.

Answer: 3, 2, 1

Explanation: First, compare if the number is greater than or equal to 1, then check if it is less than or equal to 10, and finally, combine these comparisons using the and operator.

Question 9: Complete the following code to check if a number is not equal to 10.

num = 7
if num ______ 10:
    print("Number is not 10")

Answer: !=

Explanation: The != operator checks if num is not equal to 10. Since 7 != 10 is True, "Number is not 10" is printed.

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

x = 3
y = 7
print(not (x > y) and y > x)
  1. True
  2. False
  3. None
  4. SyntaxError

Answer: A) True

Explanation: The expression x > y is False, so not (x > y) is True. The expression y > x is True, so the entire expression evaluates to True.

Question 11: Insert the correct Boolean expression to check if a number is either positive or even.

num = 4
if ______:
    print("Number is positive or even")

Answer: num > 0 or num % 2 == 0

Explanation: The or operator is used to check if the number is either positive (num > 0) or even (num % 2 == 0).

Question 12: What does the following Boolean expression evaluate to?

5 < 3 or 4 == 4 and 7 > 6
  1. True
  2. False
  3. None
  4. SyntaxError

Answer: A) True

Explanation: The and operator has higher precedence than or, so 4 == 4 and 7 > 6 is evaluated first (which is True), making the entire expression True.

Question 13: Which of the following Boolean expressions evaluate to False? (Choose all that apply)

  1. False or False
  2. True and False
  3. not True
  4. 5 > 10

Answer:

  1. False or False
  2. not True
  3. 5 > 10
  4. True and False

Explanation: All these expressions evaluate to False because they involve either False values or conditions that are not true.

Question 14: Arrange the steps to create a Boolean expression that checks if a value is either a multiple of 3 or greater than 10.

  1. Use the or operator to combine the comparisons.
  2. Check if the value is a multiple of 3.
  3. Check if the value is greater than 10.

Answer: 3, 2, 1

Explanation: First, check if the value is a multiple of 3, then check if it is greater than 10, and finally combine these conditions using the or operator.

Question 15: Complete the following code to check if x is between 5 and 15 (inclusive).

x = 10
if x >= 5 and x ______ 15:
    print("x is in the range")

Answer: <=

Explanation: The expression checks if x is greater than or equal to 5 and less than or equal to 15, which means x is in the range [5, 15].

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

a = 10
b = 20
print(a == 10 and b == 20)
  1. True
  2. False
  3. None
  4. SyntaxError

Answer: A) True

Explanation: Both comparisons a == 10 and b == 20 are True, so the entire expression evaluates to True.

Question 17: Insert the appropriate Boolean expression to check if a string s is either empty or contains the letter "a".

s = "apple"
if ______:
    print("String is either empty or contains 'a'")

Answer: s == "" or "a" in s

Explanation: The expression checks if the string s is either empty (s == "") or contains the letter "a" ("a" in s).

Question 18: What will the following Boolean expression evaluate to?

not (True and False) or (False and True)
  1. True
  2. False
  3. None
  4. SyntaxError

Answer: A) True

Explanation: The expression inside the not evaluates to False, so not (True and False) is True. The entire expression is True or False, which evaluates to True.

Question 19: Which of the following Boolean expressions are equivalent to not (x > 5)? (Choose all that apply)

  1. x <= 5
  2. x == 5
  3. x < 5
  4. x >= 5

Answer: A) x <= 5

Explanation: The expression not (x > 5) is equivalent to x <= 5. The not operator negates the condition, so x > 5 becomes x <= 5.

Question 20: Arrange the steps to create a Boolean expression that checks if a number is not between 1 and 100.

  1. Combine the conditions with the or operator.
  2. Use not with the and operator to check the opposite range.
  3. Check if the number is less than 1.
  4. Check if the number is greater than 100.

Answer: 3, 4, 1, 2

Explanation: First, check if the number is less than 1 or greater than 100, then combine these conditions with or, and finally, use not with the and operator to check the opposite range.

Question 21: Complete the following code to print "Match" if either x equals y or y equals z.

x = 3
y = 5
z = 5
if x == y or y ______ z:
    print("______")

Answer: ==, Match

Explanation: The or operator is used to check if either x == y or y == z. Since y == z is True, "Match" is printed.

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

x = 2
y = 4
print(x % 2 == 0 and y % 2 == 0)
  1. True
  2. False
  3. None
  4. SyntaxError

Answer: A) True

Explanation: The expression checks if both x and y are even numbers by checking if their remainder when divided by 2 is 0. Since both are even, the expression evaluates to True.

Question 23: Insert the correct Boolean expression to check if x is not equal to y and y is greater than z.

x = 5
y = 10
z = 3
if ______:
    print("Conditions met")

Answer: x != y and y > z

Explanation: The expression checks if x is not equal to y and if y is greater than z. Since both conditions are met, "Conditions met" is printed.

Question 24: What is the result of the following Boolean expression?

(True and False) or (False and True)
  1. True
  2. False
  3. None
  4. SyntaxError

Answer: B) False

Explanation: Both (True and False) and (False and True) evaluate to False, so the entire expression evaluates to False.

Question 25: Which of the following expressions will result in a True value? (Choose all that apply)

  1. 10 == 10 and 5 != 4
  2. not (3 > 5)
  3. 7 < 2 or 8 > 3
  4. not (2 == 2 and 4 != 4)

Answer: A) 10 == 10 and 5 != 4 B) not (3 > 5) C) 7 < 2 or 8 > 3

Explanation: The first three expressions evaluate to True. The last expression evaluates to False.



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-boolean-expressions.php