In the following Java program, what string is printed to the console? class Hell { public static void main(String[] args) { Integer i = 42; String s = (i < 40) ? "life" : (i > 50) ? "base" : "ball"; System.out.println(s); } }

Difficulty: Medium

Correct Answer: ball

Explanation:


Introduction / Context:
The conditional or ternary operator in Java is a compact way to write simple if else expressions. When nested, however, it can become harder to read, and interviewers often use such examples to test your understanding of evaluation order and conditions. This program assigns a value to the string s using a nested ternary expression and then prints it. To answer correctly, you must trace the boolean conditions carefully rather than guess based on appearance.


Given Data / Assumptions:

  • The integer variable i is initialised to 42.
  • The expression uses the ternary operator in the form condition ? valueIfTrue : valueIfFalse.
  • The nested expression is (i < 40) ? "life" : (i > 50) ? "base" : "ball".
  • System.out.println(s) will print exactly the chosen string with no extra logic.


Concept / Approach:
The ternary operator in Java is right associative, but parentheses in this example make the grouping explicit. First, the outer ternary checks whether i is less than 40. If that condition is true, the result is "life". If it is false, the expression after the colon is evaluated, which is another ternary expression testing whether i is greater than 50. If i is greater than 50, the result is "base". Otherwise, the result is "ball". Because i is 42, it fails the first condition (less than 40) and also fails the second condition (greater than 50), so the final else branch "ball" is selected.


Step-by-Step Solution:
Step 1: Evaluate the first condition i < 40. Since i is 42, 42 < 40 is false. Step 2: Because the first condition is false, the result of the outer ternary is not "life"; instead, we evaluate the expression after the colon, which is (i > 50) ? "base" : "ball". Step 3: Evaluate the second condition i > 50. Since i is 42, 42 > 50 is also false. Step 4: Because the second condition is false, the nested ternary expression yields its else part, which is the string "ball". Step 5: Assign "ball" to the variable s and then print s using System.out.println(s), which outputs ball to the console.


Verification / Alternative check:
An alternative way to reason about the same code is to rewrite the ternary expression using if else statements. You can transform it into if (i < 40) { s = "life"; } else if (i > 50) { s = "base"; } else { s = "ball"; }. With i equal to 42, the first condition if (i < 40) fails, the second condition else if (i > 50) also fails, and the final else branch assigns "ball". This rewritten form is easier to read and confirms that the nested ternary logic chooses "ball" when i is 42.


Why Other Options Are Wrong:
Option null: The code never assigns null to s; it always assigns one of the three constant strings. Option base: This string would be chosen only if i > 50, which is not the case for i = 42. Option life: This string would be chosen only if i < 40, which is also not true for i = 42.


Common Pitfalls:
When reading nested ternary operators, some people forget to evaluate conditions in order or misinterpret right associativity, leading them to pick the wrong branch. Another pitfall is mentally inverting the comparisons, such as confusing < with >. To avoid these mistakes, it is often helpful to rewrite complex ternary expressions using if else blocks or to evaluate each condition step by step. In exams, do not rush; carefully checking each boolean expression will lead to the correct output.


Final Answer:
The program prints the string ball to the console.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion