public class Test { public static void main(String[] args) { boolean a = true; boolean b = false; boolean c = true; if (a && b || c) { System.out.println("YES"); } else { System.out.println("NO"); } } }
The expression a && b || c is evaluated as (a && b) || c. Since a && b is false, but c is true, the overall expression is true, so "YES" is printed.
public class Test { public static void main(String[] args) { int x = 0; if (false && (++x > 0)) { System.out.println("Inside if"); } System.out.println(x); } }
The && operator short-circuits. Since the first condition is false, ++x is not evaluated, so x remains 0.
public class Test { public static void main(String[] args) { boolean a = false; boolean b = true; boolean c = false; if (a || b && c) { System.out.println("TRUE"); } else { System.out.println("FALSE"); } } }
The expression is evaluated as a || (b && c). Since b && c is false, and a is false, the whole expression is false, so "FALSE" is printed.
public class Test { public static void main(String[] args) { boolean flag = true; if (!flag || flag && false) { System.out.println("YES"); } else { System.out.println("NO"); } } }
The expression !flag || flag && false is (!flag) || (flag && false). Since flag is true, !flag is false and flag && false is false, so the whole expression is false || false = false. So the output should be "NO". The correct answer is B.
public class Test { public static void main(String[] args) { boolean a = true; boolean b = false; boolean c = true; boolean d = false; if ((a || b) && !(c && d) || b && !a) { System.out.println("PASS"); } else { System.out.println("FAIL"); } } }
Evaluate stepwise:
1. (a || b) = true
2. (c && d) = false
3. !(c && d) = true
4. (a || b) && !(c && d) = true && true = true
5. b && !a = false && false = false
6. Overall: true || false = true
So "PASS" is printed.