Output will the code fragment.

Q9. What output will the following code fragment produce?

int val, res, n = 1000;

cin >> val;

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

cout << res ;

  1. if the input is 2000
  2. if the input is 1000
  3. if the input is 500.

Ans.(1). 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.

Ans(2). 400

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

Ans.(3).  200

Because (1000 + 500) > 1750 is false.

Leave a Reply

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

399 Views
%d bloggers like this: