C# Programming - Constructors - Discussion

Discussion :: 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;
    } 
}

[A]. Sample s1 = new Sample();
[B]. Sample s1 = new Sample(10);
[C]. Sample s2 = new Sample(10, 1.2f);
[D]. Sample s3 = new Sample(10, 1.2f, 2.4);
[E]. Sample s1 = new Sample(, , 2.5);

Answer: Option D

Explanation:

No answer description available for this question.

Malathisachu said: (Aug 16, 2011)  
In this example we are passing three parameter like int, single, double, . So ans is d.

Bhushan Gupta said: (Aug 29, 2011)  
I've passed three parameter & so passes three value. That's right.

Rajeev said: (Nov 29, 2011)  
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);

Vidya V Nair said: (May 15, 2013)  
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);

Geek said: (Dec 14, 2013)  
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.

Sharu said: (May 23, 2014)  
The Question is about only cretion of object sample.

Even,
A option is correct I think.

Since D option is invoking the Constructor Sample.

Chowdaiah said: (Jun 18, 2014)  
Simply we can say there are 3 with in the parameter then we got the values three like integer, string, float values.

Rahul said: (Mar 10, 2017)  
A is also correct because there will always present an implement constructor also which does not take any argument.

Suresh said: (Aug 4, 2017)  
[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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.