Q & A - CSharp Question & Answers

 

Q&A 1.

What is Encapsulation in C#?

Encapsulation helps bind the data and behaviors in one object. Only defined behaviors in a class can modify the data. We hide the state of an object by using properties and methods.
Clients of the object can modify data using these methods and behaviors while we can protect the data using access specifiers such as private and protected.

Q&A 2.

What is the difference between a struct and a class?

Structs cannot be inherited, structs are passed by value and not by reference. Structs are stored on the stack not the heap. The result is better performance with Structs.

Q&A 3.

Can we have only "try" block without "catch" block in C#?

Yes we can have only try block without catch block in C#.

Q&A 4.

How do you prevent a class from being inherited?

The sealed keyword makes prohibit a class from being inherited.

Q&A 5.

Explain circular reference in C#?

This is a situation where in, multiple resources are dependent on each other and this causes a lock condition and this makes the resource to be unused or unavialable.

To handle the problem of circular references in C#, you should use garbage collection. It detects and collects circular references. The garbage collector begins with local and static and it marks each object that can be reached through their children.

Q&A 6.

What is the GAC, and where is it located?

The GAC is the Global Assembly Cache. Shared assemblies reside in the GAC; this allows applications to share assemblies instead of having the assembly distributed with each application. Versioning allows multiple assembly versions to exist in the GAC—applications can specify version numbers in the config file. The gacutil command line tool is used to manage the GAC.

There are two ways to remove an assembly from the global assembly cache (GAC): By using the Global Assembly Cache tool (Gacutil.exe). You can use this option to uninstall assemblies that you've placed in the GAC during development and testing.

 

For Eg: gacutil –u <assembly name>

Q&A 7.

Will a base class constructor will be invoked while calling derived class?

The base class's constructors are called in the order of inheritance and then the derived class's constructor.
For ex:

 

class Program  
    {  
        static void Main(string[] args)  
        {  
            Derived derivedObj= new Derived();  
            Console.ReadLine();  
  
        }  
    }  
    public class Base  
    {  
        public Base()  
        {  
            Console.WriteLine("This is a base class");  
        }  
    }  
  
    public class Derived : Base  
    {  
        public Derived()  
        {  
            Console.WriteLine("This is a derived class");  
        }  
    }   

Even though we are creating an object of the derived class, it will invoke the base class first.

Q&A 8.

How to create a constructor for a class in C#?

In C#, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C# has the same name as class or struct. There can be two types of constructors in C#.

  1.     Default constructor
  2.     Parameterized constructor


Note : A constructor should not have a return type.

public class Base
{
    public int x;
    public Base()
    {
        x = 5;
    }
}

class TestClass
{
    static void Main()
    {
        Base b = new Base();
        Console.WriteLine("The integer value is : " + b);
    }
}

 

Q&A 9.

What is method overloading in C#?

Two or more than two methods having the same name but different parameters is what we call method overloading in C#.
Methods are overloaded via different signatures ie. method overloading can be performed in C# different data types, different number of parameters, or a different order of parameters.

Q&A 10.

What is the difference between a struct and a class?

Structs cannot be inherited, structs are passed by value and not by reference. Structs are stored on the stack not the heap whereas Classes are stored on the heap. The result is getting better performance while using Structs.


Related Quiz Content

x
This website is using cookies to make user experience of the site better. More info. OK