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.

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s