Given the code fragment:
What is the result?
Given the code fragment:
What is the result?
The variable `var1` is evaluated using the ternary operator. In this case, the expression `(12_345.01 <= 123_45.00)` is false (since 12,345.01 is greater than 12,345.00), so `var1` is assigned the value `124_56.02f`. Adding 1024 to this value results in `13480.02`. Therefore, the correct result printed is 13480.02.
Answer is C
Answer is D. is tested
Tested Correct answer is D : 13480.02
Answer is D. To test: public static void main(String[] args) { float var1 = (12_345.01 <= 123_45.00) ? 12_456 : 124_56.02f; float var2 = var1 + 1024; System.out.println(var2); }
Answer is D. float var1 = (12_345.01 <= 123_45.00) ? 12_456 : 124_56.02f; float var2 = var1 + 1024; System.out.println(var2); // 13480.02
B, if returns true so double comes. Then it couldn’t be assigned float
12_456 is not double but int.
int value can assign to the float value
Correct answer C
The correct answer C, Tested 13480.0
this is a tricky question. the boolean statement is double and double doesn't fit inside the float. Looks like false/true return type matters here, not data type inside the boolean expression.
The answer is C Confirmed on Netbeans
Correct answer is C
Correct Answer is D float var1 = (12_345.01 <= 123_45.00) ? 12_456 : 124_56.02f; // 124_56.02f value assigned to var1 float var2 = var1 + 1024; // 124_56.02f +1024 System.out.println(var2); // 13480.02
Indeed, the key aspect here is the return type of the ternary expression, which is determined by the possible types of the operands. In this case, even though the boolean expression involves doubles, the ternary operator considers the common type compatible with both branches, which is float because float can accommodate both 12_456 and 12456.02f. This is why the result is float and not double.
The answer is the letter D, analyzing the expression, it returns a false, so it returns 124_56.02f (remembering that the _ does not separate the decimal place, it only separates the number, it only serves to help reading) so when adding 12456.02 + 1024 = 13480.02
float var1 = (12_345.01 >= 123_45.00) ? 12_456 : 124_56.02f; float var2 = var1 + 1024; System.out.println(var2);
Correct answer : C - 13480.0 float var1; if(12_345.01 >= 123_45.00){ var1 = 12_456; }else{ var1=124_56.02f } => the condition is true : 12345.01 > 12345.00 => var1 = 12_456 which means 12456 (The underscore is useful just for the readability). Then var 2 = var1 + 1024; => var 2 = 13480 => Output: 13480.0.
tested. correct id D. but still can't say why