Saturday, 15 March 2014

Interview Questions And Answers Of Assemblies and Threading in C#

Assemblies

1. Define Assembly in .NET.

An assembly is the primary unit of a .NET application.
It includes an assembly manifest.
Manifest describes the assembly.

2. What is a shared assembly?

An assembly that can be shared by multiple applications. To make an assembly a shared assembly, first assign it a strong name, and install it to the global assembly cache.

3. What is Assembly manifest?

The manifest of an assembly contains assembly's data like version, scope, security information (strong name),etc.
It also contains a reference to the resource and classes.
It is stored in either an .exe or a .dll with Microsoft intermediate language (MSIL) code.

4. What is GAC (global assembly cache)?

GAC, global assembly cache is an area of memory reserved to store the assemblies of all .NET applications that are running on a certain machine.

It shares assemblies among multiple .NET applications. The assemblies must have a strong name and must be publicly shared to be installed in the GAC.

5. How is the DLL Hell problem solved in .NET?

DLL Hell problem is solved in .NET with the introduction of Assembly versioning. It allows the application to specify not only the library it needs to run, but also the version of the assembly.

 6.  What are Satellite assemblies ?

To support the feature of multiple languages, we need to create different modules that are customized on the basis of localization. These assemblies created on the basis of different modules are knows as satellite assemblies.

7. What does an assembly contain?

An assembly contains following information:

Assembly manifest: Information about the assembly.
Type metadata: Information about the types.
IL Code
Resource files.

8. Define private assembly and a shared assembly.

A private assembly is stored in the application’s directory and used by a single application.
A share assembly can be used by multiple applications and is stored in the Global assembly cache, a repository of assemblies maintained by the .Net Framework.

9. What do you understand by side-by-site execution of assembly?

This means multiple version of same assembly to run on the same computer. This feature enables to deploy multiple versions of the component

10. Define Strong Name. How do you apply a strong name to assembly?

A strong name means generating public key in order to provide unique name to the assembly.

The key pair that defines the strong name is created using the Strong Name utility, Sn.exe.

To sign an assembly with a strong name

Create Key pair using the Strong Name utility, Sn.exe.

11. Define Global Assembly Cache.

Global Assembly Cache is the place holder for shared assembly. If an assembly is installed to the Global Assembly Cache, the assembly can be accessed by multiple applications. In order to install an assembly to the GAC, the assembly must have to be signed with strong name.

12. Explain the purpose of CultureInfo class. What namespace contains it?

System.Globalization namespace contains CultureInfo class. This class provides information about a specific culture, i.e. datetime format, currency, language etc.

13. What are the ways to deploy an assembly?

MSI installer
CAB archive
XCOPY command

14. How to view an assembly?

We can use the tool “ildasm.exe” known as “Assembly Disassembler” to view the assembly.

15. which tool is used to deploy an assembly ,so as the assembly is available to all the application?

The GacUtil.exe is the tool, which allows us to add any assembly to the windows GAC.

16. Difference between dll and exe?

dll
exe
Dll assemblies are out process components which are not capable of running on their own. They can only support others to execute.
Exe assemblies are in process components which are capable ofrunning on their own as well as provide support for others to execute.
Class and control library will generate an dll assembly when compiled
Windowsforms ,wcf,console application and windows services generate an exe assembly when compiled

  1. what is obsfucator?
Obfuscator is a tool to protect .Net assemblies and exe files. The tool renames all possible symbols, types, interfaces, namespaces etc into meaningless values and removes all unnecessary information. This reduces the size of the assemblies and also helps in protecting the code. It's normally used to protect the assemblies from exposing information for reverse engineering .

                                                            Threading

1.    How can we make a thread sleep for infinite period ?

You can also place a thread into the sleep state for an indeterminate amount of time by calling Thread.Sleep (System.Threading.Timeout.Infinite).To interrupt this sleep you can call the Thread.Interrupt method.

2.    What is Suspend and Resume in Threading ?

Suspend will pause the execution of current thread.
Resume method will resume the execution of a suspended thread.

3.    Tell Three Differences Between Thread and Process?

a) Processes run in parallel on a computer, threads run in parallel within a single process.
b) Processes are fully isolated from each other; threads have just a limited degree of isolation.
c) Threads share (heap) memory with other threads running in the same application.

4.    What are the uses of Thread? Tell any five.

      a) Maintaining a responsive user interface
      b) Making efficient use of an otherwise blocked CPU
      c) Parallel programming
      d) Speculative execution
      e) Allowing requests to be processed simultaneously.

5.    Is Multi-Threading always advantagious? If no tell atleast three drawbacks.

     No, Mutltithreading has its own drawbacks also.

a) It can slow your application down if used excessively or inappropriately.
b) There’s also a creation/tear-down cost.
c) Can cause long development cycles and an ongoing susceptibility to intermittent and nonreproducible bugs.

6.    How can we pass data to a thread?

The easiest way to pass arguments to a thread’s target method is to execute a lambda expression that calls the method with the desired arguments.

(Thread t = new Thread ( () => Print ("Hello from t!") );) 

Another technique is to pass an argument into Thread’s Start method.

7.    What happens to background thread if all the forground thread execution finishes?

Background threads still running are abruptly terminated.

8.    What happens to threads if uses the Task Manager to forcibly end a .NET process?

All threads “drop dead” as though they were background threads and it could vary depending on the CLR and operating system version.

9.    What is the default upper limit of threads in a threadpool in .Net framework 4.0? How can we change it?

>> 1023 in Framework 4.0 in a 32-bit environment
>> 32768 in Framework 4.0 in a 64-bit environment
We can change the upper limit by calling ThreadPool.SetMaxThreads method. (public static bool SetMaxThreads(int workerThreads, int completionPortThreads)

10.  Garbage collector thread belongs to which priority?
     
low-priority

11.  What is meant by time slicing or time sharing?
     
Time slicing is the method of allocating CPU time to individual threads in a priority schedule.

12.  How can we know a state of a thread ?
Thread State" property can be used to get detail of a thread.

The following Thread State's exists in .net:

Running
Suspend Requested
Background
Unstarted
Wait Sleep Join
Suspended
Abort Requested
Aborted.

13.  How can you avoid deadlock in threading in c#.Net ?

A good and careful planning can avoid deadlocks.There so many ways microsoft has provided by which you can reduce deadlocks example Monitor ,Interlocked classes , Wait handles, Event raising from one thread to other thread ThreadState property which you can poll and act accordingly etc.

14.  What are Daemon thread's and how can a thread be created as Daemon?

Daemon thread's run in background and stop automatically when nothing is running program.Example of a Daemon thread is "Garbage collector".Garbage collector runs until some .NET code is running or else its idle. You can make a thread Daemon by Thread.Isbackground=true.

15. How can you reference current thread of the method ?

"Thread.CurrentThread" refers to the current thread running in the method."CurrentThread" is a public static property.

17.  What happens when a thread terminates while owning a mutex?

The state of mutex is set to signal and the next waiting thread gets ownership

18.  How many types of mutex are there? Tell any one difference.

Two types of Mutex. Those are named & unnamed.
Difference is unnamed is visible within the process only where as named are visible through out the OS.

19.  Which namespace contains threading - related classes ?

Systems.Threading has all the classes related to implement threading. Any .NET application who wants to implement threading has to import this namespace. .NET program always has at least two threads running one is the main program and second is the garbage collector.

20.  How can we change priority and what the levels of priority are provided by .NET ?

Thread Priority can be changed by using Threadname.Priority = ThreadPriority.Highest. In the sample provided look out for code where the second thread is ran with a high priority.

Following are different levels of Priority provided by .NET :
1.    ThreadPriority.Highest
2.    ThreadPriority.AboveNormal
3.    ThreadPriority.Normal
4.    ThreadPriority.BelowNormal
5.    ThreadPriority.Lowest

21.  What does AddressOf operator do in background?

The AddressOf operator creates a delegate object to the BackgroundProcess method. A delegate within VB.NET is a type-safe, object-oriented function pointer. After the thread has been instantiated, you begin the execution of the code by calling the Start() method of the thread.

22.  What does Thread.Sleep() do in threading?
Thread’s execution can be paused by calling the Thread.Sleep method. This method takes an integer value in milliseconds that determines how long the thread should sleep. Example: Thread.CurrentThread.Sleep(2000).

23.  How can we make a thread sleep for infinite period?
You can also place a thread into the sleep state for an indeterminate amount of time by calling Thread.Sleep (System.Threading.Timeout.Infinite). To interrupt this sleep you can call the Thread.Interrupt() method.

24.  How to stop a long running thread ?
Thread.Abort() stops the thread execution at that moment itself.

25.  How to debug threads ?
"Debug\Windows\Treads" in Visual Studio gives an access to the threads debug window. This window is only seen when the program is running in debug mode.

26.  What is Thread.Join() in threading?

There are two versions of Thread.Join():

1.  Thread.Join().
2.  Thread.Join(Integer) this returns a Boolean value.

The Thread.Join() method is useful for determining if a thread has completed before starting another task. The Join() method waits a specified amount of time for a thread to end. If the thread ends before the time-out, Join() returns true; otherwise it returns False. Once you call Join(), the calling procedure stops and waits for the thread to signal that it is done.

Example you have "Thread1" and "Thread2" and while executing "Thread1" you call "Thread2.Join()".So "Thread1" will wait until "Thread2" has completed its execution and the again invoke "Thread1".

Thread.Join(Integer) ensures that threads do not wait for a long time. If it exceeds a specific time which is provided in integer the waiting thread will start.

27.  When working with shared data in threading how do you implement synchronization ?

There are certain situtations that you need to be careful with when using threads. If two threads (e.g. the main and any worker threads) try to access the same variable at the same time, you’ll have a problem. This can be very difficult to debug because they may not always do it at exactly the same time. To avoid the problem, you can lock a variable before accessing it. However, if the two threads lock the same variable at the same time, you’ll have a deadlock problem

SyncLock x
'Do something with x
End SyncLock

28.  Can we use events with threading?

Yes, we can use events with thread; this is one of the techniques to synchronize one thread with other.

29.  How can we know a state of a thread?

"ThreadState" property can be used to get detail of a thread. Thread can have one or a combination of Status.System.Threading. Threadstate enumeration has all the values to detect a state of thread. Some sample states are IsRunning, IsAlive, Suspended etc.

30.  What is a Monitor object?
                                            
Monitor objects are used to ensure that a block of code runs without being interrupted by code running on other threads. In other words, code in other threads cannot run until code in the synchronized code block has finished.

SyncLock and End SyncLock statements are provided in order to simplify access to monitor object.
run in a process context. 













No comments:

Post a Comment