What is the result of the following code execution?

public class BooleanTest {
   static boolean test1(int val) {
        System.out.println("test1("+ val +")");
        System.out.println("result: " + (val < 10));
        return val < 10;
    }

    static boolean test2(int val) {
        System.out.println("test2("+ val +")");
        System.out.println("result: " + (val < 20));
        return val < 20;
    }

    static boolean test3(int val) {
        System.out.println("test3("+ val +")");
        System.out.println("result: " + (val < 30));
        return val < 30;
    }

    public static void main(String[] args) {
       if(test1(0) && test2(20) && test3(20))
           System.out.println("Expression is true");
       else
           System.out.println("Expression is false");
    }
}
Explanation
&& operator evaluates the operands in order from left to right until it can unequivocally determine the truth of the whole expression.
Therefore, in this example, only two methods are called:
test1(0) – true
test2(20) – false
There is no sense to call test3() method, because regardless of its result, it is clear that the value of the entire expression is false.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Java
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести