Conditionals - AP Computer Science Principles

  1. What is short-circuit evaluation in conditional logic?
    Evaluation stops once the result is determined. Optimizes performance by avoiding unnecessary evaluations.
  2. Which keyword is used to create an alternative condition in Java?
    else. Provides an alternative path when the if condition is false.
  3. 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.
  4. How is a conditional expression structured in Java?
    condition ? trueExpression : falseExpression. Ternary operator provides a compact if-else alternative.
  5. What does an else if statement allow in conditional logic?
    Multiple conditions to be checked sequentially. Enables testing additional conditions when previous ones fail.
  6. What is the significance of nesting if-else statements?
    Allows complex conditional logic. Enables sophisticated decision trees and branching logic.
  7. Which operator is used for logical AND in Java?
    &&. Returns true only when both operands are true.
  8. Identify the error: if (x = 5) { return true; }
    Replace '=' with '==' for comparison. Assignment operator '=' assigns value; '==' compares values.
  9. 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.
  10. Identify the error: if (x > 0) { return true; } return false;
    No error; the logic is correct. Valid pattern: if returns early, else is implicit.
  11. 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.
  12. What does a conditional statement evaluate to?
    A boolean value (true or false). All conditional expressions result in boolean values.
  13. How do you denote 'not equal to' in Java?
    !=. Comparison operator that returns true when values differ.
  14. What is the outcome if the condition in a ternary operator is false?
    The falseExpression is executed. Ternary operator returns the false branch value.
  15. Which operator is used for logical OR in Java?
    ||. Returns true when at least one operand is true.
  16. Identify the error: if (x = 10) { y = 5; }
    Replace '=' with '==' for comparison. Assignment in condition should be comparison instead.
  17. What does the logical NOT operator (!) do?
    Inverts the truth value of a boolean expression. Flips true to false and false to true.
  18. 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.
  19. 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.
  20. Which keyword is used to break out of a switch statement?
    break. Prevents fall-through to subsequent case statements.
  21. 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.
  22. 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.
  23. Identify the error: if (x < 0) { return negative; }
    Ensure 'negative' is a defined variable or value. Undefined variables cause compilation errors.
  24. What is the purpose of nested if statements?
    To evaluate multiple conditions within each other. Inner conditions depend on outer conditions being true.
  25. Which keyword allows an action when all if conditions are false?
    else. Catches all cases where if conditions evaluate to false.
  26. How do you check if two variables x and y are equal?
    x == y. Double equals performs comparison, not assignment.
  27. 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.
  28. What type of value must an if statement condition evaluate to?
    Boolean. Conditions must resolve to true or false values.
  29. What does '==' operator check in Java?
    Equality of two values. Comparison operator that tests for value equivalence.
  30. What is a compound conditional statement?
    Combines multiple conditions with logical operators. Uses AND, OR, or NOT to link multiple boolean expressions.
  31. Identify the error: if (x < 7) { return true }
    Missing semicolon after 'return true'. All statements in Java must end with a semicolon.
  32. How can you check for both conditions A and B being true?
    A && B. AND requires both conditions to be true simultaneously.
  33. How do you check if either condition A or B is true?
    A || B. OR returns true if any condition is satisfied.
  34. Identify the error: if (x < 10); return x;
    Remove the semicolon after the if condition. Empty statement creates unintended logic flow.
  35. 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.

Study these flashcards