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

Kim Thu Đoàn

New member
What will be the output of the following C# code?
Code:
  static void Main(string[] args)
  {
      int a, b, c, x;
      a = 90;
      b = 15;
      c = 3;
      x = a - b / 3 + c * 2 - 1;
      Console.WriteLine(x);
      Console.ReadLine();
  }
A. 92
B. 89
C. 90
D. 88
 
Answer: c
Explanation: The basic evaluation process includes two left to right passes through the expression. During first pass, the high priority operators are applied and during second pass, the low priority operators are applied as they are encountered.
Code:
First pass :
       step 1 : x = 90 - 15 / 3 + 3 * 2 - 1 (15 / 3 evaluated)
       step 2 : x = 90 - 5 + 3 * 2 - 1
       step 3 : x = 90 - 5 + 3 * 2 -1 (3 * 2 is evaluated)
       step 4 : x = 90 - 5 + 6 - 1
 Second pass :
       step 5 : x = 85 + 6 - 1 (90 - 5 is evaluated)
       step 6 : x = 91 - 1(85 + 6 is evaluated)
       step 7 : x = 90(91 - 1 is evaluated)
Output : 90.
 

Tìm kiếm

Back
Top