Bird
Raised Fist0
Javaprogramming~5 mins

If–else statement in Java - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of an if–else statement in Java?
An if–else statement lets the program choose between two paths: one if a condition is true, and another if it is false.
Click to reveal answer
beginner
Write the basic syntax of an if–else statement in Java.
if (condition) {
// code if true
} else {
// code if false
}
Click to reveal answer
beginner
What happens if the condition in an if statement is false and there is no else block?
The program skips the if block and continues with the next code after it, doing nothing inside the if block.
Click to reveal answer
intermediate
Can you have multiple else if blocks in Java? What is their purpose?
Yes, multiple else if blocks let you check several conditions in order, running the code for the first true condition.
Click to reveal answer
beginner
Explain with an example how if–else helps in decision making in programs.
Example: if (score >= 50) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
This decides if a student passes or fails based on score.
Click to reveal answer
What will happen if the condition in an if statement is true?
ANothing happens
BThe code inside the if block runs
CThe program stops
DThe code inside the else block runs
Which keyword is used to check an additional condition after an if in Java?
Aelif
Belseif
Celse
Delse if
What is the output of this code?
int x = 10;
if (x > 5) {
System.out.println("Yes");
} else {
System.out.println("No");
}
AError
BNo
CYes
DNothing
What happens if no else block is provided and the if condition is false?
AThe program skips the if block and continues
BThe if block runs anyway
CThe program crashes
DThe else block runs
Which of these is a correct if–else statement in Java?
Aif (x > 5) { System.out.println("Hi"); } else { System.out.println("Bye"); }
Bif x > 5 then { print("Hi"); } else { print("Bye"); }
Cif (x > 5) print("Hi"); else print("Bye");
Dif x > 5 { System.out.println("Hi"); } else { System.out.println("Bye"); }
Explain how an if–else statement controls the flow of a Java program.
Think about how the program chooses which code to run.
You got /4 concepts.
    Write a simple Java if–else statement that prints 'Adult' if age is 18 or more, otherwise prints 'Minor'.
    Use comparison and print statements.
    You got /5 concepts.

      Practice

      (1/5)
      1. What does an if-else statement do in Java?
      easy
      A. It defines a new class.
      B. It repeats code multiple times.
      C. It stores multiple values in a list.
      D. It chooses between two actions based on a condition.

      Solution

      1. Step 1: Understand the purpose of if-else

        An if-else statement lets the program decide between two paths depending on a condition.
      2. Step 2: Compare with other options

        Repeating code is done by loops, storing values is done by collections, and defining classes is unrelated.
      3. Final Answer:

        It chooses between two actions based on a condition. -> Option D
      4. Quick Check:

        If-else = choose between two paths [OK]
      Hint: If you want two choices, use if-else [OK]
      Common Mistakes:
      • Confusing if-else with loops
      • Thinking if-else stores data
      • Mixing if-else with class definitions
      2. Which of the following is the correct syntax for an if-else statement in Java?
      easy
      A. if condition { } else { }
      B. if (condition) { } else { }
      C. if (condition) then { } else { }
      D. if condition then { } else { }

      Solution

      1. Step 1: Recall Java syntax rules

        Java requires parentheses around the condition and curly braces for code blocks.
      2. Step 2: Check each option

        Only if (condition) { } else { } uses parentheses and curly braces correctly without extra keywords like 'then'.
      3. Final Answer:

        if (condition) { } else { } -> Option B
      4. Quick Check:

        Parentheses + braces = correct syntax [OK]
      Hint: Remember: condition in () and code in {} [OK]
      Common Mistakes:
      • Omitting parentheses around condition
      • Using 'then' keyword incorrectly
      • Missing curly braces for blocks
      3. What will be the output of this Java code?
      int x = 10;
      if (x > 5) {
          System.out.println("Greater");
      } else {
          System.out.println("Smaller or equal");
      }
      medium
      A. Greater
      B. Smaller or equal
      C. No output
      D. Compilation error

      Solution

      1. Step 1: Evaluate the condition x > 5

        Since x is 10, the condition 10 > 5 is true.
      2. Step 2: Determine which block runs

        The if block runs and prints "Greater"; the else block is skipped.
      3. Final Answer:

        Greater -> Option A
      4. Quick Check:

        10 > 5 is true, so print "Greater" [OK]
      Hint: Check condition true? Run if-block, else run else-block [OK]
      Common Mistakes:
      • Confusing > with <
      • Thinking else block runs when if is true
      • Assuming no output if condition true
      4. Identify the error in this Java code snippet:
      int num = 7;
      if num > 5 {
          System.out.println("Yes");
      } else {
          System.out.println("No");
      }
      medium
      A. Missing curly braces around else block.
      B. Missing semicolon after if statement.
      C. Missing parentheses around the condition in if statement.
      D. Variable num is not declared.

      Solution

      1. Step 1: Check if statement syntax

        Java requires parentheses around the condition in an if statement.
      2. Step 2: Verify other parts

        Semicolon after if is not needed; curly braces are present; num is declared.
      3. Final Answer:

        Missing parentheses around the condition in if statement. -> Option C
      4. Quick Check:

        If condition needs () [OK]
      Hint: Always put condition inside () in if [OK]
      Common Mistakes:
      • Omitting parentheses in if condition
      • Adding unnecessary semicolon after if
      • Forgetting to declare variables
      5. You want to write a Java program that prints "Even" if a number is even, and "Odd" if it is odd. Which if-else code correctly does this?
      int n = 4;
      // Your code here
      hard
      A. if (n % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); }
      B. if (n / 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); }
      C. if (n % 2 = 0) { System.out.println("Even"); } else { System.out.println("Odd"); }
      D. if (n % 2 != 0) { System.out.println("Even"); } else { System.out.println("Odd"); }

      Solution

      1. Step 1: Understand how to check even numbers

        A number is even if dividing by 2 leaves remainder 0, so use n % 2 == 0.
      2. Step 2: Check each option's condition

        if (n % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } uses correct equality check with == and modulo operator. if (n / 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } uses division instead of modulo. if (n % 2 = 0) { System.out.println("Even"); } else { System.out.println("Odd"); } uses assignment = instead of comparison ==. if (n % 2 != 0) { System.out.println("Even"); } else { System.out.println("Odd"); } reverses logic.
      3. Final Answer:

        if (n % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } -> Option A
      4. Quick Check:

        Use modulo == 0 to check even [OK]
      Hint: Use n % 2 == 0 to test even numbers [OK]
      Common Mistakes:
      • Using = instead of == for comparison
      • Using division instead of modulo
      • Reversing even and odd logic
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard