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