Search code examples
c#mathdiscrete-mathematicsarithmetic-expressions

Which of the following arithmetic expressions is identical to the expression a += b * c


A - Which of the following arithmetic expressions is identical to the expression a += b * c ?

  1. a = (a + b) * c
  2. a = b * c
  3. a = a + b * c
  4. a = ++b * c
  5. None of the above

B - Which of these following expressions is wrong ?

  1. c=a+b
  2. a+b=c
  3. a=b+c
  4. c+=a+b
  5. None of the above

Apparently the answers are A = 3 and B = 2, would someone please explain how these are the answers. I believe these questions are related to C#.


Solution

  • A. x += y is just shorthand for x = x + y, the compiler always expands it out as such https://msdn.microsoft.com/en-us/library/sa7629ew.aspx

    B. C# isn't like math where you can rearrange an equation as you wish. As such, you can't have an expression such as a+b on the lefthand side of an assignment, which is what the = does. How would you store the value of c in a+b? it just isn't logical so this is the clear wrong answer. In programming, = does not have the same meaning as an equals sign in a mathematical equation. https://msdn.microsoft.com/en-us/library/sbkb459w.aspx