Code Fragment.

Q6. What output will the following code fragment produce?

int val, res, n=1000;

res = n + val > 1750 ? 400 : 200 ;

System.out.println(res);

i.) if the input is 2000     

ii.) if the input is 1000    

iii.) if the input is 500.

Ans.

i) 400 because the arithmetic operator + has higher precedence than ? : operator thus the condition before ? is taken as (n+val) and (1000+2000)>1750 is true.

ii) 400 the reason is the same as explained above((1000+1000)>1750 is true).

iii) 200 because (1000+500)>1750 is false.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: