if Statements - AP Computer Science A

  1. Which logical operator represents 'OR' in Java?
    ||. Double pipe requires at least one condition to be true.
  2. What block executes if the condition is true? if (condition) { ... } else {...}
    The first block executes. True conditions execute the if block, false conditions execute else.
  3. What is the purpose of an if statement?
    To execute code based on a condition. Allows conditional execution of code blocks based on boolean expressions.
  4. Find and correct the error: if x = 10 { System.out.println("Ten"); }
    if (x == 10) { System.out.println("Ten"); }. Use double equals for comparison, not single equals for assignment.
  5. Evaluate the condition: if (5 < 3) { ... }
    false. Five is not less than three, so the condition evaluates to false.
  6. What keyword is used to test multiple conditions?
    else if. Allows chaining multiple conditions in sequence.
  7. Identify the keyword used for an alternative block in if statements.
    else. Provides an alternative path when the if condition is false.
  8. Identify the error in this code: if (x > 5) System.out.println(x); }
    Remove the closing brace '}' after the statement. Single statements don't need braces, so the extra closing brace is invalid.
  9. What is the result of if (x < 0) {...} with x = -1?
    true. Negative one is indeed less than zero.
  10. What is the output if x = 5? if (x != 5) { System.out.println("No"); }
    No output. Since x equals 5, the not-equal condition is false, so nothing prints.
  11. What is the condition to check for not equal to in Java?
    !=. Exclamation equals tests for inequality between two values.
  12. Choose the correct syntax for an else block.
    else { statements; }. Else block syntax follows the if block with statements in braces.
  13. What is the return type of a condition in an if statement?
    boolean. Conditions must evaluate to either true or false.
  14. What is the result of if (x >= 10) {...} with x = 9?
    false. Nine is not greater than or equal to ten.
  15. What does the following print? if (true || false) { System.out.println("A"); }
    A. OR operation with true makes entire condition true, executing if block.
  16. What is the difference between '=' and '==' in Java?
    '=' is assignment, '==' is equality check. Assignment stores a value, equality comparison tests for same value.
  17. What is the result if x = 8? if (x == 10) { System.out.println("Yes"); } else { System.out.println("No"); }
    No. Since 8 ≠ 10, the condition is false and else block executes.
  18. Identify the keyword used to introduce an if statement.
    if. Reserved keyword that begins every conditional statement in Java.
  19. What is the outcome of if (x < 5 && x > 10) {...} with x = 7?
    false. Seven cannot be both less than five and greater than ten simultaneously.
  20. In an if-else ladder, when does the else execute?
    When all conditions are false. Else serves as the default case when no if/else-if conditions match.
  21. Find and correct the error: if (x > 5); { System.out.println(x); }
    Remove the semicolon ';' after the condition. Semicolon after condition creates empty statement, breaking the if structure.
  22. What will this code print? if (3 != 3) { System.out.println("Yes"); } else { System.out.println("No"); }
    No. Since 3 equals 3, the not-equal condition is false, executing else.
  23. What will execute if both conditions are false? if (false) {...} else if (false) {...} else {...}
    The else block executes. When no previous conditions are true, else provides the default action.
  24. What keyword follows an if block when no condition is specified?
    else. Else provides the alternative when the if condition fails.
  25. Find and correct the error: if (x > 5) { System.out.println("Yes") }
    Add a semicolon ';' after "Yes". Missing semicolon after println statement causes compilation error.
  26. What is the syntax for an if statement in Java?
    if (condition) { statements; }. Standard Java syntax with condition in parentheses and statements in braces.
  27. What is the output if y = 0? if (y) { System.out.println("Yes"); }
    Compilation error. Integer variables cannot be used directly as boolean conditions in Java.
  28. Choose the correct relational operator for equality.
    ==. Double equals compares values for equality, unlike single equals for assignment.
  29. What is the outcome? if (false) { System.out.println("Yes"); } else { System.out.println("No"); }
    No. False condition skips if block, so else block executes instead.
  30. Which logical operator represents 'AND' in Java?
    &&. Double ampersand requires both conditions to be true.
  31. What is the output if x = 5? if (x > 3) { System.out.println("Yes"); }
    Yes. Since 5 > 3 is true, the if block executes and prints.
  32. What is the outcome of this statement? if (true && false) { ... } else { ... }
    The else block executes. AND operation with false makes entire condition false, triggering else.
  33. What is the result of if (x <= 10) {...} with x = 10?
    true. Less than or equal includes equality, so 10 ≤ 10 is true.
  34. Evaluate: if (x == 5 || x == 10) {...} with x = 5.
    true. OR condition is true when x equals either 5 or 10.
  35. Choose the correct syntax for else if block.
    else if (condition) { statements; }. Else if combines else and if to test additional conditions sequentially.
  36. What will be the output? if (true) { System.out.println("Hello"); }
    Hello. The condition true always executes the if block.
  37. What is the result of if (x == 0 && x > -1) {...} with x = 0?
    true. Both x equals zero and x greater than negative one are true.

Study these flashcards