Conditionals - AP Computer Science Principles
Card 1 of 30
What is short-circuit evaluation in conditional logic?
What is short-circuit evaluation in conditional logic?
Tap to reveal answer
Evaluation stops once the result is determined. Optimizes performance by avoiding unnecessary evaluations.
Evaluation stops once the result is determined. Optimizes performance by avoiding unnecessary evaluations.
← Didn't Know|Knew It →
When is a switch statement preferred over multiple if-else?
When is a switch statement preferred over multiple if-else?
Tap to reveal answer
When evaluating a single variable against constants. Switch is cleaner for testing one variable against many values.
When evaluating a single variable against constants. Switch is cleaner for testing one variable against many values.
← Didn't Know|Knew It →
Identify the error: if (x > 0) { return true; } return false;
Identify the error: if (x > 0) { return true; } return false;
Tap to reveal answer
No error; the logic is correct. Valid pattern: if returns early, else is implicit.
No error; the logic is correct. Valid pattern: if returns early, else is implicit.
← Didn't Know|Knew It →
Which keyword is used to break out of a switch statement?
Which keyword is used to break out of a switch statement?
Tap to reveal answer
break. Prevents fall-through to subsequent case statements.
break. Prevents fall-through to subsequent case statements.
← Didn't Know|Knew It →
How can you check for both conditions A and B being true?
How can you check for both conditions A and B being true?
Tap to reveal answer
A && B. AND requires both conditions to be true simultaneously.
A && B. AND requires both conditions to be true simultaneously.
← Didn't Know|Knew It →
Identify the error: if (x < 10); return x;
Identify the error: if (x < 10); return x;
Tap to reveal answer
Remove the semicolon after the if condition. Empty statement creates unintended logic flow.
Remove the semicolon after the if condition. Empty statement creates unintended logic flow.
← Didn't Know|Knew It →
What does the logical NOT operator (!) do?
What does the logical NOT operator (!) do?
Tap to reveal answer
Inverts the truth value of a boolean expression. Flips true to false and false to true.
Inverts the truth value of a boolean expression. Flips true to false and false to true.
← Didn't Know|Knew It →
What is the outcome if the condition in a ternary operator is false?
What is the outcome if the condition in a ternary operator is false?
Tap to reveal answer
The falseExpression is executed. Ternary operator returns the false branch value.
The falseExpression is executed. Ternary operator returns the false branch value.
← Didn't Know|Knew It →
In Java, what is the default case in a switch statement?
In Java, what is the default case in a switch statement?
Tap to reveal answer
Executed if no other case matches. Handles cases not explicitly covered by other cases.
Executed if no other case matches. Handles cases not explicitly covered by other cases.
← Didn't Know|Knew It →
How do you check if either condition A or B is true?
How do you check if either condition A or B is true?
Tap to reveal answer
A || B. OR returns true if any condition is satisfied.
A || B. OR returns true if any condition is satisfied.
← Didn't Know|Knew It →
What is the result of an else block when the if condition is true?
What is the result of an else block when the if condition is true?
Tap to reveal answer
The else block is skipped. True conditions bypass else blocks entirely.
The else block is skipped. True conditions bypass else blocks entirely.
← Didn't Know|Knew It →
What is the significance of nesting if-else statements?
What is the significance of nesting if-else statements?
Tap to reveal answer
Allows complex conditional logic. Enables sophisticated decision trees and branching logic.
Allows complex conditional logic. Enables sophisticated decision trees and branching logic.
← Didn't Know|Knew It →
What does a conditional statement evaluate to?
What does a conditional statement evaluate to?
Tap to reveal answer
A boolean value (true or false). All conditional expressions result in boolean values.
A boolean value (true or false). All conditional expressions result in boolean values.
← Didn't Know|Knew It →
Identify the error: if (x < 0) { return negative; }
Identify the error: if (x < 0) { return negative; }
Tap to reveal answer
Ensure 'negative' is a defined variable or value. Undefined variables cause compilation errors.
Ensure 'negative' is a defined variable or value. Undefined variables cause compilation errors.
← Didn't Know|Knew It →
What is a compound conditional statement?
What is a compound conditional statement?
Tap to reveal answer
Combines multiple conditions with logical operators. Uses AND, OR, or NOT to link multiple boolean expressions.
Combines multiple conditions with logical operators. Uses AND, OR, or NOT to link multiple boolean expressions.
← Didn't Know|Knew It →
Identify the error: if (x < 7) { return true }
Identify the error: if (x < 7) { return true }
Tap to reveal answer
Missing semicolon after 'return true'. All statements in Java must end with a semicolon.
Missing semicolon after 'return true'. All statements in Java must end with a semicolon.
← Didn't Know|Knew It →
How do you check if two variables x and y are equal?
How do you check if two variables x and y are equal?
Tap to reveal answer
x == y. Double equals performs comparison, not assignment.
x == y. Double equals performs comparison, not assignment.
← Didn't Know|Knew It →
What is the purpose of an if statement in programming?
What is the purpose of an if statement in programming?
Tap to reveal answer
To execute code based on a condition being true. Conditionals control program flow based on boolean evaluations.
To execute code based on a condition being true. Conditionals control program flow based on boolean evaluations.
← Didn't Know|Knew It →
Which keyword is used to create an alternative condition in Java?
Which keyword is used to create an alternative condition in Java?
Tap to reveal answer
else. Provides an alternative path when the if condition is false.
else. Provides an alternative path when the if condition is false.
← Didn't Know|Knew It →
Identify the error: if (x = 5) { return true; }
Identify the error: if (x = 5) { return true; }
Tap to reveal answer
Replace '=' with '==' for comparison. Assignment operator '=' assigns value; '==' compares values.
Replace '=' with '==' for comparison. Assignment operator '=' assigns value; '==' compares values.
← Didn't Know|Knew It →
What does an else if statement allow in conditional logic?
What does an else if statement allow in conditional logic?
Tap to reveal answer
Multiple conditions to be checked sequentially. Enables testing additional conditions when previous ones fail.
Multiple conditions to be checked sequentially. Enables testing additional conditions when previous ones fail.
← Didn't Know|Knew It →
How do you denote 'not equal to' in Java?
How do you denote 'not equal to' in Java?
Tap to reveal answer
!=. Comparison operator that returns true when values differ.
!=. Comparison operator that returns true when values differ.
← Didn't Know|Knew It →
Which operator is used for logical AND in Java?
Which operator is used for logical AND in Java?
Tap to reveal answer
&&. Returns true only when both operands are true.
&&. Returns true only when both operands are true.
← Didn't Know|Knew It →
Identify the error: if (x > 10) else { return false; }
Identify the error: if (x > 10) else { return false; }
Tap to reveal answer
Remove 'else' or associate it with a preceding 'if'. Every 'else' must be paired with a preceding 'if' statement.
Remove 'else' or associate it with a preceding 'if'. Every 'else' must be paired with a preceding 'if' statement.
← Didn't Know|Knew It →
What is the result of a condition that evaluates to false?
What is the result of a condition that evaluates to false?
Tap to reveal answer
The code block is not executed. False conditions cause the associated code block to be skipped.
The code block is not executed. False conditions cause the associated code block to be skipped.
← Didn't Know|Knew It →
How is a conditional expression structured in Java?
How is a conditional expression structured in Java?
Tap to reveal answer
condition ? trueExpression : falseExpression. Ternary operator provides a compact if-else alternative.
condition ? trueExpression : falseExpression. Ternary operator provides a compact if-else alternative.
← Didn't Know|Knew It →
What keyword marks the end of an if-else statement block?
What keyword marks the end of an if-else statement block?
Tap to reveal answer
There is no specific keyword; it's marked by braces. Java uses braces to define block boundaries, not keywords.
There is no specific keyword; it's marked by braces. Java uses braces to define block boundaries, not keywords.
← Didn't Know|Knew It →
What does '==' operator check in Java?
What does '==' operator check in Java?
Tap to reveal answer
Equality of two values. Comparison operator that tests for value equivalence.
Equality of two values. Comparison operator that tests for value equivalence.
← Didn't Know|Knew It →
Identify the error: if (a == b) { return a } else { return b }
Identify the error: if (a == b) { return a } else { return b }
Tap to reveal answer
Missing semicolons after 'return a' and 'return b'. Return statements require semicolons to terminate properly.
Missing semicolons after 'return a' and 'return b'. Return statements require semicolons to terminate properly.
← Didn't Know|Knew It →
Which keyword allows an action when all if conditions are false?
Which keyword allows an action when all if conditions are false?
Tap to reveal answer
else. Catches all cases where if conditions evaluate to false.
else. Catches all cases where if conditions evaluate to false.
← Didn't Know|Knew It →