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 1 of 3.

Anjani Kumar ray said:   5 months 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().

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

Checos said:   5 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)

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

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

Viktor said:   6 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.

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

Ravindra Maurya said:   8 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)

David said:   8 years ago
Due to the naming collision more than having to use the this keyword as proper syntax.

Snehal said:   9 years ago
0 0 are default values set by constructor when object gets created.


Post your comments here:

Your comments will be displayed after verification.