πŸ“Š JAVA
Q. What is the output of this program?
Code:
public class bool_operator_Example
    {
        public static void main(String args[]) 
        {    
             boolean p = true;
             boolean q = !true;
             boolean r = p | q;
 	     boolean s = p & q;
             boolean z = s ? q : r;
             System.out.println(s + " " + z);
        } 
    }
  • (A) false true
  • (B) true false
  • (C) true true
  • (D) false false
πŸ’¬ Discuss
βœ… Correct Answer: (A) false true

Explanation: Operator | returns true if any one operand is true, thus ‘r = true | false’ is true. Operator & returns p true if both of the operand is true thus s is false. Ternary operator ?: assigns left of ‘:’ if condition is true and right hand of ‘:’ if condition is false. z is false thus z = s ? q : r , assigns r to z , z contains true.
output: false true

Explanation by: Rajeev Malhotra
Operator | returns true if any one operand is true, thus ‘r = true | false’ is true. Operator & returns p true if both of the operand is true thus s is false. Ternary operator ?: assigns left of ‘:’ if condition is true and right hand of ‘:’ if condition is false. z is false thus z = s ? q : r , assigns r to z , z contains true.
output: false true

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
228
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Rajeev Malhotra
Publisher
πŸ“ˆ
97%
Success Rate