Sunday, November 28, 2010

.NET Fundamentals 13(AppDomain)

Que: What is AppDomain?
Ans: AppDomain is logical container for set of assemblies.It is kind of shell to make appliction secure. It is different from a process. A process can have multiple AppDomains. It is kind of isolation of memory used by application. As the memory in appdomain is managed by CLR, so it is responsibility of CLR to make sure that one appdomain doesn't access the memory of other appdomain.

Que: What is difference between AppDomain and a process?
Ans: A process is OS level concept whereas AppDomain is .NET level concept. The purpose of both is to provide security but at different level. An AppDomain can belong to only one process, but a process can have more than one AppDomain in it.

Que: What is the advantage of having AppDomain?
Ans: We are having concept of process to provide isolation of memory, but still .NET have given AppDomains, because creating a process is always costly. AppDomain creation is .NET level so creating an AppDomain is not so costly. Moreover unloading an AppDomain causes unloading all the assemblies loaded inside that AppDomain.and unloading one appdomain never affects another appdomain.

Que. If AppDomain is isolating the memory usage, than is it possible to access object in one AppDomain in another AppDomain?
Ans:It is possible that code in one appdomain can communicate with types and objects in another appdomain.
Object by reference:
If an object have to get shared by reference, that class has to inherit from "MarshalByRefObject" class.
Actually when an object is  passed as reference from one appdomain to another, then CLR create a proxy object at destination. This proxy looks exactly same as type of object we want pass, but it has information about how to access that object in another appdomain(where original object got created).So the actually one appdomain still not accessing the memory of another appdomain. but CLR manages this by itself. Now when we try to use that object(by executing some method in it) through proxy, actually Appdomain transition occures and we reaches to original object(and executes the method).
Object by Value:
if we want to pass an object by value from on appdomain to another we need to add [Serializable] attribute to that class. So ih this case all field values of original object gets copied to destination appdomain. So if we unload the appdomain still the object exist in destination appdomain. Actually CLR creates exact replica of original object in destination.

Que: How to unload an appdomain?
Ans:Appdomain has a static unload method which will unload the appdomain.All threads running in appdomain and also those thread which can return at some point , get unloaded. CLR forces all the threads involve with unloading appdomain to throw ThreadAbortException. During shutdown no new threads are allowed to enter the application domain and all application domain specific data structures are freed. You cannot unload an application domain in a finalizer or destructor. If the application domain has run code from a domain-neutral assembly, the domains copy of the statics and related CLR data structures are freed, but the code for the domain-neutral assembly remains until the process is shutdown. There is no mechanism to fully unload a domain-neutral assembly other than shutting down the process.

 Note:
Application domain is not a secure boundary when the application runs with full trust. Applications running with full trust can execute native code and circumvent all security checks by the .NET runtime. ASP.NET applications run with full trust by default.

Saturday, November 20, 2010

Java Part-1

Que1: What is difference between Process and Thread?
Ans: Process and threads are methods to get parallel execution of an application. A process have its own address space, where multiple thread can execute, It means a process can have multiple threads in it. Multiple process can interact with each others only by Inter process communication mechanism thus Inter process communication is always expensive but inter thread communication is cheap. As two process uses separate memory spaces thus if one process dies it never affects other process, but if one thread gets corrupt then other thread(and sometimes corresponding process) will get die.

Que2: What are abstract classes?
Ans: Class that contains one or more methods which are only declared not defined.In other words Abstract class is class which have one or more abstract methods(Method without body). We can not have any object of abstract class.But it can be sub classed, and that subclass have to implement all abstract methods else this subclass too have to declare as abstract class. Abstract class can be used to refer an object reference, so that at run time we can decide which method of subclass to execute.

Que3: What is an interface?
Ans: Interface is an abstract class with all abstract methods. Interface is generally defined to show a contract that any class implementing this interface will implement methods in it.
Note: Syntax for both abstract class and interface is different. The definition given it only for understanding.

Que4: What is the difference abstract class and interface?
Ans: In Abstract class contains one or many abstract methods but an interface have only abstract methods. A class can be a subclass of only one abstract class, but a class can implement one or many interfaces. Interface is a way to achieve multiple inheritance.

Que5: What is overriding?
Ans: When method name and its  parameters (signature) are same in sub and super class but its definition is different. it is called as overriding.

Que6: What is overloading?
Ans: Method with same name and different signature in same class, is called method overloading.

Que7: What are different ways to block a thread in Java?
Ans: There are 3 ways:
    Sleep(): thread will be blocked for specific amount of time.
    Suspend(): thread need to use resume() to return back.
    wait(): thread need to use notify() to return back.

Que8: What is JAR file?
Ans: JAR file is a kind of zip file for Java application containing application or libraries in the form of classes and associated meta data and resources.

Que9: What is the difference between "==" and .equals() method?
Ans: "==" compares object reference only but .equals compares its fields. Generally to compare any 2 objects use .equals method not "==".

Que10: What is the difference between constructor and methods?
Ans: Constructor has same name as class name, no other method can have name as class name. Constructor has no return type, not even void. constructor may or may not be explicitly defined for any class, Java does provide a default constructor.
For example,

public class abc{
int i;

public static void main(String args[])
{
abc h1 = new abc();
System.out.println("this works");
}
}

This works.

But if we gave abc h1=new abc(5); then it gives error that constructor not specified.