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?
  1. Constructors are not required in a class.
  2. Constructors are used to initializing data members of the class.
  3. The constructor name and class name must be the same.
  4. Contractors are old technique, now a day they are deprecated.

Options:

  1. Only A
  2. Only D
  3. B and C
  4. A and D

2) There are following statements are given below, which of them are NOT correct about constructors in C#.NET?
  1. A constructor does not have any return type.
  2. Constructors can be overloaded.
  3. Constructors cannot be overloaded.
  4. A constructor is called when an object of the class gets created.

Options:

  1. Only A
  2. Only B
  3. Only C
  4. Only D

3) In C#.NET, constructor is a?
  1. Method
  2. Class
  3. Variable
  4. Keyword

4) If we do not create a constructor in a class, then compiler inserts a default constructor automatically?
  1. Yes
  2. No

5) Can we create a constructor with an empty body in C#.NET?
  1. Yes
  2. No

6) In C#.NET, how many times we can call a constructor during the lifetime of the object?
  1. It depends upon the setting in Visual studio.
  2. Any number of times it can be called by the programmer explicitly.
  3. Only One time.
  4. 2 times only.

7) We can use default arguments in constructors also?
  1. Yes
  2. No

8) There are the following options are given below, which is not the type of constructor in C#.NET?
  1. Default Constructor
  2. Copy Constructor
  3. Parameterized constructor
  4. Body Constructor

9) There are following statements are given below, which of them are correct in C#.NET?
  1. A constructor is used to write code in a simple manner.
  2. A class can contain multiple destructors.
  3. A constructor is basically used to set default values in data members associated with an object.
  4. A copy constructor is used in C#.NET

Opyions:

  1. Only A
  2. C and D
  3. Only C
  4. Only D

10) Can we pass a parameter to the destructor in C#.NET?
  1. Yes
  2. No

11) There are the following options are given below, in which option we need to use a tilde (~) operator?
  1. Default constructor
  2. Copy constructor
  3. Destructor
  4. Super constructor

12) There are following statements are given below, which of them are correct about destructors in C#?
  1. A destructor is a special type of method, it automatically invokes when an object of the class gets destroyed.
  2. We need to use a tilde (~) operator to define a destructor.
  3. A destructor is used to reload the data member's value.
  4. We can create multiple destructors in a class.

Options:

  1. Only A
  2. A and B
  3. Only C
  4. C and D

13) A destructor has the same name as class name in C#.NET?
  1. Yes
  2. No

14) What is the return type of a destructor?
  1. Void
  2. Integer
  3. It does not have any return type
  4. It returns an object of the structure

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();
    }
}
  1. 10 10
  2. 1o 1o
  3. Runtime Exception
  4. Syntax Error

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();
    }
}
  1. 10 10
  2. 1o 1o
  3. Runtime Exception
  4. Syntax Error

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;
    }
}
  1. Example E = new Example();
  2. Example E = Example(10,20);
  3. Example E = new Example(10,20);
  4. Example E(10,20);

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;
}
  1. Code 1
  2. Code 2
  3. Code 3
  4. Code 4

19) Can we create a private constructor in a class?
  1. Yes
  2. No

20) Can we use the private keyword to define destructor in class?
  1. Yes
  2. No

21) Can we use the public keyword to define destructor in class?
  1. Yes
  2. No

22) There are following statements are given below, which of them are correct about copy constructor in C#?
  1. A copy constructor is an old technique, it is deprecated nowadays.
  2. A copy constructor is not supported in C#.NET.
  3. A copy constructor is used to initialize an object from an existing object.
  4. A copy constructor is also known as a no-argument constructor.

Options:

  1. Only A
  2. Only B
  3. C and D
  4. Only C

23) There are following constructors are given below, which is also known as no argument constructor?
  1. Default constructor
  2. Parameterized constructor
  3. Copy constructor
  4. Body 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();
    }
}
  1. shaurya 5
    shaurya 5
  2. shaurya
    shaurya
  3. Runtime Exception
  4. Syntax Error

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();
    }
}
  1. shaurya 5
    shaurya 5
  2. shaurya
    shaurya
  3. Runtime Exception
  4. Syntax Error





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.