Microsoft .NET Interview Questions and Answers
Experienced / Expert level questions & answers
Ques 1. .NET Stands for?
Ques 2. What is the default accessibility for members of a struct?
Private
Ques 3. Can the members of an interface be private?
No
Ques 4. 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)
{
}
Ques 5. What are the two return types for main?
void and int
Ques 6. 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.
Ques 7. 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());
}
}
}
}
Ques 8. Can you use access modifiers with destructors?
No
Ques 9. What is a delegate useful for?
The main reason we use delegates is for use in event driven programming.
Ques 10. Are events synchronous of asynchronous?
Asynchronous
Ques 11. 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.
Ques 12. 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);
}
}
}
Ques 13. Is a struct stored on the heap or stack?
Stack
Ques 14. Can C# have global overflow checking?
Yes
Ques 15. 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.
Ques 16. What doe the stackalloc operator do?
Allocate a block of memory on the stack (used in unsafe mode).
Most helpful rated by users:
- Name 10 C# keywords.
- What is public accessibility?
- .NET Stands for?
- What is private accessibility?
- What is protected accessibility?
Related interview subjects
ASP interview questions and answers - Total 82 questions |
C# interview questions and answers - Total 41 questions |
LINQ interview questions and answers - Total 20 questions |
ASP .NET interview questions and answers - Total 31 questions |
Microsoft .NET interview questions and answers - Total 60 questions |