Q & A - CSharp Question & Answers

 

Q&A 1.

What is CSharp (C#)?

C# is type-safe, managed and object oriented language, which is compiled by .Net framework for generating intermediate language (IL).

Q&A 2.

Explain a sealed class in C#.

Sealed class is used to prevent the class from being inherited from other classes. Also sealed "modifier" can be used with methods to avoid the methods to be overridden in child classes.

Q&A 3.

Why to use using keyword in C#?

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

Q&A 4.

Two ways to use Using keyword in C#.

Using keyword can be used in two different ways and are hence referred to as differently.
As a Directive, in this usage, the "using" keyword can be used to include a namespace in your programme.
As a Statement, Using can also be used in a block of code to dispose a IDisposable objects. This plays a vital role in improving performance in Garbage Collection.

For ex:

using (SqlConnection conn = new SqlConnection(connString))  
{
      SqlCommand cmd = conn.CreateCommand();  
      cmd.CommandText = "SELECT * FROM Customers";  
      conn.Open();    
      ...
}

In the preceding code a connection to the SQL will be closed automatically while exiting using statement.

Q&A 5.

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

Dispose method uses the "IDisposable" interface and it will free up both managed and unmanaged codes like – database connection, files etc.
While finalize method is called internally unlike Dispose method which is called explicitly. It is called by garbage collector and cannot be called from within the code.

Q&A 6.

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

"Const" keyword is used to make an entity or a variable constant. We cannot reassign the value to constant.
Eg: const string _username = "Ali";

Q&A 7.

Explain the types of comments in C#?

Below are the types of comments in C# –

  • Single Line Comment Eg : //
  • Multiline Comments Eg: /* */
  • XML Comments Eg : ///

Q&A 8.

Why are strings in C# immutable?

Immutable means string values cannot be changed once they have been created. Any modification to a string value results in a completely new string instance, thus makes an inefficient use of memory and garbage collector. Therefore we should use mutable System.Text.StringBuilder class when string values are going to be changed.

Q&A 9.

What is coupling?

OOPS Modules are dependent on each other. Coupling refers to level of dependency between two software modules.
Two modules are called highly dependent on each other when you have make change in one module and then  you have to make change in dependent module every time.
Loose Coupling is always preferred. Inversion of Control and dependency injections are some techniques for getting loose coupling in modules.

Q&A 10.

How is this pointer supporting OOPS concept?

The this pointer is basically a way for a data structure (object) to be able to access methods that allow itself to perform operations on its own data. It is a way to manage state within a data structure.


Related Quiz Content

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