C# .NET interview questions

C# .NET quiz questions

  • 1.

    Mention the assembly name where System namespace lies in C#?

    Answer:

    Assembly Name – mscorlib.dll

    View
  • 2.

    Do we get error while executing “finally” block in C#?

    Answer:

    Yes. We may get error in finally block.

    View
  • 3.

    List out two different types of errors in C#?

    Answer:

    Below are the types of errors in C# -

    • Compile Time Error
    • Run Time Error
    View
  • 4.

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

    Answer:

    Yes we can have only try block without catch block.

    View
  • 5.

    What is the difference between “throw ex” and “throw” methods in C#?

    Answer:

    • “throw ex” will replace the stack trace of the exception with stack trace info of re throw point.
    • “throw” will preserve the original stack trace info.
    View
  • 6.

    What is the difference between “finalize” and “finally” methods in C#?

    Answer:

    • Finalize – This method is used for garbage collection. So before destroying an object this method is called as part of clean up activity.
    • Finally – This method is used for executing the code irrespective of exception occurred or not.
    View
  • 7.

    Why to use “finally” block in C#?

    Answer:

    “Finally” block will be executed irrespective of exception. So while executing the code in try block when exception is occurred, control is returned to catch block and at last “finally” block will be executed. So closing connection to database / releasing the file handlers can be kept in “finally” block.

    View
  • 8.

    Can we execute multiple catch blocks in C#?

    Answer:

    No. Once any exception is occurred it executes specific exception catch block and the control comes out.

    View
  • 9.

    What is the difference between “dispose” and “finalize” variables in C#?

    Answer:

    • Dispose - This method uses interface – “IDisposable” interface and it will free up both managed and unmanaged codes like – database connection, files etc.
    • Finalize - This method is called internally unlike Dispose method which is called explicitly. It is called by garbage collector and can’t be called from the code.
    View
  • 10.

    Explain “static” keyword in C#?

    Answer:

    “Static” keyword can be used for declaring a static member. If the class is made static then all the members of the class are also made static. If the variable is made static then it will have a single instance and the value change is updated in this instance.

    View
  • 11.

    What is the difference between “constant” and “readonly” variables in C#?

    Answer:

    • “Const” keyword is used for making an entity constant. We cannot modify the value later in the code. Value assigning is mandatory to constant variables.
    • “readonly” variable value can be changed during runtime and value to readonly variables can be assigned in the constructor or at the time of declaration.
    View
  • 12.

    Why to use keyword “const” in C#? Give an example.

    Answer:

    “Const” keyword is used for making an entity constant. We can’t reassign the value to constant.

    Eg: const string _name = "Test";
    View
  • 13.

    Explain namespaces in C#?

    Answer:

    Namespaces are containers for the classes. We will use namespaces for grouping the related classes in C#. “Using” keyword can be used for using the namespace in other namespace.

    View
  • 14.

    Why to use “using” in C#?

    Answer:

    “Using” statement calls – “dispose” method internally, whenever any exception occurred in any method call and in “Using” statement objects are read only and cannot be reassignable or modifiable.

    View
  • 15.

    List out the differences between Array and ArrayList in C#?

    Answer:

    • Array stores the values or elements of same data type but arraylist stores values of different datatypes.
    • Arrays will use the fixed length but arraylist does not uses fixed length like array.
    View
  • 16.

    Give an example of using sealed class in C#?

    Answer:

    Below is the sample code of sealed class in C# -

    class X {} 
    sealed class Y : X {}
    
    Sealed methods –
    
    class A
    {
     protected virtual void First() { }
     protected virtual void Second() { }
    }
    class B : A
    {
     sealed protected override void First() {}
     protected override void Second() { }
    }

    If any class inherits from class “B” then method – “First” will not be overridable as this method is sealed in class B.

    View
  • 17.

    Explain sealed class in C#?

    Answer:

    Sealed class is used to prevent the class from being inherited from other classes. So “sealed” modifier also can be used with methods to avoid the methods to override in the child classes.

    View
  • 18.

    Explain the types of comments in C#?

    Answer:

    Below are the types of comments in C# -

    • Single Line Comment Eg : //
    • Multiline Comments Eg: /* */
    • XML Comments Eg : ///
    View
  • 19.

    What are IDE’s provided by Microsoft for C# development?

    Answer:

    Below are the IDE’s used for C# development –

    • Visual Studio Express (VCE)
    • Visual Studio (VS)
    • Visual Web Developer

     

    View
  • 20.

    List some of the advantages of C#?

    Answer:

    Below are the advantages of C# -

    • Easy to learn
    • Object oriented
    • Component oriented
    • Part of .NET framework
    View

© 2017 QuizBucket.org