Boolean Logic & Conditional Statements
What is the correct syntax for a basic if-else if-else
statement in Java?
if (condition) { //code } else if (condition) { //code } else { //code }
if (condition) { //code } elseif (condition) { //code } else { //code }
if (condition) { //code } else { //code } else if (condition) { //code }
if (condition) { //code } else if (condition) { //code }
In an if-else if-else
statement, what happens if multiple conditions evaluate to true
?
All corresponding code blocks are executed.
Only the code block corresponding to the first true
condition is executed.
The code block corresponding to the last true
condition is executed.
An error occurs.
Given the following scenario: A program needs to determine if a number is positive, negative, or zero. Which if-else if-else
statement would be most appropriate?
if (num > 0) { //positive } else { //not positive }
if (num > 0) { //positive } if (num < 0) { //negative } else { //zero }
if (num > 0) { //positive } else if (num < 0) { //negative } else { //zero }
if (num > 0) { //positive } else if (num == 0) { //zero } else { //negative }
Consider the following code snippet:
java
int x = 15;
if (x > 10) {
System.out.println("A");
} else if (x > 5) {
System.out.println("B");
} else {
System.out.println("C");
}
What will be printed to the console?
A
B
C
A and B
You are writing a program to categorize students based on their age. The categories are: Child (0-12), Teen (13-19), and Adult (20+). Which order of conditions in an if-else if-else
statement is most efficient and correct?
if (age >= 20) { //Adult } else if (age >= 13) { //Teen } else { //Child }
if (age >= 13) { //Teen } else if (age >= 20) { //Adult } else { //Child }
if (age <= 12) { //Child } else if (age <= 19) { //Teen } else { //Adult }
if (age >= 0) { //Child } else if (age >= 13) { //Teen } else { //Adult }
What is the most likely outcome of the following code snippet?
java
int x = 7;
if (x > 10)
System.out.println("A");
else if (x > 5)
System.out.println("B");
else
System.out.println("C");
A
B
C
The code will not compile due to missing braces.
Consider the following code:
java
int num = 15;
if (num % 5 == 0) {
System.out.println("Divisible by 5");
} else if (num % 3 == 0) {
System.out.println("Divisible by 3");
} else {
System.out.println("Not divisible by 3 or 5");
}
What is the output of this code?
Divisible by 5
Divisible by 3
Not divisible by 3 or 5
Divisible by 5 and Divisible by 3

How are we doing?
Give us your feedback and let us know how we can improve
Given the following code, complete the determineSign
method so that it returns "Positive" if the number is greater than 0, "Negative" if it's less than 0, and "Zero" if it's equal to 0. ```
java
public static String determineSign(int num) {
// Your code here
}
public static String determineSign(int num) { if (num > 0) return "Positive"; else if (num < 0) return "Negative"; else return "Zero"; }
public static String determineSign(int num) { if (num > 0) return "Positive"; if (num < 0) return "Negative"; return "Zero"; }
public static String determineSign(int num) { if (num > 0) return "Positive"; else return "Negative"; return "Zero"; }
public static String determineSign(int num) { return (num > 0) ? "Positive" : (num < 0) ? "Negative" : "Zero"; }
Write a method called getLetterGrade
that accepts an integer score
and returns a letter grade according to the following scale:
- 90-100: 'A'
- 80-89: 'B'
- 70-79: 'C'
- 60-69: 'D'
- 0-59: 'F'
Which of the following implementations is correct?
public static char getLetterGrade(int score) { if (score >= 90) return 'A'; else if (score >= 80) return 'B'; else if (score >= 70) return 'C'; else if (score >= 60) return 'D'; else return 'F'; }
public static char getLetterGrade(int score) { if (score >= 90) return 'A'; if (score >= 80) return 'B'; if (score >= 70) return 'C'; if (score >= 60) return 'D'; else return 'F'; }
public static char getLetterGrade(int score) { if (score >= 60) return 'D'; else if (score >= 70) return 'C'; else if (score >= 80) return 'B'; else if (score >= 90) return 'A'; else return 'F'; }
public static char getLetterGrade(int score) { if (score >= 90 && score <= 100) return 'A'; else if (score >= 80 && score <= 89) return 'B'; else if (score >= 70 && score <= 79) return 'C'; else if (score >= 60 && score <= 69) return 'D'; else return 'F'; }