C# - Constructors Question and Answer
C# - Constructors Question and Answer
1. Which of the following statements is correct?
- A constructor can be used to set default values and limit instantiation.
- C# provides a copy constructor.
- Destructors are used with classes as well as structures.
- A class can have more than one destructor.
2. 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;
}
}
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);
3. Which of the following statements are correct about static functions?
Static functions can access only static data.
Static functions cannot call instance functions.
It is necessary to initialize static data.
Instance functions can call static functions and access static data.
this reference is passed to static functions.
Static functions can access only static data.
Static functions cannot call instance functions.
It is necessary to initialize static data.
Instance functions can call static functions and access static data.
this reference is passed to static functions.
- 1, 2, 4
- 2, 3, 5
- 3, 4
- 4, 5
4. Which of the following statements is correct about constructors?
- If we provide a one-argument constructor then the compiler still provides a zero-argument constructor.
- Static constructors can use optional arguments.
- Overloaded constructors cannot use optional arguments.
- If we do not provide a constructor, then the compiler provides a zero-argument constructor.
5. 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);
Sample s1 = new Sample();
Sample s2 = new Sample(9, 5.6f);
- public Sample() { i = 0; j = 0.0f; } public Sample (int ii, Single jj) { i = ii; j = jj; }
- public Sample (Optional int ii = 0, Optional Single jj = 0.0f) { i = ii; j = jj; }
- public Sample (int ii, Single jj) { i = ii; j = jj; }
- Sample s;
6. In which of the following should the methods of a class differ if they are to be treated as overloaded methods?
Type of arguments
Return type of methods
Number of arguments
Names of methods
Order of arguments
Type of arguments
Return type of methods
Number of arguments
Names of methods
Order of arguments
- 2, 4
- 3, 5
- 1, 3, 5
- 3, 4, 5
8. Which of the following statements are correct about constructors in C#.NET?
Constructors cannot be overloaded.
Constructors always have the name same as the name of the class.
Constructors are never called explicitly.
Constructors never return any value.
Constructors allocate space for the object in memory.
Constructors cannot be overloaded.
Constructors always have the name same as the name of the class.
Constructors are never called explicitly.
Constructors never return any value.
Constructors allocate space for the object in memory.
- 1, 3, 5
- 2, 3, 4
- 3, 5
- 4, 5
9. How many times can a constructor be called during lifetime of the object?
- As many times as we call it.
- Only once.
- Depends upon a Project Setting made in Visual Studio.NET.
- Any number of times before the object gets garbage collected.