Multiple choice java

What is output of the above program?

Class conditional {
 public static void main(String args[]) {
  int i = 20;
  int j = 55;
  int z = 0;
  z = i < j ? i : j; // ternary operator
  System.out.println("The value assigned is " + z);
 }
}

  1. 20

  2. 55

  3. None of the above

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The ternary operator checks if i (20) is less than j (55). This is true. Therefore, the expression evaluates to i, which is 20. The value 20 is assigned to z.

AI explanation

To answer this question, let's go through the code step by step:

The given code snippet demonstrates the usage of the ternary operator to assign a value to the variable z based on a condition.

The ternary operator (condition) ? (expression1) : (expression2) works as follows:

  • If the condition is true, it evaluates to expression1.
  • If the condition is false, it evaluates to expression2.

In this case, the condition is i < j, where i is 20 and j is 55.

Since the condition i < j is true (20 is less than 55), the ternary operator will evaluate to i, which is 20.

Therefore, the value assigned to z is 20.

The output of the program will be: "The value assigned is 20"

Hence, the correct answer is option A) 20.