Python PCEP Exam: Nested if, else, and elif Statements
PCEP Certification Practice Test - Questions, Answers and Explanations
Following comprehensive set of questions and explanations covers the topic of controlling flow with conditional blocks and loops, with a focus on nesting conditional statements. These exercises will help reinforce your understanding and preparation for the PCEP-30-02 examination.
Question 1: What will be the output of the following code?
x = 12 if x > 10: if x < 20: print("x is between 10 and 20") else: print("x is 20 or more")
- x is between 10 and 20
- x is 20 or more
- No output
- x is less than 10
Answer: a) x is between 10 and 20
Explanation: The if conditions check if x is greater than 10 and less than 20, which is true, so "x is between 10 and 20" is printed.
Question 2: Given the following code, what will be printed?
age = 17 if age >= 18: if age >= 21: print("Adult") else: print("Young Adult") else: print("Minor")
- Adult
- Young Adult
- Minor
- No output
Answer: c) Minor
Explanation: The outer if condition (age >= 18) is false, so the else block is executed, printing "Minor".
Question 3: What will be the output of this code snippet?
num = 50 if num >= 50: if num > 100: print("Large number") else: print("Medium number") else: print("Small number")
- Large number
- Medium number
- Small number
- No output
Answer: b) Medium number
Explanation: The outer if condition (num >= 50) is true, so the nested if-else checks whether num > 100. Since this is false, "Medium number" is printed.
Question 4: Consider the following code. Which of the following outputs are possible? (Select all that apply)
temp = 15 if temp > 0: if temp < 10: print("Cold") elif temp < 20: print("Cool") else: print("Warm") else: print("Freezing")
- Cold
- Cool
- Warm
- Freezing
Answer: b) Cool
Explanation: Since temp = 15, the condition temp > 0 is true, and temp < 20 is true, so "Cool" is printed.
Question 5: Which of the following outputs can be generated by this code? (Select all that apply)
score = 85 if score >= 90: print("Excellent") elif score >= 70: if score < 80: print("Good") else: print("Very Good") else: print("Needs Improvement")
- Excellent
- Very Good
- Good
- Needs Improvement
Answer: b) Very Good
Explanation: Since score = 85, the outer elif condition (score >= 70) is true, and the nested if-else checks whether score < 80. Since this is false, "Very Good" is printed.
Question 6: Arrange the following steps in the correct order to evaluate nested if statements:
- If the outer condition is true, evaluate the inner if condition.
- Evaluate the outer if condition.
- Execute the code block associated with the true inner condition.
Answer:
- Evaluate the outer if condition.
- If the outer condition is true, evaluate the inner if condition.
- Execute the code block associated with the true inner condition.
Explanation: The correct order is to first check the outer condition, then the inner condition, and finally execute the corresponding code.
Question 7: In nested if statements, the inner if block is only executed if the outer if condition is ______.
▼Answer: true
Explanation: The inner block of a nested if statement is only executed if the outer condition is true.
Question 8: To check multiple conditions within a single if statement, you can use ______ statements.
▼Answer: nested
Explanation: Nested if statements allow you to check multiple conditions within one if statement structure.
Question 9: Sort the following code snippets to correctly implement nested conditional logic that prints "Child" if age is less than 12, "Teen" if age is between 12 and 18, and "Adult" if age is 18 or more:
- age = 16
- print("Child")
- elif age >= 12:
- if age < 12:
- else:
- if age < 18:
- print("Teen")
- print("Adult")
Answer:
- age = 16
- if age < 12:
- print("Child")
- elif age >= 12:
- if age < 18:
- print("Teen")
- else:
- print("Adult")
Explanation: The nested structure allows checking the age range to determine whether the person is a child, teen, or adult.
Question 10: Fill in the missing code to check if x is both positive and even.
if x > 0: if ______: print("Positive and even")▼
Answer: x % 2 == 0
Explanation: The nested if checks if x is positive and even by evaluating x % 2 == 0.
Question 11: Fill in the missing code to print "High Score" if score is greater than 80 and "Excellent" if it is greater than 90.
if score > 80: print("High Score") if score > 90: print("______")▼
Answer: Excellent
Explanation: The code checks if score is greater than 90 to print "Excellent" after confirming it is greater than 80.
Question 12: Insert the correct condition to print "Prime" if num is greater than 1 and also meets a secondary condition.
if num > 1: if ______: print("Prime")▼
Answer: (num % 2 != 0 and num % 3 != 0)
Explanation: The condition checks if num is greater than 1 and is not divisible by 2 or 3, indicating it might be prime.
Question 13: Insert the appropriate condition to print "Valid Password" if password length is greater than 8 and contains a special character.
if len(password) > 8: if any(char in '!@#$%^&*()' for char in password): print("______")▼
Answer: Valid Password
Explanation: The condition checks the password length and content to validate it as a "Valid Password.".
Question 14: What will be the output of the following code?
y = 25 if y > 20: if y > 30: print("Above 30") else: print("Between 20 and 30")
- Above 30
- Between 20 and 30
- No output
- Below 20
Answer: b) Between 20 and 30
Explanation: Since y = 25, the outer if condition (y > 20) is true, and the nested if-else checks whether y > 30. Since this is false, "Between 20 and 30" is printed.
Question 15: What will be the output of this code snippet?
num = 3 if num > 0: if num % 2 == 0: print("Even") else: print("Odd")
- Even
- Odd
- No output
- Error
Answer: b) Odd
Explanation: Since num = 3, the outer if condition (num > 0) is true, and the nested if-else checks whether num % 2 == 0. Since this is false, "Odd" is printed.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics