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

Lan Anh

New member
What will be the output of the following C# code?
Code:
  static void Main(string[] args)
  {
      int a = 3, b = 5, c = 1;
      int z = ++b;
      int y = ++c;
      b = Convert.ToInt32((Convert.ToBoolean(z)) && (Convert.ToBoolean(y)) 
       || Convert.ToBoolean(Convert.ToInt32(!(++a == b))));
      a = Convert.ToInt32(Convert.ToBoolean(c) || Convert.ToBoolean(a--));
      Console.WriteLine(++a);
      Console.WriteLine(++b);
      Console.WriteLine(c);
  }
A. 2, 2, 1
B. 2, 3, 2
C.
Code:
2, 2, 2
D. 2, 0, 9
 
Answer: c
Explanation: z = 6 as ++b.
y = 2 as ++c.
6 && 2 = 1
(++a == b) which is false as 4!=6. Now, !(false) = true i.e 1.
So, 1 || 1 = 1. So, b = 1.
Similarly, c = 2 and a = 4. Now, 2 || 4 = 1.
So, a = 1.
Hence ++a = 2, ++b = 2, c = 2.
Output :
Code:
2, 2, 2
 

Tìm kiếm

Back
Top