Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

C# Interview questions and answers for freshers and experienced - Part One


Share with WhatsApp


Hello friend, in this post I am sharing some C# interview questions with their answers. I had shared advanced level interview questions before this but most of my friends has asking me about some interview questions for freshers and experience range of 1-3 years so have decided to share some with this series. Hope you will get benefited.

Can we declare string variables like below?

string a,b,c = string.Empty;

If yes then what will be the value of a,b and c?

Yes it is possible to declare variables in above way.
Many developers has habit of declaring each variable separately so sometime they gets confused after seeing code like this.
Coming to the sub question what will the values of a,b and c.  In above case c will have string.Empty value on the other hand a & b will have null value as we haven't set any for those. All will have string.Empty value if you had set like below

string a = string.Empty,b=sting.Empty,c=string.Empty;

What is faster a Class or Structure?

When it comes to the difference between class and structure you know Class is reference type and structure is value type but when it comes to the which is faster among them then there is long discussion and most of the time no confirmed output.

To answer this question, as struct is a value type it gets allocated on stack or inline in containing types so if we consider struct independently then it is faster to instantiate & to destroy as compare to class which gets allocated on heap.

In my point of view when to use which is more important and it depends on some other considerations such as size of type, its logical representation,frequency of it being get boxed,mutable or immutable etc.
I will recommend reading this post on MSDN to have more idea on choosing between class and struct.

Which Constructor gets called first ? Parent Class or child class?

This is very basic question now days but still has capability to confuse freshers during interviews. Coming to the answer, Base class constructor always gets called first and rather memorizing it you can think it like you have derived class to add some more functionality to the base class so to add something you need a base similar case is with this too.

Point to note, though base class constructor gets called first derived class initializer gets called before base class initializer. Same case with destructor too, dervied class destructor gets invoked before the base class destructor.
 

Use of ? in C#

You might be using ? in your code many times but when it gets specifically asked what is the use of ? in C# then sometimes you not able to recall which makes this as an interview question.
Coming to the answer. Below are soem usage of "?" in C#

Nullable type :

To declare a nullable type ? is gets used.
e.g. 

int? num = null;

Nullable types are instances of the System.Nullable<T> struct. A nullable type can represent the correct range of values for its underlying value type, plus an additional null value.

To know more about Nullable Type,read this MSDN post.

?? Operator (null-coalescing operator) which returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand. It mostly gets used with nullable types.

int? x = null;
int y = x ?? -1;

In above example if x is null then y will have -1 value else x's value.

Conditional operator (?:)

? gets used in conditional operator which returns one of two values depending on the value of a Boolean expression.

int i = 6;
string str = (i >3 ? "yes": "no");

In above example conditional operator is used. So if i > 3 condition is true str will have value "yes" else value "no"

What is the use of "using" statement in c#?

The "using" statement provides convenient syntax that ensures the correct use of IDisposable objects. Within the using block, the object is read-only and cannot be modified or reassigned.

To use "using" statement for particular type it must implement IDisposable interface. When you use "using" statement.

When you wrap a IDisposable objects with using statement compiler converts it to try....finally block and object gets disposed in finally block. Read more about it here.


Let me know your thoughts about above questions in comment section below. Thanks.

Go to Part Two



If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Advanced Interview Questions and answers on C#.NET - Part 1

In this post we will discuss some advanced interview questions on C#.NET. which will help you to understand the type of questions asked in interviews for senior software engineer post.


C# Interview questions and answers for freshers and experienced - Part Two

In this second post I will share questions like Default Access Modifier for Class,Generics in C#,Static class inheritance is possible or not along with different types of JIT compiler.


How to Prepare for the interviews - Simple but important tips - Part 3 - Prepare for HR Round

In this third post of my interview tips series, I am going to share some tips about how to prepare for HR Round. Also about things to do and things to not to do during the same.


How to Prepare for the interviews - Simple but important tips - Part 1 - Resume Writing

In this post I will share some interview tips from my own personal experience which will help you to prepare for the interview. In part one I will cover how to design a resume which will put your first impression smartly.


How to Prepare for the interviews - Simple but important tips - Part 2 - Things that matter during interview

In this second post of my interview tips series, I am going to share some tips about how to behave during the interview. Also about how to prepare for the interview and maintain self confidence during the interview.


Your opinion is valuable for us! Comments, suggetions are welcome.


Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!

x