aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Expert Level quesiton from J@Whiz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Expert Level quesiton from J@Whiz" Watch "Expert Level quesiton from J@Whiz" New topic
Author

Expert Level quesiton from J@Whiz

Rajat Singla
Ranch Hand

Joined: Jun 21, 2001
Posts: 63
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
anand raman
Ranch Hand

Joined: Jun 06, 2001
Posts: 66
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
Hassan Naqvi
Ranch Hand

Joined: May 03, 2001
Posts: 158
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.


Always Belive On Logic!!
Haining Mu
Ranch Hand

Joined: Jun 01, 2001
Posts: 51
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

Joined: Jun 06, 2001
Posts: 66
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.
 
subject: Expert Level quesiton from J@Whiz