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

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.

Chowdaiah said:   1 decade ago
Here don't use this keyword that's why does not takes values for example if u take this.i and this.j in setdata then u will print some values so output is 0 0.

Penchala Naidu said:   1 decade ago
Whenever parameters names and instance variables name are same we need to make use of "this" keyword in the method body to refer to instance variables.

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)

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().

Narmadha said:   1 decade ago
Here in Line s1.Setdata(10,5.4f);

So Here we called only floating point number, so its displayed 0,0 .

Ajesh said:   1 decade ago
If you run this program using "this" keyword the answer becomes 10 and 5.4 and them how come zero?

Snehal said:   1 decade ago
Assignment should done to the different variables.

That's why it is printing default var 0 0?

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

Muthu (VMK) said:   1 decade ago
@ Durg, Well Said.

Until you explain about this Keyword no beginner could understand.


Post your comments here:

Your comments will be displayed after verification.