Prepare Interview

Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Microsoft .NET Interview Questions and Answers

Test your skills through the online practice test: Microsoft .NET Quiz Online Practice Test

Freshers / Beginner level questions & answers

Ques 1. Name 10 C# keywords.

abstract, event, new, struct, explicit, null, base, extern, object, this

Is it helpful? Add Comment View Comments
 

Ques 2. What is public accessibility?

There are no access restrictions. All can access the public instance from anywhere.

Is it helpful? Add Comment View Comments
 

Ques 3. What is private accessibility?

Access is restricted to within the containing class.

Is it helpful? Add Comment View Comments
 

Ques 4. Class methods to should be marked with what keyword?

Static.

Is it helpful? Add Comment View Comments
 

Ques 5. Does an object need to be made to run main?

No

Is it helpful? Add Comment View Comments
 

Ques 6. Write a hello world console application.

using System;
namespace Console1
{
class Class1
{
[STAThread] // No longer needed
static void Main(string[] args)
{
Console.WriteLine("Hello world");
}
}
}

Is it helpful? Add Comment View Comments
 

Ques 7. What is recursion?

Recursion is when a method calls itself.

Is it helpful? Add Comment View Comments
 

Ques 8. What is a constructor?

A constructor performs initialisation for an object (including the struct type) or class.

Is it helpful? Add Comment View Comments
 

Ques 9. What is a destructor?

A C# destuctor is not like a C++ destructor. It is actually an override for Finalize(). This
is called when the garbage collector discovers that the object is unreachable. Finalize()
is called before any memory is reclaimed.

Is it helpful? Add Comment View Comments
 

Ques 10. Name 5 built in types.

Bool, char, int, byte, double

Is it helpful? Add Comment View Comments
 

Ques 11. Is string Unicode, ASCII, or something else?

Unicode

Is it helpful? Add Comment View Comments
 

Ques 12. Strings are immutable, what does this mean?

Any changes to that string are in fact copies.

Is it helpful? Add Comment View Comments
 

Ques 13. Name a few string properties.

trim, tolower, toupper, concat, copy, insert, equals, compare.

Is it helpful? Add Comment View Comments
 

Ques 14. What is boxing and unboxing?

Converting a value type (stack->heap) to a reference type (heap->stack), and viseversa.

Is it helpful? Add Comment View Comments
 

Ques 15. Write some code to box and unbox a value type.

// Boxing
int i = 4;
object o = i;
// Unboxing
i = (int) o;

Is it helpful? Add Comment View Comments
 

Ques 16. What is a heap and a stack?

There are 2 kinds of heap – 1: a chunk of memory where data is stored and 2: a tree
based data structure. When we talk about the heap and the stack we mean the first kind
of heap. The stack is a LIFO data structure that stores variables and flow control
information. Typically each thread will have its own stack.

Is it helpful? Add Comment View Comments
 

Ques 17. What is a pointer?

A pointer is a reference to a memory address.

Is it helpful? Add Comment View Comments
 

Ques 18. What does new do in terms of objects?

Initializes an object.

Is it helpful? Add Comment View Comments
 

Ques 19. What is a struct?

Unlike in C++ a struct is not a class – it is a value type with certain restrictions. It is
usually best to use a struct to represent simple entities with a few variables. Like a Point
for example which contains variables x and y.

Is it helpful? Add Comment View Comments
 

Ques 20. Describe 5 numeric value types ranges.

sbyte -128 to 127, byte 0 – 255, short -32,768 to 32,767, int -2,147,483,648 to
2,147,483,647, ulong 0 to 18,446,744,073,709,551,615

Is it helpful? Add Comment View Comments
 

Ques 21. Write code for a case statement.

switch (n)
{
case 1:
x=1;
break;
case 2:
x=2;
break;
default:
goto case 1;
}

Is it helpful? Add Comment View Comments
 

Intermediate / 1 to 5 years experienced level questions & answers

Ques 22. What is protected accessibility?

Access is restricted to types derived from the containing class.

Is it helpful? Add Comment View Comments
 

Ques 23. What is protected internal accessibility?

Access is restricted to types derived from the containing class or from files within the same assembly.

Is it helpful? Add Comment View Comments
 

Ques 24. What is the default accessibility for a class?

internal for a top level class, private for a nested one.

Is it helpful? Add Comment View Comments
 

Ques 25. What is the default accessibility for members of an interface?

public

Is it helpful? Add Comment View Comments
 

Ques 26. Methods must declare a return type, what is the keyword used when nothing is returned from the method?

Void

Is it helpful? Add Comment View Comments
 

Ques 27. What is a reference parameter?

Reference parameters reference the original object whereas value parameters make a
local copy and do not affect the original. Some example code is shown:
using System;
namespace Console1
{
class Class1
{
static void Main(string[] args)
{
TestRef tr1 = new TestRef();
TestRef tr2 = new TestRef();
tr1.TestValue = "Original value";
tr2.TestValue = "Original value";
int tv1 = 1;
int tv2 = 1;
TestRefVal(ref tv1, tv2, ref tr1, tr2);
Console.WriteLine(tv1);
Console.WriteLine(tv2);
Console.WriteLine(tr1.TestValue);
Console.WriteLine(tr2.TestValue);
Console.ReadLine();
}
static public void TestRefVal(ref int tv1Parm,
int tv2Parm,
ref TestRef tr1Parm,
TestRef tr2Parm)
{
tv1Parm = 2;
tv2Parm = 2;
tr1Parm.TestValue = "New value";
tr2Parm.TestValue = "New value";
}
}
}
class TestRef
{
public string TestValue;
}
The output for this is:
2
1
New value
New value

Is it helpful? Add Comment View Comments
 

Ques 28. What is an overloaded method?

An overloaded method has multiple signatures that are different.

Is it helpful? Add Comment View Comments
 

Ques 29. If I have a constructor with a parameter, do I need to explicitly create a default constructor?

Yes

Is it helpful? Add Comment View Comments
 

Ques 30. What is a delegate?

A delegate in C# is like a function pointer in C or C++. A delegate is a variable that calls
a method indirectly, without knowing its name. Delegates can point to static or/and
member functions. It is also possible to use a multicast delegate to point to multiple
functions.

Is it helpful? Add Comment View Comments
 

Ques 31. Write some code to use a delegate.

Member function with a parameter
using System;
namespace Console1
{
class Class1
{
delegate void myDelegate(int parameter1);
static void Main(string[] args)
{
MyClass myInstance = new MyClass();
myDelegate d = new myDelegate(myInstance.AMethod);
d(1); // <--- Calling function without knowing its name.
Test2(d);
Console.ReadLine();
}
static void Test2(myDelegate d)
{
d(2); // <--- Calling function without knowing its name.
}
}
class MyClass
{
public void AMethod(int param1)
{
Console.WriteLine(param1);
}
}
}
Multicast delegate calling static and member functions
using System;
namespace Console1
{
class Class1
{
delegate void myDelegate(int parameter1);
static void AStaticMethod(int param1)
{
Console.WriteLine(param1);
}
static void Main(string[] args)
{
MyClass myInstance = new MyClass();
myDelegate d = null;
d += new myDelegate(myInstance.AMethod);
d += new myDelegate(AStaticMethod);
d(1); //both functions will be run.
Console.ReadLine();
}
}
class MyClass
{
public void AMethod(int param1)
{
Console.WriteLine(param1);
}
}
}

Is it helpful? Add Comment View Comments
 

Ques 32. What is a value type and a reference type?

A reference type is known by a reference to a memory location on the heap.
A value type is directly stored in a memory location on the stack. A reference type is
essentially a pointer, dereferencing the pointer takes more time than directly accessing
the direct memory location of a value type.

Is it helpful? Add Comment View Comments
 

Ques 33. string is an alias for what?

System.String

Is it helpful? Add Comment View Comments
 

Ques 34. How do you dereference an object?

Set it equal to null.

Is it helpful? Add Comment View Comments
 

Ques 35. In terms of references, how do == and != (not overridden) work?

They check to see if the references both point to the same object.

Is it helpful? Add Comment View Comments
 

Ques 36. What is the default value for a bool?

False

Is it helpful? Add Comment View Comments
 

Ques 37. Write code for an enumeration.

public enum animals {Dog=1,Cat,Bear};

Is it helpful? Add Comment View Comments
 

Ques 38. Can a struct have methods?

Yes

Is it helpful? Add Comment View Comments
 

Ques 39. What is checked { } and unchecked { }?

By default C# does not check for overflow (unless using constants), we can use
checked to raise an exception. E.g.:
static short x = 32767; // Max short value
static short y = 32767;
// Using a checked expression
public static int myMethodCh()
{
int z = 0;
try
{
z = checked((short)(x + y));
//z = (short)(x + y);
}
catch (System.OverflowException e)
{
System.Console.WriteLine(e.ToString());
}
return z; // Throws the exception OverflowException
}
This code will raise an exception, if we remove unchecked as in:
//z = checked((short)(x + y));
z = (short)(x + y);
Then the cast will raise no overflow exception and z will be assigned –2. unchecked can
be used in the opposite way, to say avoid compile time errors with constanst overflow.
E.g. the following will cause a compiler error:
const short x = 32767; // Max short value
const short y = 32767;
public static int myMethodUnch()
{
int z = (short)(x + y);
return z; // Returns -2
}
The following will not:
const short x = 32767; // Max short value
const short y = 32767;
public static int myMethodUnch()
{
int z = unchecked((short)(x + y));
return z; // Returns -2
}

Is it helpful? Add Comment View Comments
 

Ques 40. Can assignment operators be overloaded directly?

No

Is it helpful? Add Comment View Comments
 

Ques 41. What do operators is and as do?

as acts is like a cast but returns a null on conversion failure. Is comares an object to a
type and returns a boolean.

Is it helpful? Add Comment View Comments
 

Ques 42. What is the difference between the new operator and modifier?

The new operator creates an instance of a class whereas the new modifier is used to
declare a method with the same name as a method in one of the parent classes.

Is it helpful? Add Comment View Comments
 

Ques 43. Explain sizeof and typeof.

typeof obtains the System.Type object for a type and sizeof obtains the size of a type.

Is it helpful? Add Comment View Comments
 

Ques 44. Contrast ++count vs. count++.

Some operators have temporal properties depending on their placement. E.g.
double x;
x = 2;
Console.Write(++x);
x = 2;
Console.Write(x++);
Console.Write(x);
Returns
323

Is it helpful? Add Comment View Comments
 

Experienced / Expert level questions & answers

Ques 45. .NET Stands for?

About 10 years ago, I was part of the large team in Redmond working on the set of projects which became ".net". This was during the time the decisions were being made about what to name this work. I can tell you from first-hand experience that ".net" is not an acroynm.

Instead, the James Kovacs blog post that Jim W posted is accurate: ".net" was one of many names that the teams cycled through (and thankfully rejected) before settling on ".net". The name was chosen because it:

mirrored the domain suffix of (at the time) every ISP, so was intended to remind users that "web-enabling your software" was the core scenario being targetted by this work
was more approachable to business types and CIOs than geekier names like "Universal Runtime" or "COM+ 2.0"
had practical benefits like: being short, easy to spell, globalized well, could leverage already-owned domain names for every Microsoft product, etc.
actually passed legal/trademark review (surprisingly difficult!)
So it was intended to mean something, but more so by connotation rather than directly abbreviating or describing something. In other words, the name was only partly marketing nonsense! ;-)

More trivia

I don't remember the exact positioning (it's been 10 years!), but I believe that the name ".net" was supposed cover three basic things:

".net Framework" - a new framework for writing web-enabled apps
".net web services" - a way of accessing Microsoft (and others') software over the web programmatically using open standards and protocols (anyone remember "Hailstorm"?)
".net enterprise servers" - a set of products which made bulding web-enabled applications easier.
In practice, only the first meaning stuck with users. The others morphed into other names (e.g. "Windows Server System") or were genericized by the public (e.g. "web services", SOA, etc.). Anyway, that's why you don't see Microsoft products named "<product name here>.NET Server" any more-- Microsoft wisely decided to limit the ".net" name to the things that developers actually thought of as ".net"!

BTW, in addition to being short and easy to spell and say, ".net" as a name also helped with the web services strategy which Microsoft was persuing at the time, which revolved around (and still does) offering software which was also available in the cloud. The idea was that we'd have, for example, Office.com for a hosted UI version, and Office.net for the APIs. The name also was convenient since Microsoft already owned the .net domain-name variants for every microsoft product.

Is it helpful? Add Comment View Comments
 

Ques 46. What is the default accessibility for members of a struct?

Private

Is it helpful? Add Comment View Comments
 

Ques 47. Can the members of an interface be private?

No

Is it helpful? Add Comment View Comments
 

Ques 48. A class can have many mains, how does this work?

Only one of them is run, that is the one marked (public) static, e.g:
public static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
}
private void Main(string[] args, int i)
{
}

Is it helpful? Add Comment View Comments
 

Ques 49. What are the two return types for main?

void and int

Is it helpful? Add Comment View Comments
 

Ques 50. What is an out parameter?

An out parameter allows an instance of a parameter object to be made inside a method.
Reference parameters must be initialised but out gives a reference to an uninstanciated
object.

Is it helpful? Add Comment View Comments
 

Ques 51. Write code to show how a method can accept a varying number of parameters.

using System;
namespace Console1
{
class Class1
{
static void Main(string[] args)
{
ParamsMethod(1,"example");
ParamsMethod(1,2,3,4);
Console.ReadLine();
}
static void ParamsMethod(params object[] list)
{
foreach (object o in list)
{
Console.WriteLine(o.ToString());
}
}
}
}

Is it helpful? Add Comment View Comments
 

Ques 52. Can you use access modifiers with destructors?

No

Is it helpful? Add Comment View Comments
 

Ques 53. What is a delegate useful for?

The main reason we use delegates is for use in event driven programming.

Is it helpful? Add Comment View Comments
 

Ques 54. Are events synchronous of asynchronous?

Asynchronous

Is it helpful? Add Comment View Comments
 

Ques 55. Events use a publisher/subscriber model. What is that?

Objects publish events to which other applications subscribe. When the publisher raises
an event all subscribers to that event are notified.

Is it helpful? Add Comment View Comments
 

Ques 56. Can a subscriber subscribe to more than one publisher?

Yes, also - here's some code for a publisher with multiple subscribers.
using System;
namespace Console1
{
class Class1
{
delegate void myDelegate(int parameter1);
static event myDelegate myEvent;
static void AStaticMethod(int param1)
{
Console.WriteLine(param1);
}
static void Main(string[] args)
{
MyClass myInstance = new MyClass();
myEvent += new myDelegate(myInstance.AMethod);
myEvent += new myDelegate(AStaticMethod);
myEvent(1); //both functions will be run.
Console.ReadLine();
}
}
class MyClass
{
public void AMethod(int param1)
{
Console.WriteLine(param1);
}
}
}
Another example:
using System;
using System.Threading;
namespace EventExample
{
public class Clock
{
public delegate void TwoSecondsPassedHandler(object clockInstance,
TimeEventArgs time);
//The clock publishes an event that others subscribe to
public event TwoSecondsPassedHandler TwoSecondsPassed;
public void Start()
{
while(true)
{
Thread.Sleep(2000);
//Raise event
TwoSecondsPassed(this, new TimeEventArgs(1));
}
}
}
public class TimeEventArgs : EventArgs
{
public TimeEventArgs(int second)
{
seconds += second;
instanceSeconds = seconds;
}
private static int seconds;
public int instanceSeconds;
}
public class MainClass
{
static void Main(string[] args)
{
Clock cl = new Clock();
// add some subscribers
cl.TwoSecondsPassed += new
Clock.TwoSecondsPassedHandler(Subscriber1);
cl.TwoSecondsPassed += new
Clock.TwoSecondsPassedHandler(Subscriber2);
cl.Start();
Console.ReadLine();
}
public static void Subscriber1(object clockInstance, TimeEventArgs time)
{
Console.WriteLine("Subscriber1:" + time.instanceSeconds);
}
public static void Subscriber2(object clockInstance, TimeEventArgs time)
{
Console.WriteLine("Subscriber2:" + time.instanceSeconds);
}
}
}

Is it helpful? Add Comment View Comments
 

Ques 57. Is a struct stored on the heap or stack?

Stack

Is it helpful? Add Comment View Comments
 

Ques 58. Can C# have global overflow checking?

Yes

Is it helpful? Add Comment View Comments
 

Ques 59. What is explicit vs. implicit conversion?

When converting from a smaller numeric type into a larger one the cast is implicit. An
example of when an explicit cast is needed is when a value may be truncated.

Is it helpful? Add Comment View Comments
 

Ques 60. What doe the stackalloc operator do?

Allocate a block of memory on the stack (used in unsafe mode).

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

Related interview subjects

Microsoft .NET interview questions and answers - Total 60 questions
C# interview questions and answers - Total 41 questions
ASP .NET interview questions and answers - Total 31 questions
©2023 WithoutBook