C# Programming - Constructors - Discussion

Discussion Forum : Constructors - General Questions (Q.No. 3)
3.
Which of the following ways to create an object of the Sample class given below will work correctly?
class Sample
{
    int i;
    Single j;
    double k;
    public Sample (int ii, Single jj, double kk)
    {
        i = ii;
        j = jj;
        k = kk;
    } 
}
Sample s1 = new Sample();
Sample s1 = new Sample(10);
Sample s2 = new Sample(10, 1.2f);
Sample s3 = new Sample(10, 1.2f, 2.4);
Sample s1 = new Sample(, , 2.5);
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
9 comments Page 1 of 1.

Suresh said:   8 years ago
[A]. Sample s1 = new Sample(); ----->Gives error " Sample does not contain a constructor that takes 0 arguments" as we are not passing parameters.

[B]. Sample s1 = new Sample(10); --->Gives error " Sample does not contain a constructor that takes 1 arguments".

[C]. Sample s2 = new Sample(10, 1.2f); --->Gives error " Sample does not contain a constructor that takes 2 arguments".

[D]. Sample s3 = new Sample(10, 1.2f, 2.4); -->is Right answer.

[E]. Sample s1 = new Sample(, , 2.5); ---->Error 1 Argument missing, Error 2 Argument missing.

Rahul said:   8 years ago
A is also correct because there will always present an implement constructor also which does not take any argument.

Chowdaiah said:   1 decade ago
Simply we can say there are 3 with in the parameter then we got the values three like integer, string, float values.

Sharu said:   1 decade ago
The Question is about only cretion of object sample.

Even,
A option is correct I think.

Since D option is invoking the Constructor Sample.

Geek said:   1 decade ago
Since we have 3 arguments in parameterized constructor.
i.e.
public Sample (int ii, Single jj, double kk)

So while creating an object we have to also use 3 parameters.

Vidya v nair said:   1 decade ago
I Have integer i, string j, and double k so I can call 3 parameter values
for eg: INPUT: public Sample (int ii, Single jj, double kk);

OUTPUT: Sample s3 = new Sample(10, 1.2f, 2.4);

Rajeev said:   1 decade ago
When we write constructor by your own then default constructor destroyed(), so as we can see in question ,

public Sample (int ii, Single jj, double kk)
{
i = ii;
j = jj;
k = kk;
}

the constructor has been written now if we want to call constructor for this class we will have to create constructor accordingly.
like this---

Sample s3 = new Sample(10, 1.2f, 2.4);

Bhushan Gupta said:   1 decade ago
I've passed three parameter & so passes three value. That's right.

Malathisachu said:   1 decade ago
In this example we are passing three parameter like int, single, double, . So ans is d.

Post your comments here:

Your comments will be displayed after verification.