if Statements - AP Computer Science A
Card 1 of 30
Which logical operator represents 'AND' in Java?
Which logical operator represents 'AND' in Java?
Tap to reveal answer
&&. Double ampersand requires both conditions to be true.
&&. Double ampersand requires both conditions to be true.
← Didn't Know|Knew It →
What is the return type of a condition in an if statement?
What is the return type of a condition in an if statement?
Tap to reveal answer
boolean. Conditions must evaluate to either true or false.
boolean. Conditions must evaluate to either true or false.
← Didn't Know|Knew It →
Identify the keyword used to introduce an if statement.
Identify the keyword used to introduce an if statement.
Tap to reveal answer
if. Reserved keyword that begins every conditional statement in Java.
if. Reserved keyword that begins every conditional statement in Java.
← Didn't Know|Knew It →
What is the syntax for an if statement in Java?
What is the syntax for an if statement in Java?
Tap to reveal answer
if (condition) { statements; }. Standard Java syntax with condition in parentheses and statements in braces.
if (condition) { statements; }. Standard Java syntax with condition in parentheses and statements in braces.
← Didn't Know|Knew It →
What is the outcome? if (false) { System.out.println("Yes"); } else { System.out.println("No"); }
What is the outcome? if (false) { System.out.println("Yes"); } else { System.out.println("No"); }
Tap to reveal answer
No. False condition skips if block, so else block executes instead.
No. False condition skips if block, so else block executes instead.
← Didn't Know|Knew It →
Find and correct the error: if (x > 5); { System.out.println(x); }
Find and correct the error: if (x > 5); { System.out.println(x); }
Tap to reveal answer
Remove the semicolon ';' after the condition. Semicolon after condition creates empty statement, breaking the if structure.
Remove the semicolon ';' after the condition. Semicolon after condition creates empty statement, breaking the if structure.
← Didn't Know|Knew It →
What is the result if x = 8? if (x == 10) { System.out.println("Yes"); } else { System.out.println("No"); }
What is the result if x = 8? if (x == 10) { System.out.println("Yes"); } else { System.out.println("No"); }
Tap to reveal answer
No. Since 8 ≠ 10, the condition is false and else block executes.
No. Since 8 ≠ 10, the condition is false and else block executes.
← Didn't Know|Knew It →
Choose the correct syntax for else if block.
Choose the correct syntax for else if block.
Tap to reveal answer
else if (condition) { statements; }. Else if combines else and if to test additional conditions sequentially.
else if (condition) { statements; }. Else if combines else and if to test additional conditions sequentially.
← Didn't Know|Knew It →
What is the output if x = 5? if (x != 5) { System.out.println("No"); }
What is the output if x = 5? if (x != 5) { System.out.println("No"); }
Tap to reveal answer
No output. Since x equals 5, the not-equal condition is false, so nothing prints.
No output. Since x equals 5, the not-equal condition is false, so nothing prints.
← Didn't Know|Knew It →
What will execute if both conditions are false? if (false) {...} else if (false) {...} else {...}
What will execute if both conditions are false? if (false) {...} else if (false) {...} else {...}
Tap to reveal answer
The else block executes. When no previous conditions are true, else provides the default action.
The else block executes. When no previous conditions are true, else provides the default action.
← Didn't Know|Knew It →
What is the outcome of this statement? if (true && false) { ... } else { ... }
What is the outcome of this statement? if (true && false) { ... } else { ... }
Tap to reveal answer
The else block executes. AND operation with false makes entire condition false, triggering else.
The else block executes. AND operation with false makes entire condition false, triggering else.
← Didn't Know|Knew It →
What does the following print? if (true || false) { System.out.println("A"); }
What does the following print? if (true || false) { System.out.println("A"); }
Tap to reveal answer
A. OR operation with true makes entire condition true, executing if block.
A. OR operation with true makes entire condition true, executing if block.
← Didn't Know|Knew It →
What is the output if y = 0? if (y) { System.out.println("Yes"); }
What is the output if y = 0? if (y) { System.out.println("Yes"); }
Tap to reveal answer
Compilation error. Integer variables cannot be used directly as boolean conditions in Java.
Compilation error. Integer variables cannot be used directly as boolean conditions in Java.
← Didn't Know|Knew It →
Evaluate: if (x == 5 || x == 10) {...} with x = 5.
Evaluate: if (x == 5 || x == 10) {...} with x = 5.
Tap to reveal answer
true. OR condition is true when x equals either 5 or 10.
true. OR condition is true when x equals either 5 or 10.
← Didn't Know|Knew It →
What is the result of if (x >= 10) {...} with x = 9?
What is the result of if (x >= 10) {...} with x = 9?
Tap to reveal answer
false. Nine is not greater than or equal to ten.
false. Nine is not greater than or equal to ten.
← Didn't Know|Knew It →
In an if-else ladder, when does the else execute?
In an if-else ladder, when does the else execute?
Tap to reveal answer
When all conditions are false. Else serves as the default case when no if/else-if conditions match.
When all conditions are false. Else serves as the default case when no if/else-if conditions match.
← Didn't Know|Knew It →
What is the result of if (x < 0) {...} with x = -1?
What is the result of if (x < 0) {...} with x = -1?
Tap to reveal answer
true. Negative one is indeed less than zero.
true. Negative one is indeed less than zero.
← Didn't Know|Knew It →
What will this code print? if (3 != 3) { System.out.println("Yes"); } else { System.out.println("No"); }
What will this code print? if (3 != 3) { System.out.println("Yes"); } else { System.out.println("No"); }
Tap to reveal answer
No. Since 3 equals 3, the not-equal condition is false, executing else.
No. Since 3 equals 3, the not-equal condition is false, executing else.
← Didn't Know|Knew It →
Find and correct the error: if (x > 5) { System.out.println("Yes") }
Find and correct the error: if (x > 5) { System.out.println("Yes") }
Tap to reveal answer
Add a semicolon ';' after "Yes". Missing semicolon after println statement causes compilation error.
Add a semicolon ';' after "Yes". Missing semicolon after println statement causes compilation error.
← Didn't Know|Knew It →
What block executes if the condition is true? if (condition) { ... } else {...}
What block executes if the condition is true? if (condition) { ... } else {...}
Tap to reveal answer
The first block executes. True conditions execute the if block, false conditions execute else.
The first block executes. True conditions execute the if block, false conditions execute else.
← Didn't Know|Knew It →
What is the result of if (x <= 10) {...} with x = 10?
What is the result of if (x <= 10) {...} with x = 10?
Tap to reveal answer
true. Less than or equal includes equality, so 10 ≤ 10 is true.
true. Less than or equal includes equality, so 10 ≤ 10 is true.
← Didn't Know|Knew It →
What is the condition to check for not equal to in Java?
What is the condition to check for not equal to in Java?
Tap to reveal answer
!=. Exclamation equals tests for inequality between two values.
!=. Exclamation equals tests for inequality between two values.
← Didn't Know|Knew It →
What is the result of if (x == 0 && x > -1) {...} with x = 0?
What is the result of if (x == 0 && x > -1) {...} with x = 0?
Tap to reveal answer
true. Both x equals zero and x greater than negative one are true.
true. Both x equals zero and x greater than negative one are true.
← Didn't Know|Knew It →
Find and correct the error: if x = 10 { System.out.println("Ten"); }
Find and correct the error: if x = 10 { System.out.println("Ten"); }
Tap to reveal answer
if (x == 10) { System.out.println("Ten"); }. Use double equals for comparison, not single equals for assignment.
if (x == 10) { System.out.println("Ten"); }. Use double equals for comparison, not single equals for assignment.
← Didn't Know|Knew It →
What is the difference between '=' and '==' in Java?
What is the difference between '=' and '==' in Java?
Tap to reveal answer
'=' is assignment, '==' is equality check. Assignment stores a value, equality comparison tests for same value.
'=' is assignment, '==' is equality check. Assignment stores a value, equality comparison tests for same value.
← Didn't Know|Knew It →
What keyword is used to test multiple conditions?
What keyword is used to test multiple conditions?
Tap to reveal answer
else if. Allows chaining multiple conditions in sequence.
else if. Allows chaining multiple conditions in sequence.
← Didn't Know|Knew It →
Identify the keyword used for an alternative block in if statements.
Identify the keyword used for an alternative block in if statements.
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 →
What is the purpose of an if statement?
What is the purpose of an if statement?
Tap to reveal answer
To execute code based on a condition. Allows conditional execution of code blocks based on boolean expressions.
To execute code based on a condition. Allows conditional execution of code blocks based on boolean expressions.
← Didn't Know|Knew It →
Which logical operator represents 'OR' in Java?
Which logical operator represents 'OR' in Java?
Tap to reveal answer
||. Double pipe requires at least one condition to be true.
||. Double pipe requires at least one condition to be true.
← Didn't Know|Knew It →
Choose the correct syntax for an else block.
Choose the correct syntax for an else block.
Tap to reveal answer
else { statements; }. Else block syntax follows the if block with statements in braces.
else { statements; }. Else block syntax follows the if block with statements in braces.
← Didn't Know|Knew It →