-5

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#.

Cœur
  • 37,241
  • 25
  • 195
  • 267
I.Ali
  • 5
  • 5
  • You should start by looking up the `+=` operator. – tnw Jan 08 '16 at 16:31
  • for b, you could look up variables and assignments or some variation of assigning values to variables. Most programming languages use the same rule for assigning variables that's I've dealt with. – Charles May Jan 08 '16 at 16:33
  • Did you pay attention in basic math class in regards to `Order of Operation`? I am just curious.. this is not that difficult – MethodMan Jan 08 '16 at 16:45
  • _I believe these questions are related to C#_ Just what makes you think that? – TaW Jan 08 '16 at 17:03

1 Answers1

0

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

Marco Fatica
  • 937
  • 7
  • 12