Multiple choice

The codes given below are trying to add 'n' and the value 250 or 50 depending upon whether sales > 15000 is true or false: CODE 1: bonus = n + sales >15000 ? 250 : 50; CODE 2: bonus = n + (sales > 15000 ? 250 : 50); Which code is correct?

  1. Code1

  2. Code 2

  3. Both codes 1 and 2

  4. None of the codes will give desired result

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

This option is correct as '+' operator is having higher precedence over > and ?: operator but we have used the parentheses to set up the priority and the compiler will read as per our priority setup and get the desired result.