What will be the output of the following C# code snippet? [code] class sum { public int x; public int y; public int

Trang Esther

New member
What will be the output of the following C# code snippet?
Code:
 class sum   
 {
     public int x;
     public int y;
     public  int add (int a, int b)
    {
        x = a + b;
        y = x + b;
        return 0;
    }
}    
class Program
{
    static void Main(string[] args)
    {
        sum obj1 = new sum();
        sum obj2 = new sum();   
        int a = 2;
        obj1.add(a, a + 1);
        obj2.add(5, a);
        Console.WriteLine(obj1.x + "  " + obj2.y);     
        Console.ReadLine();
    }
}
A. 6, 9
B.
Code:
5, 9
C. 9, 10
D. 3, 2
 
Answer: b
Explanation: Here, a = 2, a + 1 = 2 + 1 = 3.
So, a = 2, b = 3.
x = 2 + 3 = 5.
y = 5 + 3 = 8.
Similarly, a = 5, b = a + 1 = 4.
y = 5 + 4 = 9.
Output :
Code:
5, 9
 

Tìm kiếm

Back
Top