programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Expert Level quesiton from J@Whiz

Ranch Hand
Posts: 63
• Number of slices to send:
Optional 'thank-you' note:
Hi All,
This is one of the expert level question from the final test of jwhiz software.
What results from attempting to compile and run the following code?
public class Ternary
{
public static void main(String args[])
{
int a = 5;
System.out.println("Value is - " + ((a < 5) ? 9.9 : 9));
}
}
Choices are
a.) Value is - 9
a.) Value is - 5
c.) Compile Time Error
d.) None of these
After going throught the complete explanation i got the hidden truth and got convinced that answer would be d. The explanation is as follows:
D is correct. The code compiles successfully. In this code the optional value for the ternary operator, 9.0(a double) and 9(an int) are of different types. The result of a ternary operator must be determined at the compile time, and here the type chosen using the rules of promotion for binary operands, is double. Since the result is a double, the output value is printed in a floating point format. The choice of which value to be printed is made on the basis of the result of the comparison "a < 5" which results in false, hence a takes the second of the two possible values, which is 9, but because the result type is promoted to double, the output value is actually written as 9.0, rather than the more obvious 9, hence D is correct.
Now my question is what will be the affect if i change the type of variable a from int to some other integeral type. Will it have any type in the result.
Pl help.
Thanks
rajat

Ranch Hand
Posts: 66
• Number of slices to send:
Optional 'thank-you' note:
the answer will not be affected by just changing the data type of the variable a.
As i understand it, the return type of ternary operator is determined by the types of the true and false option. The return type will be determined by the larger option.
Hope this helps
Anand

Ranch Hand
Posts: 158
• Number of slices to send:
Optional 'thank-you' note:
Hi Rajat.
It's a common behavior of ternary operator... you can get explanation of this behavior in almost all certification guides. I don't think that it's a master level question.
Well try boolean instead of 9.
Regards,
Hassan.

Ranch Hand
Posts: 51
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by anand raman:
the answer will not be affected by just changing the data type of the variable a. As i understand it, the return type of ternary operator is determined by the types of the true and false option. The return type will be determined by the larger option.

NOT absolutly right. for this code:
System.out.println("Value is - " + ((3 < 5) ? 9.9 : 9)); will give output "9" instead of 9.0 although double is widen than int here.

anand raman
Ranch Hand
Posts: 66
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Haining Mu:
NOT absolutly right. for this code:
System.out.println("Value is - " + ((3 < 5) ? 9.9 : 9)); will give output "9" instead of 9.0 although double is widen than int here.

But the above code snippet will result in value 9.9 as (3<5) and thus the condition is true. For the sake of argument i reveresed the condition to (3>5) and still got 9.0 rather than simply 9
Anand

 Consider Paul's rocket mass heater.