What will be the output of the following C# code? [code] class method1 { public int fun(int m) { return( m++ % 10);

Mỹ An

New member
What will be the output of the following C# code?
Code:
 class method1
 {
     public int fun(int m)
     {
         return( m++ % 10);
     }
 }
 class Program
 {
     static void Main(string[] args)
     {
         int a = 23, b = 0, c;
         method1 z = new method1();
         c = z.fun (++b * --a % 2);
         int d = (z.fun (c-- + --a));
         Console.WriteLine(c);
         Console.WriteLine(a++);
         Console.WriteLine(d);
         Console.ReadLine();
     }
 }
A. -1, 22, 0
B. -1, 21, 1
C. 0, 22, 1
D. 0, 22, 0
 
Answer: b
Explanation: Here, for first value of c, ++b = 1 and 1 * (22%2) = 0. c = 0. Now c -- = 0 and -- a = 22 - 1 =21. Now, c -- is the first condition executed and then decremented So, c = -1. Similarly, a++ = 21. Now, as we can see from options we are confirmed over value of c = -1, a = 21. So, we can easily know that d = 1.
Output:
Code:
-1 21 1
 

Tìm kiếm

Back
Top