if Statements - AP Computer Science A
- Which logical operator represents 'OR' in Java?
- ||. Double pipe requires at least one condition to be true.
- 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.
- 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.
- 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.
- Evaluate the condition: if (5 < 3) { ... }
- false. Five is not less than three, so the condition evaluates to false.
- What keyword is used to test multiple conditions?
- else if. Allows chaining multiple conditions in sequence.
- Identify the keyword used for an alternative block in if statements.
- else. Provides an alternative path when the if condition is false.
- 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.
- What is the result of if (x < 0) {...} with x = -1?
- true. Negative one is indeed less than zero.
- 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.
- What is the condition to check for not equal to in Java?
- !=. Exclamation equals tests for inequality between two values.
- Choose the correct syntax for an else block.
- else { statements; }. Else block syntax follows the if block with statements in braces.
- What is the return type of a condition in an if statement?
- boolean. Conditions must evaluate to either true or false.
- What is the result of if (x >= 10) {...} with x = 9?
- false. Nine is not greater than or equal to ten.
- What does the following print? if (true || false) { System.out.println("A"); }
- A. OR operation with true makes entire condition true, executing if block.
- What is the difference between '=' and '==' in Java?
- '=' is assignment, '==' is equality check. Assignment stores a value, equality comparison tests for same value.
- 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.
- Identify the keyword used to introduce an if statement.
- if. Reserved keyword that begins every conditional statement in Java.
- 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.
- 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.
- 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.
- 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.
- 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.
- What keyword follows an if block when no condition is specified?
- else. Else provides the alternative when the if condition fails.
- 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.
- What is the syntax for an if statement in Java?
- if (condition) { statements; }. Standard Java syntax with condition in parentheses and statements in braces.
- 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.
- Choose the correct relational operator for equality.
- ==. Double equals compares values for equality, unlike single equals for assignment.
- 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.
- Which logical operator represents 'AND' in Java?
- &&. Double ampersand requires both conditions to be true.
- 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.
- 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.
- What is the result of if (x <= 10) {...} with x = 10?
- true. Less than or equal includes equality, so 10 ≤ 10 is true.
- Evaluate: if (x == 5 || x == 10) {...} with x = 5.
- true. OR condition is true when x equals either 5 or 10.
- Choose the correct syntax for else if block.
- else if (condition) { statements; }. Else if combines else and if to test additional conditions sequentially.
- What will be the output? if (true) { System.out.println("Hello"); }
- Hello. The condition true always executes the if block.
- 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.