HCL C#.Net Interview Question and Answer



Q. What is Overriding?

Overriding is a concept where a method in a derived class uses the same name with the same return type and also the same arguments as a method in its base class. In other words, if the derived class contains its own implementation of the method rather than using the method in the base class, the process is called overriding.
Example:
1.
class BC
{
  public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  new public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class TC : BC
{
  new public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;
     b = new BC();
     b.Display();   

     b = new DC();
     b.Display();   

     b = new TC();
     b.Display();   
  }
}
Output         
BC::Display
BC::Display
BC::Display
2.
Solve Problem using Virtual and Override keyword.
class BC
{
  public virtual void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  public override void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;
     b = new BC();
     b.Display();   

     b = new DC();
     b.Display();   
  }
}
Output
BC::Display
DC::Display

2.What are events and delegates?

Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts. Events are used for inter-process communication.
Example:
using System;
namespace SimpleEvent
{
   using System;
   
   public class EventTest
   {
      private int value;
       
      public delegate void NumManipulationHandler();
      public event NumManipulationHandler ChangeNum;
 
      protected virtual void OnNumChanged()
      {
         if (ChangeNum != null)
         {
            ChangeNum();
         }
         else
         {
            Console.WriteLine("Event fired!");
         }
      }
      
      public EventTest(int n )
      {
         SetValue(n);
      }
      
      public void SetValue(int n)
      {
         if (value != n)
         {
            value = n;
            OnNumChanged();
         }
      }
   }
   
   public class MainClass
   {
      public static void Main()
      {
         EventTest e = new EventTest(5);
         e.SetValue(7);
         e.SetValue(11);
         Console.ReadKey();
      }
   }
}
When the above code is compiled and executed, it produces the following result:
Event Fired!
Event Fired!
Event Fired!

Delegate :

A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.
Example:
using System;

namespace Akadia.BasicDelegate
{
    // Declaration
    public delegate void SimpleDelegate();

    class TestDelegate
    {
        public static void MyFunc()
        {
            Console.WriteLine("I was called by delegate ...");
        }

        public static void Main()
        {
            // Instantiation
            SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

            // Invocation
            simpleDelegate();
        }
    }
}
Compile an test:
# csc SimpleDelegate1.cs
# SimpleDelegate1.exe
I was called by delegate .



3.Can multiple catch blocks be executed?

No, Multiple catch blocks can't be executed. Once the proper catch code executed, the control is transferred to the finally block and then the code that follows the finally block gets executed.




4. Difference between public, static and void?

All these are access modifiers in C#. Public declared variables or methods are accessible anywhere in the application. Static declared variables or methods are globally accessible without creating an instance of the class. The compiler stores the address of the method as the entry point and uses this information to begin execution before any objects are created. And Void is a type modifier that states that the method or variable does not return any value.

5. Difference between ref & out parameters?

An argument passed as ref must be initialized before passing to the method whereas out parameter needs not to be initialized before passing to a method

6. Can “this” be used within a static method?

We can’t use ‘This’ in a static method because we can only use static variables/methods in a static method.


7. What is difference between constants and read-only?

Constant variables are declared and initialized at compile time. The value can’t be changed after wards. Read-only variables will be initialized only from the Static constructor of the class. Read only is used only when we want to assign the value at run time.

8. What is an interface class?

Interface is an abstract class which has only public abstract methods and the methods only have the declaration and not the definition. These abstract methods must be implemented in the inherited classes.

9. What are value types and reference types?

Value types are stored in the Stack whereas reference types stored on heap. Following are some of the Value types:
int, enum, byte, decimal, double, float, long
Reference Types:
string, class, interface, object.

10. Different categories of inheritance?

There are 4 types of Inheritance in Object Oriented Programming and they are as follows
(i)Single inheritance : Have one base class and one derived class.
(ii)Hierarchical inheritance : Have one base class and multiple derived classes of the same base class.
(iii)Multilevel inheritance : Have a class derived from a derived class.
(iv)Multiple inheritance : Have several base classes and a derived class.

11. Difference between constants, readonly and, static ?


(i) Constants: Value can’t be changed.
(ii) Read-only: Value will be initialized only once from the constructor of the class.
(iii)Static: Value can be initialized once.


12. What is multi cast delegates?

Each delegate object holds reference to a single method. However, it is possible for a delegate object to hold references of and invoke multiple methods. Such delegate objects are called multicast delegates or combinable delegates.
public partial class Form1 : Form
{
public delegate void MyDelegate();
private void Method1()
{
MessageBox.Show(“Method1 Invoked”);
}
//lock code start
private void Method2()
{
MessageBox.Show(“Method2 Invoked”);
}
//lock code End
private void button1_Click(object sender, EventArgs e)
{
 
//2.create delegate referance
MyDelegate myptr = null;
 
//3.point the referance to Add function
myptr += this.Method1;
 
////lock code start
myptr += this.Method2;
 
////lock code end
//4.invoke the method through delegate object
myptr.Invoke();
       }
public Form1()
{
InitializeComponent();
}
}

13. What is data encapsulation?
Encapsulation is hiding the code and data into a single unit to protect the data from outer world. Data encapsulation, also referred to as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user. The user can only perform a restricted set of operations on the hidden members of the class by executing special functions called methods.
Abstraction:
Abstraction refers to the act of representing essential features without including the background details or explanations.

14. Is it possible to override private virtual methods?
No. Private methods are not accessible outside the class.

15. Can you store multiple data types in System.Array?
No.

16. What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?

The System.Array.CopyTo() a deep copy of the array, the System.Array.Clone() is shallow.

Comments