AP Computer Science Principles Quiz: Conditionals
20 questions · exam conditions
0:00
ConditionalsQuestion 1 of 20

Which of the following Boolean expressions would be used in a conditional statement to check if a number is between 10 and 20, inclusive?

(number > 10) AND (number < 20)
(number >= 10) AND (number <= 20)
(number > 10) OR (number < 20)
(number >= 10) OR (number <= 20)
← Back to quizzes

AP Computer Science Principles Quiz

AP Computer Science Principles Quiz: Conditionals

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.

What this quiz covers

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.

How to use this quiz

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.

All questions

Question 1

Which of the following Boolean expressions would be used in a conditional statement to check if a number is between 10 and 20, inclusive?

  1. (number > 10) AND (number < 20)
  2. (number >= 10) AND (number <= 20) (correct answer)
  3. (number > 10) OR (number < 20)
  4. (number >= 10) OR (number <= 20)

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.

Question 2

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?

  1. This code will execute faster than an IF-ELSE statement in all cases
  2. This code checks both conditions regardless of the first result, while IF-ELSE skips the second check (correct answer)
  3. This code will produce different output than an IF-ELSE statement for some temperature values
  4. This code will always display both messages, while IF-ELSE displays only one message

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.

Question 3

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") } }

  1. x is greater
  2. y is greater (correct answer)
  3. equal
  4. No output will be displayed

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.

Question 4

In a conditional statement, what happens if the Boolean expression in the IF condition evaluates to false and there is no ELSE clause?

  1. The program will display an error message and terminate execution
  2. The program will automatically execute a default set of statements
  3. The program will skip the IF block and continue with the next statement after the IF block (correct answer)
  4. The program will repeatedly evaluate the condition until it becomes true

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.

Question 5

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") } } }

  1. A
  2. B (correct answer)
  3. C
  4. 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.

Question 6

Which of the following conditions would be true when temperature = 75 and humidity = 60?

  1. (temperature > 80) AND (humidity > 70)
  2. (temperature < 80) OR (humidity < 50)
  3. (temperature >= 70) AND (humidity <= 65) (correct answer)
  4. (temperature = 75) OR (humidity = 70)

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.

Question 7

Consider this code structure:

IF(condition1) { statement1 } ELSE { IF(condition2) { statement2 } ELSE { statement3 } }

Under what circumstances will statement3 execute?

  1. When condition1 is true and condition2 is false
  2. When condition1 is false and condition2 is true
  3. When condition1 is false and condition2 is false (correct answer)
  4. When both condition1 and condition2 are true

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.

Question 8

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?

  1. Quadrant I
  2. Quadrant IV (correct answer)
  3. Not in Quadrant I or IV
  4. No output will be displayed

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.

Question 9

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") }

  1. Only when score is exactly 90
  2. When score is 90 or higher (correct answer)
  3. When score is between 80 and 89, inclusive
  4. When score is between 70 and 89, inclusive

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".

Question 10

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") } }

  1. Even
  2. Multiple of 5 (correct answer)
  3. Odd
  4. Both Even and Multiple of 5

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.

Question 11

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") } } }

  1. 1 (correct answer)
  2. 2
  3. 3
  4. 4

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.

Question 12

What is the primary difference between these two code structures?

Structure 1: IF(condition) { statement1 } statement2

Structure 2: IF(condition) { statement1 } ELSE { statement2 }

  1. Structure 1 always executes both statements, while Structure 2 executes only one statement
  2. Structure 1 executes statement2 regardless of the condition, while Structure 2 executes statement2 only when condition is false (correct answer)
  3. Structure 1 is faster to execute than Structure 2 in all programming languages
  4. Structure 1 requires the condition to be true for statement2 to execute, while Structure 2 does not

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.

Question 13

In the given scenario, an online store applies discounts using this pseudo-code: IF membershipLevel == "premium" THEN finalPrice = price * 0.80 ELSE finalPrice = price. Which branch of the condition will execute if membershipLevel is "premium"?

  1. The ELSE branch executes; no discount applies.
  2. Neither branch executes; finalPrice stays unset.
  3. The IF branch executes; 20% discount applies. (correct answer)
  4. Both branches execute; finalPrice is overwritten.

Explanation: This question tests understanding of conditionals in AP Computer Science Principles, specifically how logical branches determine program flow. Conditionals allow programs to execute different actions based on whether a condition is true or false. They use 'if', 'else if', and 'else' statements to control flow. Choice C is correct because when membershipLevel equals "premium", the condition in the IF statement evaluates to true, causing the IF branch to execute and apply the 20% discount (finalPrice = price * 0.80). Choice A is incorrect because it assumes the ELSE branch runs when the condition is true, which shows a fundamental misunderstanding of how IF-ELSE logic works. To help students: Use flowcharts to visualize the decision path and trace through examples with specific values. Emphasize that only one branch executes in an IF-ELSE structure - never both.

Question 14

In the given scenario, a weather app uses: IF weatherCondition=="sunny" suggest="park" ELSE IF weatherCondition=="rainy" suggest="museum" ELSE suggest="stay home". What will happen if weatherCondition is "cloudy"?

  1. It suggests "park" because cloudy counts as sunny.
  2. It suggests "museum" because cloudy implies rain.
  3. It suggests "stay home" using the ELSE branch. (correct answer)
  4. It suggests nothing because no condition matches.

Explanation: This question tests understanding of conditionals in AP Computer Science Principles, specifically how ELSE clauses handle cases when no previous conditions match. Conditionals evaluate each condition sequentially, and the ELSE clause serves as a catch-all for any input not matching previous conditions. Choice C is correct because when weatherCondition is "cloudy", it doesn't match "sunny" or "rainy", so the program executes the ELSE branch and suggests "stay home". Choice B is incorrect because it assumes the program makes inferences about weather relationships, but conditionals only check exact matches. To help students: Emphasize that string comparisons require exact matches and that ELSE handles all unspecified cases. Create exercises with various inputs to show how ELSE acts as a default option.

Question 15

In the given scenario, an online store uses: IF isPremium==true OR cartTotal>=100 discount=0.20 ELSE discount=0.00. Which branch of the condition will execute if isPremium is false and cartTotal is 120?

  1. The IF branch executes; discount becomes 0.20. (correct answer)
  2. The ELSE branch executes; discount becomes 0.00.
  3. No branch executes because both must be true.
  4. The ELSE branch executes; cartTotal overrides OR.

Explanation: This question tests understanding of conditionals in AP Computer Science Principles, specifically how the OR logical operator works in compound conditions. The OR operator makes a condition true if at least one of its parts is true, allowing multiple paths to the same outcome. Choice A is correct because even though isPremium is false, cartTotal (120) is greater than or equal to 100, making the overall condition true due to the OR operator, so the IF branch executes and discount becomes 0.20. Choice D is incorrect because it misunderstands how OR works - any true condition makes the whole expression true. To help students: Use truth tables for OR operations and practice evaluating compound conditions step by step. Emphasize that OR needs only one true condition, while AND needs all conditions true.

Question 16

Based on the code snippet, a weather app uses: IF temperature>75 AND weather=="sunny" suggest="beach" ELSE suggest="cafe". Which branch of the condition will execute if temperature is 80 and weather is "cloudy"?

  1. The IF branch executes; it suggests "beach".
  2. The ELSE branch executes; it suggests "cafe". (correct answer)
  3. The IF branch executes because one condition is true.
  4. No suggestion is made because AND cancels output.

Explanation: This question tests understanding of conditionals in AP Computer Science Principles, specifically how the AND logical operator requires all conditions to be true. The AND operator creates a compound condition that is only true when every individual condition is satisfied. Choice B is correct because although temperature (80) is greater than 75, weather is "cloudy" not "sunny", so the overall condition is false (true AND false = false), causing the ELSE branch to execute and suggest "cafe". Choice C is incorrect because it assumes one true condition is sufficient, confusing AND with OR logic. To help students: Create truth tables showing AND requires all conditions true and use real-world analogies like "you need money AND time to go on vacation". Practice tracing compound conditions with mixed true/false values.

Question 17

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'"?

  1. IF(length >= 8 OR (hasLetters AND hasNumbers)) { DISPLAY("Strong") } ELSE { DISPLAY("Weak") }
  2. IF(length >= 8 AND hasLetters OR hasNumbers) { DISPLAY("Strong") } ELSE { DISPLAY("Weak") }
  3. IF(length >= 8 AND (hasLetters AND hasNumbers)) { DISPLAY("Strong") } ELSE { DISPLAY("Weak") } (correct answer)
  4. IF(length >= 8 AND hasLetters AND hasNumbers OR true) { DISPLAY("Strong") } ELSE { 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.

Question 18

Which of the following code segments correctly implements the logic: "If it's raining AND cold, wear a coat. Otherwise, wear a jacket"?

  1. IF(raining OR cold) { DISPLAY("wear a coat") } ELSE { DISPLAY("wear a jacket") }
  2. IF(raining AND cold) { DISPLAY("wear a coat") } ELSE { DISPLAY("wear a jacket") } (correct answer)
  3. IF(raining) { IF(cold) { DISPLAY("wear a jacket") } } ELSE { DISPLAY("wear a coat") }
  4. IF(NOT(raining AND cold)) { DISPLAY("wear a coat") } ELSE { DISPLAY("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.

Question 19

Which of the following code segments will display "Pass" only when BOTH test1 and test2 scores are greater than 70?

  1. IF(test1 > 70) { IF(test2 > 70) { DISPLAY("Pass") } } (correct answer)
  2. IF(test1 > 70 OR test2 > 70) { DISPLAY("Pass") }
  3. IF(test1 > 70) { DISPLAY("Pass") } IF(test2 > 70) { DISPLAY("Pass") }
  4. IF(test1 > 70 AND test2 <= 70) { DISPLAY("Pass") }

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.

Question 20

What will be the output of the following code segment when age is 16?

IF(age >= 18) { DISPLAY("Adult") } ELSE { DISPLAY("Minor") }

  1. Adult
  2. Minor (correct answer)
  3. Both Adult and Minor will be displayed
  4. No output will be produced

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.