C# Interview Questions

C# interview questions image

Q: What does the term immutable mean?
The data value may not be changed.  Note: The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory and any changes are in fact copiesFor example System.String is immutable and System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed. StringBuilder is more efficient in cases where there is a large amount of string manipulation.  Strings are immutable, so each time a string is changed, a new instance in memory is created.
Q: What is a dangling else?

(see here for more information)
Q: What is checked { } and unchecked { }?
By default C# does not check for overflow (unless using constants), we can use checked to raise an exception.
Example:

Q: What’s the difference between const and readonly?
The difference is that static read-only can be modified by the containing class, but const can never be modified and must be initialized to a compile time constant. To expand on the static read-only case a bit, the containing class can only modify it: -- in the variable declaration (through a variable initializer). So, if the constant value is changed in an assembly, you need to replace all assemblies that use the constant values when application upgrade and deployment, but if  readonly value  is changed you need to replace the assembly that contains the change only.
Other difference is that const varaible type could be primitive types, but readonly variable type can be any type.
Q: What’s the difference between typeof(foo) and myFoo.GetType()?
First one takes type name and return result at compile time whereas second one takes object and return type at runtime using reflection ofcourse.
Q: What operators cannot be overloaded and write some code to overload an operator?
=    .    ? :     ->      new       is       sizeof       typeof
Q: If I have a constructor with a parameter, do I need to explicitly create a default constructor?
Yes
Q:What’s the difference between an interface and abstract class?
In an interface class, all methods are abstract - there is no implementation.  In an abstract class some methods can be concrete.  In an interface class, no accessibility modifiers are allowed.  An abstract class may have accessibility modifiers.
Q: Can you declare the override method static while the original method is non-static?
No, you can’t, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.
Q:What is a struct? Is a struct stored on the heap or stack? Can you derive from a struct?
struct 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. it is stored on stack and can not be derived.
Q: What is the difference between Finalize() and Dispose() and How we can support deterministic finalization?
Finalize called by GC and Dispose called by programmer to free up resources. using statement is quite handy and makes code quite efficient as right after end of using statement object created/declared in that statement disposed automatically means dispose method get called in deterministic way to free up memory. IDisposible is interface which classes implements to dispose unmanaged resources in deterministic manner.
Q: Can you allow a class to be inherited, but prevent the method from being over-ridden?
Yes.  Just leave the class public and make the method sealed. e.g.public sealed override void Method() {}
Q: From a versioning perspective, what are the drawbacks of extending an interface as opposed to extending a class?
With regard to versioning, interfaces are less flexible than classes. With a class, you can ship version 1 and then, in version 2, decide to add another method. As long as the method is not abstract (i.e., as long as you provide a default implementation of the method), any existing derived classes continue to function with no changes. Because interfaces do not support implementation inheritance, this same pattern does not hold for interfaces. Adding a method to an interface is like adding an abstract method to a base class--any class that implements the interface will break, because the class doesn't implement the new interface method.
Q: Write some code to implement a jagged array.
Q: What is the difference between: catch(Exception e){throw e;} and catch(Exception e){throw;} ?
Throw also return stack trace which throw e doesn’t

Q: Explain the use of override with new? what is shadowing?
Overriding means giving implementation of virtual/abstract/override-able method in derived class and if we use new it means it can’t be access through by any other class ref/object
The new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. Although you can hide members without the use of the new modifier, the result is a warning. Shadowing is somehow opposite to overriding as it lets redefine method implementation with signature as well.

Q: Write code to define and use your own custom attribute.
(From MSDN)

Q: What is a delegate and a multicast delegate?
A delegate is a variable that calls a method indirectly, without knowing its name. A delegate that has multiple handlers assigned to it is a Multicast delegate.

Q: What is Lambda Expression?
A Lambda expression is nothing but an Anonymous Function, can contain expressions and statements. Lambda expressions can be used mostly to create delegates or expression tree types. Lambda expression uses lambda operator => and read as 'goes to' operator. Left side of this operator specifies the input parameters and contains the expression or statement block at the right side.
Example: myExp = myExp/10;
Now, let see how we can assign the above to a delegate and create an expression tree:

Q: Write a standard lock() plus “double check” to create a critical section around a variable access
Q: How do you implement thread synchronization (Object.Wait, Notify,and CriticalSection) in C#?
You want the lock statement, which is the same as Monitor Enter/Exit:

Q: What are design patterns? Describe some common design patterns.
“Design patterns are recurring solutions to software design problems you find again and again in real-world application development.” They are used as a common design language during software development life cycle as well.

Creational Patterns
Abstract Factory:   Creates an instance of several families of classes
Builder:  Separates object construction from its representation
Factory Method:  Creates an instance of several derived classes
Prototype:   A fully initialized instance to be copied or cloned
Singleton:   A class of which only a single instance can exist
Structural Patterns
Adapter:   Match interfaces of different classes
Bridge:   Separates an object’s interface from its implementation
Composite:   A tree structure of simple and composite objects
Decorator:   Add responsibilities to objects dynamically
Façade:   A single class that represents an entire subsystem
Flyweight:   A fine-grained instance used for efficient sharing
Proxy:   An object representing another object
Behavioral Patterns
Chain of Resp.:   A way of passing a request between a chain of objects
Command:   Encapsulate a command request as an object
Interpreter:   A way to include language elements in a program
Iterator:   Sequentially access the elements of a collection
Mediator:   Defines simplified communication between classes
Memento:   Capture and restore an object's internal state
Observer:   A way of notifying change to a number of classes
State:   Alter an object's behavior when its state changes
Strategy:   Encapsulates an algorithm inside a class
Template Method:   Defer the exact steps of an algorithm to a subclass
Visitor:   Defines a new operation to a class without change


Comments

Popular Posts