Conditional Operator Output Value Proving – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Solved Problem #JAVA#3704 siteicon   siteicon   siteicon  

Problem Statement - Conditional Operator Output Value Proving

Prove that output of the following program would be true.

 

class SP_ConditionalOP
{
  public static void main(String[] args)
  {
    double d1=7.95,d2=15.90;
    boolean x=(2*d1==15.90 && d2/2==7.95);
    boolean y=(2*d1==15.90 || d2/2==7.95);
    boolean z=(x || y);
    System.out.println(z);
  }
}

 

Solution

TC++ #3704

Run Output

true

double d1=7.95,d2=15.90;
The double variable d1 and d2 are given initial values.

boolean x=(2*d1==15.90 && d2/2==7.95);
The first part 2*d1==15.90 would be true and second part d2/2==7.95 will also be true so x would contain true.

boolean y=(2*d1==15.90 || d2/2==7.95);
Here first part 2*d1==15.90 would be true and since there is a || operator the second part will not be checked as first part is already true. So y will also contain true.

boolean z=(x || y);
Since x is true so y need not checked and z would be true.

System.out.println(z);
So this would print true only


Share

sunmitra| Created: 3-Mar-2018 | Updated: 27-Nov-2019|






Back