C# Programming - Classes and Objects - Discussion

Discussion Forum : Classes and Objects - General Questions (Q.No. 6)
6.
Which of the following will be the correct output for the C#.NET program given below?
namespace IndiabixConsoleApplication
{ 
    class Sample
    { 
        int i; 
        Single j; 
        public void SetData(int i, Single j)
        { 
            i = i;
            j = j;
        }
        public void Display()
        { 
            Console.WriteLine(i + " " + j);
        } 
    } 
    class MyProgram
    { 
        static void Main(string[ ] args)
        { 
            Sample s1 = new Sample();
            s1.SetData(10, 5.4f); 
            s1.Display(); 
        } 
    } 
}
0 0
10 5.4
10 5.400000
10 5
None of the above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
28 comments Page 3 of 3.

Ravindra Maurya said:   9 years ago
It is why because we are calling same argument name, if we make its Letter Caps value will be print or it 'll be print when argument name will be different compiler is calling default constructor when we used same arguments.

Sample s1 = new Sample();
s1.SetData(10, 5.4f,8);
s1.Display();

private int i;
private Single J;
private int b;
public void SetData(int I, Single j,int B)
{
i = i;
J = j;
b = B;
}
public void Display()
{
Console.WriteLine(i + " " + J +" "+b);
}
(1)

Nagarjun said:   9 years ago
The keyword 'this' is missing in a method so it will come 0 and 0.0.
(1)

Viktor said:   8 years ago
@Rajeev.

I hope you're not planning to get a job as a programmer/developer. I have read your answers under several questions and I just can't believe it's true.

Sridhar said:   7 years ago
Here; i and j will be no data so the answer is 0 and 0.

Anomi said:   7 years ago
According to me, the result is 10 5.4.

Checos said:   7 years ago
0 0.

Because the object is created by the default constructor but the data s1. SetData (10, 5. 4f); is not created or haven't any constructor.
(1)

Anurag kachhwaha said:   6 years ago
The correct answer is B.

Anjani Kumar ray said:   2 years ago
The correct answer is C, 10 5.400000

Because setdata(10,5.4f) set data member of class, these value is displayed by function display().


Post your comments here:

Your comments will be displayed after verification.