Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Constructors and Destructors Aptitude Questions and Answers
C# Constructors and Destructors Aptitude Questions: This section contains aptitude questions and answers on C# Constructors and Destructors.
Submitted by Nidhi, on March 24, 2020
1) There are following statements are given below, which of them are correct about constructors in C#.NET?
- Constructors are not required in a class.
- Constructors are used to initializing data members of the class.
- The constructor name and class name must be the same.
- Contractors are old technique, now a day they are deprecated.
Options:
- Only A
- Only D
- B and C
- A and D
Correct answer: 3
B and C
B and C are correct statements about constructors in C#.NET.
2) There are following statements are given below, which of them are NOT correct about constructors in C#.NET?
- A constructor does not have any return type.
- Constructors can be overloaded.
- Constructors cannot be overloaded.
- A constructor is called when an object of the class gets created.
Options:
- Only A
- Only B
- Only C
- Only D
Correct answer: 3
Only C
Only C statement is not correct about construct in C#.NET.
3) In C#.NET, constructor is a?
- Method
- Class
- Variable
- Keyword
Correct answer: 1
Method
In C#.NET, a constructor is a special type of method, which is used to initialize data members of a class.
4) If we do not create a constructor in a class, then compiler inserts a default constructor automatically?
- Yes
- No
Correct answer: 1
Yes
Yes it is true.
5) Can we create a constructor with an empty body in C#.NET?
- Yes
- No
Correct answer: 1
Yes
Yes it is true.
6) In C#.NET, how many times we can call a constructor during the lifetime of the object?
- It depends upon the setting in Visual studio.
- Any number of times it can be called by the programmer explicitly.
- Only One time.
- 2 times only.
Correct answer: 3
Only One time.
A constructor is called only one time when an object gets created.
7) We can use default arguments in constructors also?
- Yes
- No
Correct answer: 1
Yes
Yes it is true.
8) There are the following options are given below, which is not the type of constructor in C#.NET?
- Default Constructor
- Copy Constructor
- Parameterized constructor
- Body Constructor
Correct answer: 4
Body Constructor
In C#.NET, Body constructor is not a type of constructor.
9) There are following statements are given below, which of them are correct in C#.NET?
- A constructor is used to write code in a simple manner.
- A class can contain multiple destructors.
- A constructor is basically used to set default values in data members associated with an object.
- A copy constructor is used in C#.NET
Opyions:
- Only A
- C and D
- Only C
- Only D
Correct answer: 2
C and D
In the above statements, only C and D statements are correct.
10) Can we pass a parameter to the destructor in C#.NET?
- Yes
- No
Correct answer: 2
No
No, we cannot pass any parameter to the destructor.
11) There are the following options are given below, in which option we need to use a tilde (~) operator?
- Default constructor
- Copy constructor
- Destructor
- Super constructor
Correct answer: 3
Destructor
In C#.NET, the tilde (~) operator is used with a destructor.
12) There are following statements are given below, which of them are correct about destructors in C#?
- A destructor is a special type of method, it automatically invokes when an object of the class gets destroyed.
- We need to use a tilde (~) operator to define a destructor.
- A destructor is used to reload the data member's value.
- We can create multiple destructors in a class.
Options:
- Only A
- A and B
- Only C
- C and D
13) A destructor has the same name as class name in C#.NET?
- Yes
- No
Correct answer: 1
Yes
Yes it is true.
14) What is the return type of a destructor?
- Void
- Integer
- It does not have any return type
- It returns an object of the structure
Correct answer: 3
It does not have any return type
A destructor does not have any return type.
15) What is the correct output of given code snippets?
using System;
public class Example
{
int X;
int Y;
public static Example()
{
X = 10;
Y = 10;
}
public void show()
{
Console.WriteLine(X + " " + Y);
}
static void Main(string[] args)
{
Example Ob = new Example();
Ob.show();
}
}
- 10 10
- 1o 1o
- Runtime Exception
- Syntax Error
Correct answer: 4
Syntax Error
We cannot use "static" keyword constructor.
16) What is the correct output of given code snippets?
using System;
public class Example
{
int X;
int Y;
public example()
{
X = 10;
Y = 10;
}
public void show()
{
Console.WriteLine(X + " " + Y);
}
static void Main(string[] args)
{
Example Ob = new Example();
Ob.show();
}
}
- 10 10
- 1o 1o
- Runtime Exception
- Syntax Error
Correct answer: 4
Syntax Error
C# is a case sensitive language, in the above program constructor name is different from the class name.
17) What is the correct way to create an object of a given class?
public class Example
{
int X;
int Y;
public Example(int x, int y)
{
X = x;
Y = y;
}
}
- Example E = new Example();
- Example E = Example(10,20);
- Example E = new Example(10,20);
- Example E(10,20);
Correct answer: 3
Example E = new Example(10,20);
3rd option is the correct way to create an object of a given class.
18) What are the correct ways to define constructors of given code snippets?
Employee E1 = new Employee ("Shaurya");
Employee E2 = new Employee ("Shaurya", 5);
Code 1:
public Employee(string n)
{
name = n;
age = 5;
}
public Employee(string n, int a)
{
name = n;
age = a;
}
Code 2:
public Employee(int a)
{
name = "shaurya";
age = a;
}
public Employee(string n, int a)
{
name = n;
age = a;
}
Code 3:
public Employee()
{
name = "shaurya";
age = 5;
}
public Employee(string n, int a)
{
name = n;
age = a;
}
Code 4:
public Employee(int a)
{
name = "shaurya";
age = a;
}
public Employee(int n, sting a)
{
name = n;
age = a;
}
- Code 1
- Code 2
- Code 3
- Code 4
Correct answer: 1
Code 1
The Code 1 is the correct way to define constructors.
19) Can we create a private constructor in a class?
- Yes
- No
Correct answer: 1
Yes
Yes it is true.
20) Can we use the private keyword to define destructor in class?
- Yes
- No
Correct answer: 2
No
We cannot use the private keyword to define a destructor of the class.
21) Can we use the public keyword to define destructor in class?
- Yes
- No
Correct answer: 2
No
We cannot use the public keyword to define a destructor of the class.
22) There are following statements are given below, which of them are correct about copy constructor in C#?
- A copy constructor is an old technique, it is deprecated nowadays.
- A copy constructor is not supported in C#.NET.
- A copy constructor is used to initialize an object from an existing object.
- A copy constructor is also known as a no-argument constructor.
Options:
- Only A
- Only B
- C and D
- Only C
Correct answer: 4
Only C
Only C statement is correct about copy constructor.
23) There are following constructors are given below, which is also known as no argument constructor?
- Default constructor
- Parameterized constructor
- Copy constructor
- Body constructor
Correct answer: 1
Default constructor
A default constructor is also known as a no-argument constructor.
24) What is the correct output of given code snippets?
using System;
class Employee
{
string name;
int age;
public Employee()
{
name = "shaurya";
age = 5;
}
public Employee(Employee E)
{
name = E.name;
age = E.age;
}
public void show()
{
Console.WriteLine(name + " " + age);
}
static void Main(string[] args)
{
Employee e1 = new Employee();
Employee e2 = new Employee(e1);
e1.show();
e2.show();
}
}
- shaurya 5
shaurya 5
- shaurya
shaurya
- Runtime Exception
- Syntax Error
Correct answer: 1
shaurya 5
shaurya 5
1st option is the correct answer for the code snippets.
25) What is the correct output of given code snippets?
using System;
class Employee
{
string name;
int age;
public Employee()
{
this.name = "shaurya";
age = 5;
}
public Employee(Employee E)
{
name = E.name;
this.age = E.age;
}
public void show()
{
Console.WriteLine(name + " " + age);
}
static void Main(string[] args)
{
Employee e1 = new Employee();
Employee e2 = new Employee(e1);
e1.show();
e2.show();
}
}
- shaurya 5
shaurya 5
- shaurya
shaurya
- Runtime Exception
- Syntax Error
Correct answer: 1
shaurya 5
shaurya 5
1st option is the correct answer for the code snippets.