w3resource

Python PCEM Exam: Conditional Statements (If, If-Else, If-Elif-Else)

PCEP Certification Practice Test - Questions, Answers and Explanations

Below set of questions and explanations covers the topic of controlling flow with conditional blocks and loops, with a focus on multiple conditional statements. These exercises will help reinforce your understanding and preparation for the PCEP-30-2 examination.

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

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

Answer: a) x is between 5 and 15

Explanation: The if conditions check if x is greater than 5 and less than 15, which is true, so the first print() statement is executed.

Question 2: Given the following code, what will be printed?

a = 20
b = 30
if a > 10:
    if b > 25:
        print("Condition met")
    else:
        print("Condition not met")
  1. Condition met
  2. Condition not met
  3. No output
  4. Error

Answer: a) Condition met

Explanation: The if conditions check if a is greater than 10 (true) and b is greater than 25 (true), so "Condition met" is printed.

Question 3: What will be the output of this code snippet?

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

Answer: a) n is 5

Explanation: Since n == 5, the first condition is true, so "n is 5" is printed.

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

x = 7
if x > 10:
    print("Greater than 10")
elif x == 7:
    print("Equal to 7")
else:
    print("Less than 10")
  1. Greater than 10
  2. Equal to 7
  3. Less than 10
  4. Error

Answer: b) Equal to 7

Explanation: The condition x == 7 is true, so "Equal to 7" is printed.

Question 5: Which of the following outputs can be generated by this code? (Select all that apply)

num = 15
if num < 10:
    print("Less than 10")
elif num < 20:
    print("Less than 20")
else:
    print("20 or more")
  1. Less than 10
  2. Less than 20
  3. 20 or more

Answer: b) Less than 20

Explanation: Since num = 15, the second condition num < 20 is true, so "Less than 20" is printed.

Question 6: Consider the following code. Which of the following outputs are possible? (Select all that apply)

x = 25
if x < 20:
    print("x is less than 20")
elif x > 20 and x < 30:
    print("x is between 20 and 30")
else:
    print("x is 30 or more")
  1. x is less than 20
  2. x is between 20 and 30
  3. x is 30 or more

Answer: b) x is between 20 and 30

Explanation: The condition x > 20 and x < 30 is true for x = 25, so "x is between 20 and 30" is printed.

Question 7: Arrange the following steps in the correct order for evaluating multiple conditions in a nested if statement:

  • If the first condition is true, evaluate the nested condition.
  • Check if the first condition is true.
  • If the nested condition is true, execute the corresponding code block.

Answer:

  • Check if the first condition is true.
  • If the first condition is true, evaluate the nested condition.
  • If the nested condition is true, execute the corresponding code block.

Explanation: The nested if statements are evaluated sequentially, with the outer condition checked first, followed by the inner condition.

Question 8: In a multiple if-elif-else conditional block, if the ______ condition is true, the corresponding code block is executed, and the subsequent conditions are ignored.

Answer: if

Explanation: In an if-elif-else block, only the first true condition's code block is executed; subsequent conditions are ignored..

Question 9: The ______ statement is used to handle the case when none of the preceding if or elif conditions are met.

Answer: else

Explanation: The else statement is used to execute code when none of the if or elif conditions are true.

Question 10: Sort the following code snippets to correctly implement a decision structure that prints "Valid" if a number is between 1 and 10 (inclusive) and "Invalid" otherwise:

  • if number >= 1:
  • number = 5
  • print("Valid")
  • if number <= 10:
  • else:
  • print("Invalid")

Answer:

  • number = 5
  • if number >= 1:
  • if number <= 10:
  • print("Valid")
  • else:
  • print("Invalid")

Explanation: The nested if conditions ensure that the number is between 1 and 10 before printing "Valid".

Question 11: Fill in the missing code to check if a variable x is both greater than 10 and less than 20.

if x > 10 and ______:
    print("x is between 10 and 20")

Answer: x < 20

Explanation: The condition checks if x is greater than 10 and less than 20.

Question 12: Fill in the missing code to print "Even" if the variable num is divisible by 2 and "Odd" otherwise.

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

Answer: Odd

Explanation: The else statement handles the case where num is not divisible by 2, indicating it's odd.

Question 13: Insert the correct condition to print "Equal" if a is equal to b.

if a ______ b:
    print("Equal")

Answer: ==

Explanation: The == operator checks for equality between a and b.

Question 14: Insert the appropriate condition to print "Positive" if num is greater than 0, otherwise print "Negative".

if num > 0:
    print("Positive")
else:
    print("______")

Answer: Negative

Explanation: The else statement handles the case where num is not greater than 0, indicating it's negative.

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

x = 8
if x < 5:
    print("Less than 5")
elif x < 10:
    print("Less than 10")
else:
    print("10 or more")
  1. Less than 5
  2. Less than 10
  3. 10 or more
  4. No output

Answer: b) Less than 10

Explanation: Since x = 8, the second condition x < 10 is true, so "Less than 10" is printed.

Question 16: What will be the output of this code snippet?

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

Answer: b) Grade B

Explanation: Since score = 85, the condition score >= 80 is true, so "Grade B" is printed.



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/control-flow-conditional-statements-multiple-conditional-statements.php