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();
}
}
}
Discussion:
28 comments Page 1 of 3.
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.
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)
Nagarjun said:
9 years ago
The keyword 'this' is missing in a method so it will come 0 and 0.0.
(1)
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);
}
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)
Gaurav patil said:
1 decade ago
It is because the s1.SetData(10, 5.4f); will set the values if i, j but in line s1.Display(); function will call default constructor and default values assigned to i,j (0,0) .
That is why it will print 0 0.
That is why it will print 0 0.
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().
Because setdata(10,5.4f) set data member of class, these value is displayed by function display().
Anurag kachhwaha said:
6 years ago
The correct answer is B.
Anomi said:
7 years ago
According to me, the result is 10 5.4.
Sridhar said:
7 years ago
Here; i and j will be no data so the answer is 0 and 0.
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.
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.
David said:
1 decade ago
Due to the naming collision more than having to use the this keyword as proper syntax.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers