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")
- x is between 5 and 15
- x is greater than or equal to 15
- x is less than or equal to 5
- 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")
- Condition met
- Condition not met
- No output
- 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")
- n is 5
- n is greater than 5
- n is less than 5
- 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")
- Greater than 10
- Equal to 7
- Less than 10
- 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")
- Less than 10
- Less than 20
- 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")
- x is less than 20
- x is between 20 and 30
- 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")
- Less than 5
- Less than 10
- 10 or more
- 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")
- Grade A
- Grade B
- Grade C
- No output
Answer: b) Grade B
Explanation: Since score = 85, the condition score >= 80 is true, so "Grade B" is printed.
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
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics