What will be the output of the following C# code? [code] static void Main(string[] args) { int a, b, c, x; a = 80;

What will be the output of the following C# code?
Code:
  static void Main(string[] args)
  {
      int a, b, c, x;
      a = 80;
      b = 15;
      c = 2;
      x = a - b / (3 * c) * ( a + c);
      Console.WriteLine(x);
      Console.ReadLine();
  }
A. 78
B. -84
C. 80
D. 98
 
Answer: b
Explanation: Whenever the parentheses are used, the expressions within parentheses assumes higher priority. If Two or more sets of parentheses appear one after another as shown above, the expression contained on the left side is evaluated first and right hand side last.
Code:
  First pass:
      Step 1: 80 - 15/(3*2)*(80 + 2)
      Step 2: 80 - 15/6*82 ((3 * 2) evaluated first and (80 + 2) evaluated later)
  Second pass:
      Step 3: 80 - 2*82
      Step 4: 80 - 164.
  Third pass:
     Step 5 : -84. (80 - 164 is evaluated)
Output : -84 .
 

Tìm kiếm

Back
Top