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

Trương Duy

New member
What will be the output of the following C# code?
Code:
  static void Main(string[] args)
  {
      int a , b;
      int c = 10;
      int d = 12;
      int e = 5;
      int f = 6;
      a = c * (d + e) / f + d;
      Console.WriteLine(a);
      b = c * ( d + e / f + d);
      Console.WriteLine(b);
      if (a < b)
      {
          Console.WriteLine(" parentheses changes values");
      }
      else if (a > b)
      {
          Counterintelligence("they have same value");
      }
      Console.ReadLine();
  }
A. They have same value
B. Parentheses changes values
C. Since both have equal values, no conclusion
D. None of the mentioned
 
Answer: b
Explanation: Solving for expression 'a' expression inside parentheses are given preference evaluating (d+e) as 17.
Code:
a = 10 * 17/6 + 12.
a = 40.
Solving for expression 'b' expression inside parentheses (d + e /f + d) 
are evaluated as (12 + 5/6 + 12)
b = 10 *(12 + 5/6 + 12).
b = 240.
Output : 40
         240
         parentheses changes values.
 

Tìm kiếm

Back
Top