Computer Applications

Choose the statement which is equivalent to the given:

if(a>b) 
System.out.println("Honest"); 
else 
System.out.println("Sincere");
  1. a>b? System.out.println("Honest"):System.out.println("Sincere");
  2. System.out.println(a>b? "Honest":"Sincere");
  3. a>b? "Honest": "Sincere";
  4. a>b: "Honest"? "Sincere";

Java Conditional Stmts

2 Likes

Answer

System.out.println(a>b? "Honest":"Sincere");

Reason — The ternary operator condition ? value1 : value2 is used to select one of two values based on a condition. The above if-else statement can be rewritten using the ternary operator inside the println() method as: System.out.println(a > b ? "Honest" : "Sincere");. This statement prints "Honest" if a > b, otherwise it prints "Sincere".

Answered By

1 Like


Related Questions