C# Programming - Constructors - Discussion

Discussion :: Constructors - General Questions (Q.No.6)

6. 

Which of the following is the correct way to define the constructor(s) of the Sample class if we are to create objects as per the C#.NET code snippet given below?

Sample s1 = new Sample(); 
Sample s2 = new Sample(9, 5.6f);

[A].
public Sample()
{
    i = 0; 
    j = 0.0f;
}
public Sample (int ii, Single jj)
{
    i = ii;
    j = jj;
}
[B].
public Sample (Optional int ii = 0, Optional Single jj = 0.0f)
{
    i = ii;
    j = jj;
}
[C].
public Sample (int ii, Single jj)
{
    i = ii;
    j = jj;
}
[D].
Sample s;
[E].
s = new Sample();

Answer: Option A

Explanation:

No answer description available for this question.

Malathisachu said: (Aug 22, 2011)  
We are having 2 creation of object with values, . It describe 1 is default constructor and than parameterised constructor. We can declare two method to accept each object.

Rajeev said: (Nov 29, 2011)  
To create object of Simple class like--

Sample s1 = new Sample();


one should create constructor without parameters which is like this --
public Sample()
{
i = 0;
j = 0.0f;
}

and for
Sample s2 = new Sample(9, 5.6f);

constructor should contain two parameters of type int and Single respectively . so --
public Sample (int ii, Single jj)
{
i = ii;
j = jj;
}

Rohan Jadhav said: (Jun 24, 2015)  
'A' is correct because there is only default constructor and other is parametrized constructor.

Jaya said: (Oct 21, 2015)  
A is correct because first one is default constructor and second is parameterized constructor.

Veena Mullapudi said: (Jun 21, 2019)  
C is also correct right, it is parameterized constructor.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.