What will be the output of the following Java code? [code] class bool_operator { public static void main(String args[])

What will be the output of the following Java code?
Code:
    class bool_operator 
    {
        public static void main(String args[]) 
        {    
             boolean a = true;
             boolean b = !true;
             boolean c = a | b;
 	     boolean d = a & b;
             boolean e = d ? b : c;
             System.out.println(d + " " + e);
        } 
    }
A. false false
B. true ture
C. true false
D. false true
 
Answer: d
Explanation: Operator | returns true if any one operand is true, thus ‘c = true | false’ is true. Operator & returns a true if both of the operand is true thus d is false. Ternary operator ?: assigns left of ‘:’ if condition is true and right hand of ‘:’ if condition is false. d is false thus e = d ? b : c , assigns c to e , e contains true.
output:
Code:
$ javac bool_operator.java
$ java bool_operator
false true
 

Tìm kiếm

Back
Top