Which of the following code segments will output "Pass" if the variable score is greater than or equal to 70, and "Fail" otherwise?
Opening subject page...
Loading your content
AP Computer Science Principles Quiz
Practice Conditionals in AP Computer Science Principles with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.
Question 1 / 20
0 of 20 answered
Which of the following code segments will output "Pass" if the variable score is greater than or equal to 70, and "Fail" otherwise?
This quiz focuses on Conditionals, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science Principles.
Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.
Which of the following code segments will output "Pass" if the variable score is greater than or equal to 70, and "Fail" otherwise?
Explanation: Choice A correctly uses the >= operator to check if score is greater than or equal to 70, which matches the requirement. Choice B uses > which would exclude exactly 70. Choice C uses = which only checks for equality to 70. Choice D uses <= which would display "Pass" for scores 70 and below, which is the opposite of what's needed.
Which of the following Boolean expressions would be used in a conditional statement to check if a number is between 10 and 20, inclusive?
Explanation: Choice B correctly uses AND with >= and <= to include both 10 and 20 in the range. Choice A excludes the endpoints 10 and 20 using > and <. Choice C uses OR which would be true for almost all numbers (any number less than 20 OR greater than 10). Choice D also uses OR incorrectly and would be true for most numbers.
Consider the following code segment:
IF(temperature > 80) { DISPLAY("Hot") } IF(temperature <= 80) { DISPLAY("Not Hot") }
What is the difference between this code and using an IF-ELSE statement instead?
Explanation: The given code uses two separate IF statements, so both conditions are always evaluated. An IF-ELSE statement would skip the second condition if the first is true. Choice A is incorrect about execution speed. Choice C is incorrect because the output would be the same. Choice D is incorrect because only one message displays since the conditions are mutually exclusive.
What will be displayed by the following code when x = 5 and y = 10?
IF(x > y) { DISPLAY("x is greater") } ELSE { IF(x < y) { DISPLAY("y is greater") } ELSE { DISPLAY("equal") } }
Explanation: With x = 5 and y = 10, the first condition (x > y) is false, so the ELSE block executes. In the nested IF, the condition (x < y) is true since 5 < 10, so "y is greater" is displayed. Choice A is incorrect because 5 is not greater than 10. Choice C is incorrect because the values are not equal. Choice D is incorrect because the nested IF condition is true.
Which of the following represents a valid conditional statement structure in the AP Computer Science Principles reference sheet notation?
Explanation: Choice B uses the correct IF-ELSE syntax as specified in the AP Computer Science Principles reference sheet. Choices A, C, and D use non-standard keywords (WHEN/OTHERWISE, CHECK/ALTERNATE, CONDITION/FALSE) that are not part of the official AP CSP notation.
Which of the following Boolean expressions is equivalent to the condition used in this IF statement?
IF(NOT(age < 18)) { DISPLAY("Can vote") }
Explanation: NOT(age < 18) is equivalent to age >= 18. When age < 18 is false, it means age is not less than 18, which is the same as age >= 18. Choice A excludes age = 18. Choice C only includes age = 18. Choice D represents age <= 18, which is the opposite condition.
In a conditional statement, what happens if the Boolean expression in the IF condition evaluates to false and there is no ELSE clause?
Explanation: When an IF condition is false and there's no ELSE clause, the program simply skips the IF block and continues executing the next statement after the IF block. Choice A is incorrect because this is normal behavior, not an error. Choice B is incorrect because there are no default statements. Choice D is incorrect because the condition is evaluated only once.
What is the output of the following code when grade = 85?
IF(grade >= 90) { DISPLAY("A") } ELSE { IF(grade >= 80) { DISPLAY("B") } ELSE { IF(grade >= 70) { DISPLAY("C") } ELSE { DISPLAY("F") } } }
Explanation: With grade = 85, the first condition (grade >= 90) is false, so we go to the first ELSE. The nested condition (grade >= 80) is true since 85 >= 80, so "B" is displayed. Choice A is incorrect because 85 < 90. Choice C is incorrect because we don't reach the third nested condition. Choice D is incorrect because the second condition is satisfied.
Which of the following conditions would be true when temperature = 75 and humidity = 60?
Explanation: With temperature = 75 and humidity = 60, let's evaluate each choice. Choice A: (75 > 80) is false AND (60 > 70) is false, so the entire expression is false. Choice B: (75 < 80) is true OR (60 < 50) is false, so this evaluates to true. Choice C: (75 >= 70) is true AND (60 <= 65) is true, so the entire expression is true. Choice D: (75 = 75) is true OR (60 = 70) is false, so this evaluates to true. Both B, C, and D are true, but C is the best answer as it demonstrates proper AND logic with both conditions being satisfied.
Consider this code structure:
IF(condition1) { statement1 } ELSE { IF(condition2) { statement2 } ELSE { statement3 } }
Under what circumstances will statement3 execute?
Explanation: Statement3 will execute when condition1 is false (so we enter the outer ELSE) AND condition2 is false (so we enter the inner ELSE where statement3 is located). Choice A is incorrect because if condition1 is true, we never reach the nested structure. Choice B is incorrect because if condition2 is true, statement2 executes instead. Choice D is incorrect because if condition1 is true, we execute statement1.
What will be the output of the following code segment when age is 16?
IF(age >= 18) { DISPLAY("Adult") } ELSE { DISPLAY("Minor") }
Explanation: When age is 16, the condition (age >= 18) evaluates to false since 16 is not greater than or equal to 18. Therefore, the ELSE block executes, displaying "Minor". Choice A is incorrect because the condition is false. Choice C is incorrect because only one block executes in an IF-ELSE statement. Choice D is incorrect because the ELSE block will execute.
Consider the following code segment:
IF(x > 0) { IF(y > 0) { DISPLAY("Quadrant I") } ELSE { DISPLAY("Quadrant IV") } } ELSE { DISPLAY("Not in Quadrant I or IV") }
What will be displayed when x = 3 and y = -2?
Explanation: With x = 3 and y = -2, the outer condition (x > 0) is true since 3 > 0, so we enter the outer IF block. The inner condition (y > 0) is false since -2 is not > 0, so the inner ELSE block executes, displaying "Quadrant IV". Choice A is incorrect because y is negative. Choice C is incorrect because x is positive. Choice D is incorrect because a block will execute.
In the following code segment, under what condition will "Grade A" be displayed?
IF(score >= 90) { DISPLAY("Grade A") } IF(score >= 80) { DISPLAY("Grade B") } IF(score >= 70) { DISPLAY("Grade C") }
Explanation: "Grade A" will be displayed when score >= 90, which means score is 90 or higher. Note that this code structure allows multiple grades to be displayed for the same score. Choice A is incorrect because >= includes all values 90 and above. Choices C and D describe conditions for other grade displays but not specifically for "Grade A".
What will be displayed by the following code when x = 10?
IF(x > 5) { DISPLAY("Large") IF(x > 15) { DISPLAY(" and Very Large") } } ELSE { DISPLAY("Small") }
Explanation: When x = 10, the condition (x > 5) is true since 10 > 5, so we enter the IF block and display "Large". Then we check the nested condition (x > 15), which is false since 10 is not > 15, so " and Very Large" is not displayed. Choice B is incorrect because the nested condition is false. Choice C is incorrect because x > 5. Choice D is incorrect because only "Large" is displayed.
Which of the following code segments correctly implements the logic "If the password length is at least 8 characters AND contains both letters and numbers, display 'Strong', otherwise display 'Weak'"?
Explanation: Choice C correctly requires ALL three conditions: length >= 8 AND hasLetters AND hasNumbers. The parentheses clarify that both hasLetters and hasNumbers must be true. Choice A uses OR between length and the other conditions, which is too lenient. Choice B lacks proper parentheses and could be misinterpreted due to operator precedence. Choice D includes "OR true" which would make the entire condition always true.
What will be the result of executing this code when num = 15?
IF(num MOD 2 = 0) { DISPLAY("Even") } ELSE { IF(num MOD 5 = 0) { DISPLAY("Multiple of 5") } ELSE { DISPLAY("Odd") } }
Explanation: When num = 15, first we check if 15 MOD 2 = 0. Since 15 MOD 2 = 1, this is false, so we go to the ELSE block. In the nested IF, we check if 15 MOD 5 = 0. Since 15 MOD 5 = 0, this is true, so "Multiple of 5" is displayed. Choice A is incorrect because 15 is odd. Choice C is incorrect because we don't reach that ELSE. Choice D is incorrect because only one path executes.
Which of the following code segments correctly implements the logic: "If it's raining AND cold, wear a coat. Otherwise, wear a jacket"?
Explanation: Choice B correctly uses AND to check both conditions and displays "wear a coat" only when both are true. Choice A uses OR which would recommend a coat if either condition is true. Choice C has the outputs reversed and uses nested IFs unnecessarily. Choice D uses NOT which reverses the logic, recommending a coat when the conditions are not both true.
Which of the following code segments will display "Pass" only when BOTH test1 and test2 scores are greater than 70?
Explanation: Choice A uses nested IF statements which ensures both conditions must be true - test1 > 70 AND test2 > 70. Choice B uses OR, which would display "Pass" if either test is above 70. Choice C uses separate IF statements that could display "Pass" twice or once depending on the scores. Choice D requires test1 > 70 but test2 <= 70, which is not what we want.
In the following code segment, what is the minimum number of conditions that must be evaluated when score = 95?
IF(score >= 90) { DISPLAY("Excellent") } ELSE { IF(score >= 80) { DISPLAY("Good") } ELSE { IF(score >= 70) { DISPLAY("Fair") } ELSE { DISPLAY("Poor") } } }
Explanation: When score = 95, only the first condition (score >= 90) needs to be evaluated. Since 95 >= 90 is true, "Excellent" is displayed and the program skips all the nested conditions in the ELSE block. Choice B, C, and D are incorrect because the IF-ELSE structure ensures that once a true condition is found, no further conditions in that chain need to be evaluated.
What is the primary difference between these two code structures?
Structure 1: IF(condition) { statement1 } statement2
Structure 2: IF(condition) { statement1 } ELSE { statement2 }
Explanation: In Structure 1, statement2 is not part of the conditional and always executes. In Structure 2, statement2 is in the ELSE block and only executes when the condition is false. Choice A is incorrect because Structure 1 doesn't always execute statement1. Choice C makes an unsupported claim about execution speed. Choice D incorrectly describes Structure 1's behavior.