Code Fragment.

Q6. What output will the following code fragment produce?

int val, res, n=1000;

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


i.) if the input is 2000     

ii.) if the input is 1000    

iii.) if the input is 500.


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

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s