.NET interview questions for 5+ years of experience - OOPS, C#, WPF, .NET Fundamentals


Recently, I attended interview with NeST, Trivandrum and mode of the interview is telephonic. I used WPF, WCF in my last project. So they mainly concentrated in C#, .NET Fundamentals, WPF and OOPs concents. Most of the questions look common but some of the questions are new. I will try to post answers also.

1. What is the difference between abstract and interface?
2. Is it possible to use properties and events in an interface?
3. What is an explicit interface implementation?
4. When will we go for an explicit interface implementation?
5. What is the difference between value type and reference type?
6. Is string reference type or value type?
7. String is immutable. Why and how will you over come this issue?
8. How will you create a thread?
9. How will you start a thread?
10. What are the difference between delegates and events?
11. What is GAC?
12. How will you place an assembly in GAC?
13. How will you trigger GC to clean up memory?
14. C# - Managed Code; Who manages what?
15. Is it possible to create unmanaged code by C#?
16. What is the difference between C++ and C#?
17. How will you handle exceptions in C#?
18. Is it possible to have finally block without catch?
19. If catch throws an expection, will finally block gets execute?
20. What is the use of finally block?
21. What is cross thread exception?
22. What is attached property?
23. What are the advantages of WPF over windows forms?
24. What is dependency property?
25. What are the different types of bindings?
26. How will you change the shape of a button?
27. What is the difference between control template and styles?
28. Could you call a method by binding?
29. What is a value converter?
30. What is a multi value converter?
31. How will you update the UI if the data is refreshed by some other thread?
32. What is template binding?
33. What is the difference between logical tree and visual tree?
34. What are routed events?
35. What are the different types of layouts in WPF?
36. What is canvas?
37. What are the design patterns you know?
38. How will you find the dependencies in observer pattern?


Comments

Guest Author: 23 Jan 2013

1. Difference between abstract and interface:
abstract is a keyword used to declare an abstract class, method, or property; and interface is a keyword used to declare interface.
Abstract class is a semi implemented class and has to be only used as a base class. It may or may not have abstract members (methods). All abstract methods must be implemented in derived classes. You cannot instantiate an abstract class (cannot create objects).
Interface is a contract for other classes as all its members (methods) needs to be implemented in the derived classes. It is not a class. You cannot create an instance of the interface. A class can implement multiple interfaces. By defaul all members of an interface are abstract.
2.

Guest Author: 23 Jan 2013

2. You can only declare properties and events in an interface.
3. You have to explicitly implement an interface method in a derived class if this class is implementing more than one interface and if two interfaces have same name methods; then to differentiate one method has to be implemented explicitly.
Example:
interface IInterface1
{
void Display();
}
interface IInterface2
{
void Display();
}
public class MyClass:IInterface1,IInterface2
{
public void Display() //This will be considered for IInterface1 automatically
{
//Code goes here
}
void IInterface2.Display() //This will be considered for IInterface2 {
//Code goes here
}
}

Guest Author: 23 Jan 2013

5. Value types are stored in stack. Directly the data (value) stored in memory. You cannot instantiate them.
You cannot use new keyword to declare them. Examples: string, int, bool, etc.
Reference types are stored in heap memory. Only the address stored in memory. Classes are reference types.
You can use new keyword to create objects.
6. string is a value type.
7. String is immutable because it is a value type. You have to use StringBuilder class if you want to use multiple strings
and want to concatenate them. By using StringBuilder you can append to the same object, hence only one memory reference created.
10. By using delegates we can call methods directly as method can be passed as a parameter to a delegate.
Events are actions which get triggered on a user action like a Button click or Mouse click, etc. Events are actually handled
through Event hanlders. (Note: Eventhandler is a delegate).
11. GAC is Global Assembly Cache. If an assembly wants to be shared or used in different applications then it needs to be
registered in GAC. You can register through command prompt by GACUtil command.

Author: Shine S08 Feb 2013 Member Level: Gold   Points : 2

6. string is a reference type.not value type
7. string is immutable because, once a string is created it cannot be modified/overwritten in the same memory location.
if we modify the string by adding another string to it, the new string will be stored in a new memory in stack. old memory will later reclaimed by garbage collector when it runs.

Author: Pawan Awasthi10 Feb 2013 Member Level: Gold   Points : 10

Hai Saravanan,
I tried below the answers of the questions. Please if anyone is having good answers for any of the question, please reply to the post as it will be helpful for others as well as for myself.

1. What is the difference between abstract and interface?
A. Abstract Class is the collection of Abstract and Concrete members and the Interface is the collection of Abstract members only. Here Members means the Methods, Properties, events etc.

2. Is it possible to use properties and events in an interface?
A. Yes, In Interface we can write the abstract properties and abstract events. Abstract means in complete. We can't write the full definitions for any of the members in the interface.

3. What is an explicit interface implementation?
A. Explicit Interface is the interface where we need not to inherit from the interface but we can use the same interface inside the class members by providing its name before the method implementation.
e.g
interface Inf1
{
void Show();
}
interface Inf2
{
void Show();
}

Now If I want to implement both the interfaces, we can use them as:

public class DemoClass:Inf1,Inf2
{
public void Show()
{
Console.WriteLine("Interface 1 method");
}
public void Inf2.Show()
{
Console.WriteLine("Explicit Interface 2 method");
}


4. When will we go for an explicit interface implementation?
A. When we have same methods in more than one interfaces and we want all of them to be implemented in the class, then to differentiate, we need to implement them explicitly using the explicit interface.[as above]

5. What is the difference between value type and reference type?
A. value type hold data directly while the reference type holds the memory location and in that memory location, the actual data exists. value types stored in the stack memory while the reference types stored in the heap memory.

6. Is string reference type or value type?
A. String is the reference type as it holds the data in the heap memory.

7. String is immutable. Why and how will you over come this issue?
A. String objects is immutable because once the data is allocated to the memory location, the data can;t be changed. if we need to the data to be replaced with the new data, we need to create another memory location and paste the same data.

8. How will you create a thread?
A. Using the System.Threading namespace.
Here we can create a thread object and pass the method which will be used for the thread.
Create a class and method inside the class.
create a Thread object and pass the method.Start the thread using the ThreadStart delegate.

9. How will you start a thread?
A. Using the ThreadStart delegate.

10. What are the difference between delegates and events?
A. In actual scenario when we see the IL for both, it generates the same IL(Intermediate Language) or MSIL.
When we declare delegate, event keyword is used as the modifier. Delegate is used for the hiding the actual method and implementation for the security reasons. it support + and - operators so that we can add and subtract two delegates. What that delegates do, is defined by the action events.

11. What is GAC?
A. GAC(Global Assembly Cache). Its the centralized location to store the assemblies which are required by multiple application. When the assemblies are shared by more than one users or application, we need to place in the assembly folder. This assembly folder is called as GAC. All the assemblies in this
folder are having the Strong Name(SN) for the unique identification. The assemblies having the same name can be placed in eh GAC but they will have the separate version number.
We can create the strong name of an assembly by using the below command:
SN -k abc.dll
After creating strong name, we need to place that assembly in the GAC by using the below command:

GACUtil -i abc.dll


12. How will you place an assembly in GAC?
A. To place the assembly in GAC, the assembly must have the strong name and then only we can place it in the GAC. To place the assembly in the GAC, we can use the below command in the Visual Studio Command prompt:

GACUtil -i abc.dll

This command will install the assembly in the GAC.
To Uninstall, we can use the below command:

GACUtil -u abc.dll


13. How will you trigger GC to clean up memory?
A. We can force the Garbage collection to call explicitly by the below method:
GC.Collect();
This method will force the garbage collection to run and free up the memory allocated to the objects.

14. C# - Managed Code; Who manages what?
A. C# is the managed code because the code which is run through the CLR is called as managed code. CLR(common Language Runtime) is the execution engine which make sure that the code which runs through this execution engine doesn't have any memory leak or by pass the object and so called as managed code execution.

15. Is it possible to create unmanaged code by C#?
A. The code which doesn't run through CLR is called as Unmanaged code like VB 6.0 code or VC++ code which is when passed through the Visual Studio, the CLR skips that code and not executes it. For these type of code, we need to explicitly claim the memory management as it will not be taking care by the CLR. Also we need to make sure that the memory will not leak through some other process.

16. What is the difference between C++ and C#?
A. C++ is the object oriented technology which is not the type safe, need to handle the memory management by the programmer, use of pointer makes the C++ as unsafe because pointer uses the memory references.
C# is the pure object oriented, type safe language where the memory managed by CLR automatically. No use of pointer so its type safe. All the declaration for the variable need to be initialized else it will show the compile time error. It doesn't take the variable value automatically or default.

17. How will you handle exceptions in C#?
A. The exceptions occurred by the program can be handled by the Try..Catch blocks. For the code where the exceptions can occur, we can keep the logic in the try block and the to handle that error, we need to add the catch block to throw the exception and show in form of the message.

18. Is it possible to have finally block without catch?
A. Yes, finally block is possible without the Catch block. If we are sure that we won't get any exception in the try block, then we can skip the catch block and directly can write the finally block.
Finally block will always executed whether there is any exception occurred in the try or not. So the code lie to disposing the objects, closing the connection, making the value as null, we use the finally block as it should run always.

19. If catch throws an exception, will finally block gets execute?
A. Yes, finally block will always executed irrespective of catch block. If there is an exception, the control will move the the catch block and after the catch block completion, the finally block gets executed.

20. What is the use of finally block?
A. Finally block is used to free-up the memory, closing the objects, Closing the connections, disposing the objects etc.

21. What is cross thread exception?
A. When the exception occurred in a thread because of other thread execution, the exception is called as cross thread exception. In this case, either of the thread won't executed.

22. What is attached property?
A. These properties are the special dependency properties which doesn't have the wrapper. These properties allows the child elements for specifying the parent element values.

23. What are the advantages of WPF over windows forms?
A. There are many advantages of using the WPF like;-
1. Multimedia integration
2. 3-D support
3. We can create the animations, story etc using the WPF application

24. What is dependency property?
A. Dependency properties are special type of properties which are static properties but called as the instance properties.
Below is the syntax for creating and registering the dependency properties:

// Dependency Property
public static readonly DependencyProperty property=
DependencyProperty.Register( "propertyName", typeof(propertyType), typeof(parentControl),null);


25. What are the different types of bindings?
A. There are various types of bindings supported by the WCF. Bindings re used that how the service will be connected or get accessible by the clients.
1. BasicHttpBiding Binding
2. WSHttpBinding Binding
3. WSDualHttp Binding etc

26. How will you change the shape of a button?
A. We can use the control template to create the style of the button.


27. What is the difference between control template and styles?
A. Style is used to apply the styles to more than one controls.We can change the default appearance of the control by applying the property. For example if we want to change the Font Size and Font size and Font family to all the text boxes, we can create the style and then apply the style to the text-box controls.
ControlTemplate mainly used for the visual structure and visual behavior of a control. We can apply the appearance of a control by new ControlTemplate and then apply it to the controls.

28. Could you call a method by binding?
A. No, Binding is used for how to make the communication of service to the client. e.g HttpBinding, TCPBinding etc

29. What is a value converter?
A. Value converter is used to convert the one type to another types like Currency converter convert the string to currency format.

30. What is a multivalue converter?
A. Multi-Value convert is used to convert the values from one format to another.

31. How will you update the UI if the data is refreshed by some other thread?
A. We can use the Observer Pattern. Observer pattern is used to

32. What is template binding?
A. Template binding is used in template controls. It mainly used to link the property value in the control template to some other templated control property.

33. What is the difference between logical tree and visual tree?
A. Logical Tree is the logical representation of UI controls. It represents the hierarchy of UI controls and elements. The Logical Tree doesn't include inner controls.
Visual Tree is the Logical representation controls with its internal controls. So it also contains the internal controls additional to the Logical tree.

34. What are routed events?
A. These are the special types of events which navigate up or down in the visual tree as per the rounting strategy. Those could be bubbled up event, internal events, inner events etc. These events can invoke more than one other events of the visual tree.

35. What are the different types of layouts in WPF?
A. There are various layouts introduced in WPF:
a. Stack Panel
b. Grid Panel
c. Wrap panel
d. Doc panel
e. Canvas panel

36. What is canvas?
A. Canvas panel is one of the layout control which is used to place the child elements base on there coordinates. The coordinates are relative to the side of the panel. based on the Panel.Left, Panel.Right, panel.Top we can provide the position of the child controls.

37. What are the design patterns you know?
A. Design patterns are the easy way to reduce the problems of the design in the later stages of the software development. There are mainly 3 types of design patterns defined by Go4;
1. Creational- Basically defined the structure of object creation. How the object should be created lie Factory pattern, Abstract Factory patterns, Singleton Design pattern etc
2. Structural- Mainly used for how the object should be structured. e.g. Adapter, Decorator, Facade, Proxy etc
3. Behavioral- How the object behaved in the different scenarios is the main point for the behavioral design patterns. e.g.Visitor, State, Strategy etc

38. How will you find the dependencies in observer pattern?
A. By using the state change, the observer pattern finds the dependency of the objects. Observer pattern basically maintain the list of objects which are interrelated and all the objects implements the common interface. By using the INotifyPropertyChange, we can implement the observer and the observer finds the state has been changed for the particular object and accordingly the UI gets changes.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: