Introduction
In this article, I would like to discuss some of the interesting c# interview questions,This post will be updated with new interesting c# interview questions.
Question and Answer
Q1 . Consider following code snippets
1 2 |
int i=5; i=(i++)+i+(++i); |
what will be the value of variable i after these two lines?
Ans : 18(5+6+7)
Hint :
i++ will return 5 and then increment to 6
++i will increment 6 to 7 and then return 7
like a[][][][]….[]
Ans : C# array support maximum 32 dimensions. refer remark section from msdn.
1 |
MethodName(); |
Ans : yes,For c# newbies this might be a tricky question 😇. but it is really simple and you must have done this before.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//define functions string GetString() { return "Message"; } bool Validation(int i) { if (i != 0) return true; else return false; } //call functions without semicolon MessageBox.Show(GetString()); if (Validation(5)) { //do somthing here } |
Semicolon is just to indicate the end of a statement.
Q4 . Consider the following snippet
1 2 3 4 |
if (new bool()) { Console.WriteLine("Hello World"); } |
Does it print “Hello World” ? why ?
Ans : No,
Reason :
new bool() returns false
false is the default value of bool data type.in c#
1 |
new DataTypeClass() |
will return their default value. you can refer of all c# datatype from msdn.
Q5 . if C# string is a immutable class , consider following code snippet
1 2 3 4 |
string str = "ABCD"; str += ""; str = str.Replace('x', 'Q'); str = str.Trim(); |
How many instance will be created for the above code snippet ?
Ans : Only one instance will be created.C# string is a immutable,But it won’t create new instance if the operation does not change it’s value.
For more detailed explanation you can my article on String Vs StringBuider in c#
Post A Reply