w3resource

Python PCEP Exam - Control Flow if, if-else, if-elif-else

PCEP Certification Practice Test - Questions, Answers and Explanations

Below is a set of questions for the Certified Entry-Level Python Programmer (PCEP-30-02) examination focusing on the subtopic of "conditional statements: if, if-else, if-elif, if-elif-else." The questions use various formats including single- and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.

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

x = 10
if x > 5:
    print("Greater than 5")
  1. Greater than 10
  2. Greater than 5
  3. 5
  4. No output

Answer: B) Greater than 5

Explanation: The if statement checks if x is greater than 5. Since x is 10, the condition is True, and the message "Greater than 5" is printed.

Question 2: Which of the following conditional structures will print "Equal" if a equals b? (Choose all that apply)

  1. if a == b:
        print("Equal")
    
  2. if a != b:
        print("Not equal")
    else:
        print("Equal")
    
  3. if a > b:
        print("Greater")
    elif a == b:
        print("Equal")
    
  4. if a < b:
        print("Smaller")
    elif a == b:
        print("Equal")
    

Answer: A), B), C), and D)

Explanation: All the options contain a condition that checks if a equals b. If the condition is met, "Equal" will be printed.

Question 3: Complete the following code to print "Even" if the variable num is an even number.

num = 4
if num % 2 == 0:
    print("______")

Answer: Even

Explanation: The expression num % 2 == 0 checks if num is divisible by 2 with no remainder, meaning it is even. If true, "Even" is printed.

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

x = 15
if x > 10:
    print("x is greater than 10")
else:
    print("x is less than or equal to 10")
  1. x is greater than 10
  2. x is less than or equal to 10
  3. x is 10
  4. No output

Answer: A) x is greater than 10

Explanation: The condition x > 10 is True, so the first block of code is executed, and "x is greater than 10" is printed.

Question 5: Insert the appropriate elif statement to check if y is 0.

y = 10
if y > 0:
    print("Positive")
______ y == 0:
    print("Zero")
else:
    print("Negative")

Answer: elif

Explanation: The elif statement is used to check an additional condition (y == 0). If y is 0, "Zero" will be printed.

Question 6: What is the purpose of the else clause in an if-else statement?

  1. It runs when the if condition is True.
  2. It runs when the if condition is False.
  3. It runs before the if condition is checked.
  4. It is not required in an if statement.

Answer: B) It runs when the if condition is False.

Explanation: The else clause is executed only when the if condition is False.

Question 7: Which of the following code snippets will correctly handle multiple conditions? (Choose all that apply)

  1. if a > b:
        print("a is greater than b")
    elif a == b:
        print("a is equal to b")
    else:
        print("a is less than b")
    
  2. if a == b:
        print("Equal")
    else:
        print("Not equal")
    
  3. if a > b:
        print("Greater")
    elif a < b:
        print("Smaller")
    
  4. if a > b:
        print("Greater")
    if a < b:
        print("Smaller")
    

Answer: A), B), C), and D)

Explanation: All these code snippets demonstrate valid uses of if, elif, and else statements to handle multiple conditions.

Question 8: Arrange the steps to create an if-elif-else statement that prints the appropriate message based on the value of x.

  1. Add an else clause for the case where x is 0.
  2. Start with an if statement checking if x is positive.
  3. Add an elif clause to check if x is negative.
  4. Print the appropriate message in each block.

Answer: 2, 3, 1, 4

Explanation: First, check if x is positive, then add an elif clause to check if x is negative, followed by an else clause for when x is 0. Finally, print the appropriate message in each block.

Question 9: Complete the following code to check if a variable a is between 10 and 20 (inclusive).

a = 15
if a >= 10 and a ______ 20:
    print("a is in the range")
else:
    print("a is out of the range")

Answer: <=

Explanation: The condition a >= 10 and a <= 20 checks if a is between 10 and 20 (inclusive). If true, "a is in the range" is printed.

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

score = 85
if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
else:
    print("Grade: F")
  1. Grade: A
  2. Grade: B
  3. Grade: C
  4. Grade: F

Answer: B) Grade: B

Explanation: The condition score >= 80 is True, so the corresponding block is executed, and "Grade: B" is printed.

Question 11: Insert the correct else clause to handle cases where num is neither 1 nor 2.

num = 3
if num == 1:
    print("One")
elif num == 2:
    print("Two")
______:
    print("Other number")

Answer: else

Explanation: The else clause handles any cases not covered by the previous conditions. In this case, since num is neither 1 nor 2, "Other number" is printed.

Question 12: What will the following code print if x = 7?

x = 7
if x > 10:
    print("x is greater than 10")
elif x == 7:
    print("x is 7")
else:
    print("x is less than 10")
  1. x is greater than 10
  2. x is 7
  3. x is less than 10
  4. No output

Answer: B) x is 7

Explanation: The condition x == 7 is True, so the corresponding block is executed, and "x is 7" is printed.

Question 13: Which of the following conditions will correctly evaluate to True when x = 5? (Choose all that apply)

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

Answer: A), B), and C)

Explanation: The conditions x < 10, x == 5, and x >= 1 will all evaluate to True when x is 5. The condition x != 5 will evaluate to False.

Question 14: Arrange the steps to create an if-elif-else statement that checks if a number is positive, negative, or zero.

  1. Print "Zero" in the else clause.
  2. Print "Positive" in the if block.
  3. Add an elif clause to check if the number is negative.
  4. Start with an if statement to check if the number is positive.

Answer: 4, 2, 3, 1

Explanation: Start by checking if the number is positive, then check if it is negative, and finally handle the case where the number is zero.

Question 15: Complete the following code to print "Equal" if a equals b and "Not equal" otherwise.

a = 10
b = 10
if a == b:
    print("______")
else:
    print("Not equal")
  1. Start-->End.
  2. Start End.
  3. Start--> End.
  4. Start End-->.

Answer: Equal

Explanation: The condition a == b checks if a and b are equal. If true, "Equal" is printed.

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

temperature = 30
if temperature > 40:
    print("It's hot")
elif temperature > 30:
    print("It's warm")
else:
    print("It's cold")
  1. It's hot
  2. It's warm
  3. It's cold
  4. No output

Answer: C) It's cold

Explanation: Since temperature is 30, both if and elif conditions are False, so the else block is executed, printing "It's cold."

Question 17: Insert the correct condition to check if a is greater than b or c.

a = 5
b = 3
c = 4
if a > ______ or a > c:
    print("a is greater than b or c")
else:
    print("a is not greater than b or c")

Answer: b

Explanation: The condition a > b or a > c checks if a is greater than either b or c. If true, the message "a is greater than b or c" is printed.

Question 18: What will the following code print if age = 18?

age = 18
if age < 18:
    print("Underage")
elif age == 18:
    print("Exactly 18")
else:
    print("Over 18")
  1. Underage
  2. Exactly 18
  3. Over 18
  4. No output

Answer: B) Exactly 18

Explanation: The condition age == 18 is True, so the corresponding block is executed, and "Exactly 18" is printed.

Question 19: Which of the following will print "Small number" if x < 10? (Choose all that apply).

  1. if x < 10:
        print("Small number")
    
  2. if x > 10:
        print("Large number")
    elif x < 10:
        print("Small number")
    
  3. if x < 10:
        print("Small number")
    else:
        print("Large number")
    
  4. if x >= 10:
        print("Large number")
    else:
        print("Small number")
    

Answer: A), B), C), and D)

Explanation: All the options contain a condition that checks if x < 10. If the condition is met, "Small number" will be printed.

Question 20: Arrange the steps to create an if-elif statement that checks if a number is divisible by 2 or 3.

  1. Add an elif clause to check divisibility by 3.
  2. Start with an if statement to check divisibility by 2.
  3. Print "Divisible by 2" if the first condition is True.
  4. Print "Divisible by 3" if the second condition is True.

Answer: 2, 3, 1, 4

Explanation: Start by checking if the number is divisible by 2, then check if it is divisible by 3, and print the appropriate message in each block.

Question 21: Complete the following code to print "Negative" if x is less than 0 and "Non-negative" otherwise.

x = -5
if x < 0:
    print("______")
else:
    print("Non-negative")

Answer: Negative

Explanation: The condition x < 0 checks if x is negative. If true, "Negative" is printed.

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

a = 5
b = 10
if a > b:
    print("a is greater")
elif a < b:
    print("b is greater")
else:
    print("a and b are equal")
  1. a is greater
  2. b is greater
  3. a and b are equal
  4. No output

Answer: B) b is greater

Explanation: Since a is less than b, the elif block is executed, printing "b is greater."

Question 23: Insert the correct elif statement to check if n is greater than 50.

n = 45
if n < 20:
    print("Less than 20")
______ n > 50:
    print("Greater than 50")
else:
    print("Between 20 and 50")

Answer: elif

Explanation: The elif statement is used to check if n is greater than 50. If true, "Greater than 50" will be printed. Otherwise, the else block will handle all other cases.

Question 24: What will the following code print if x = 25?

x = 25
if x >= 30:
    print("High")
elif x >= 20:
    print("Medium")
else:
    print("Low")
  1. High
  2. Medium
  3. Low
  4. No output

Answer: B) Medium

Explanation: Since x is 25, which is greater than 20 but less than 30, the elif block is executed, printing "Medium."

Question 25: Which of the following conditional statements will print "True" when a == 5? (Choose all that apply)

  1. if a == 5:
        print("True")
    
  2. if a == 10:
        print("False")
    elif a == 5:
        print("True")
    
  3. if a != 5:
        print("False")
    else:
        print("True")
    
  4. if a > 5:
        print("False")
    elif a < 5:
        print("False")
    else:
        print("True")
    

Answer: A), B), C), and D)

Explanation: All these statements will print "True" when a equals 5.



Follow us on Facebook and Twitter for latest update.