C# Important Interview Questions, Concepts and Answers

9/14/2009 No Comment
Overview:C#.NET Interview Questions,Concepts and FAQs,C# Object Oriented Programming Concepts,Frequently Asked Questions regarding C#.NET

What are Namespaces?
A) Naming convention used in .Net
B) Group of classes categorized to avoid name clash
C) None of the above

C. None of the above A Namespace in .Net is like containers of objects. They may contain unions, classes, structures, interfaces, enumerators and delegates. Main goal of using namespace in .Net is for creating a hierarchical organization of program. In this case a developer does not need to worry about the naming conflicts of classes, functions, variables etc., inside a project.

What are Indexers? What is the use of it, and when to use it
An indexer is a member that enables an object to be indexed in the same way as an array. There are times when it is desirable to access a collection within a class as though the class itself were an array. For example, suppose you create a list box control named myListBox that contains alist of strings stored in a one-dimensional array, a private member variable named myStrings. Alist box control contains member properties and methods in addition to its array of strings.However, it would be convenient to be able to access the list box array with an index, just as if thelist box were an array. This can be achieved using the Indexer

What is difference between dispose() & finalize()
If you want to delete resources(objects) those are not using ,you should not worry about that, garbage collecter implicitly call finalize() method and remove all such object but if you want to delete object forcefully.The larger object ,you want to delete after completeing task),than you can explicitly call dispose() method.
Design Pattern : If your classes use unmanaged resources, you need to implement both Dispose & Finalize. Dispose() is called by user code, that is, the code that is using your class.
Finalize/Destructor cannot be called by User code, it's called by Garbage Collector
Finalize : Is a destructor, called by Garbage Collector when the object goes out of scope. Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens.
Dispose : Same purpose as finalize, to free unmanaged resources. However, implement this when you are writing a custom class, that will be used by other users. Overriding Dispose() provides a way for the user code to free the unmanaged objects in your custom class.

As an aside, here's how the GC works:

The garbage collector keeps track of objects that have Finalize methods, using an internal structure called the finalization queue. Each time your application creates an object that has a Finalize method, the garbage collector places an entry in the finalization queue that points to that object. The finalization queue contains entries for all the objects in the managed heap that need to have their finalization code called before the garbage collector can reclaim their memory.
Implementing Finalize methods or destructors can have a negative impact on performance and you should avoid using them unnecessarily. Reclaiming the memory used by objects with Finalize methods requires at least two garbage collections. When the garbage collector performs a collection, it reclaims the memory for inaccessible objects without finalizers. At this time, it cannot collect the inaccessible objects that do have finalizers. Instead, it removes the entries for these objects from the finalization queue and places them in a list of objects marked as ready for finalization. Entries in this list point to the objects in the managed heap that are ready to have their finalization code called. The garbage collector calls the Finalize methods for the objects in this list and then removes the entries from the list. A future garbage collection will determine that the finalized objects are truly garbage because they are no longer pointed to by entries in the list of objects marked as ready for finalization. In this future garbage collection, the objects' memory is actually reclaimed.

Dispose() is called by the user of an object to indicate that he is finished with it, enabling that object to release any unmanaged resources it holds. Finalize() is called by the run-time to allow an object which has not had Dispose() called on it to do the same. However
Dispose() operates determinalistically, whereas there is no guarantee that Finalize() will be called immediately when an object goes out of scope - or indeed at all, if the program ends before that object is GCed - and as such Dispose() is generally preferred.

In which scenerio we use interfaces, abstract and concrete class? Which one is better and appropriate? Differentiate these three terms in depth.

The decision tree for this is pretty detailed. :) In most cases, using an abstract class is the "right" thing to do if you're trying to create a common class from which others will derive and which isn't fully specified. Interfaces are nice if you don't want to force classes to have a single root class in their hierarchy, as a single class can implement multiple interfaces. Concrete classes should be used if it's fully specified--i.e. subclasses don't have any hooks into which they must provide functionality.
Interface - used to define a skeleton. Classes who want to confirm to that skeleton implementsts the interface
Abstract class - Use abstract class when you want to provide some functionality to the user and at the same time would like to enforce some skeleton (structure) for the users of your class
Concrete class - Use a concrete class to represent an object (data & methods) which can be extended or reused.
Use following guidelines for each of the abstractions:
-- If your child classes should all implement a certain group of methods/functionalities, but each of the child classes is free to provide its own implementation, then use interfaces.

For e.g., if you are implementing a class hierarchy for vehicles, implement an interface called "Vehicle", which has properties like Colour, MaxSpeed etc., and methods like Drive(). All child classes like "Car", "Scooter", "AirPlane", "SolarCar" etc. should derive from this base interface, but provide a seperate implementation of the methods and properties exposed by Vehicle.

-- If you want your child classes to implement multiple, unrelated functionalities, in short multiple inheritance, use interfaces.
For e.g., if you are implementing a class called "SpaceShip", that has to have functionalities from a "Vehicle",
as well as that from a "UFO", then make both "Vehicle" and "UFO" as interfaces, and then create a class "SpaceShip" that implements both "Vehicle" and "UFO".

Abstract Classes

-- When you have a requirement where your base class should provide default implementation of certain methods, whereas other methods should be open to being overridden by child classes, use abstract classes.
For e.g., again take the example of the "Vehicle" class above. If we want all classes deriving from "Vehicle" to implement the "Drive()" method in a fixed way, whereas the other methods can be overridden by child classes. In such a scenario, we implement the "Vehicle" class as an abstract class with an implementation of "Drive", while leave the other methods / properties as abstract so they could be overridden by child classes.
-- The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.

What is the difference between Abstract and Interface? Answer
In an interface class, all methods are abstract - there is no implementation.
In an abstract class some methods can be concrete - there can be implementation.
In an interface class, no accessibility modifiers are allowed - are public by default.
In an abstract class accessibility modifiers are allowed.

Abstract Class:
1. Abstract Class Can contain Abstract Methods and Non-Abstract Methods.
2. When a Non-Abstract Class is Inherited from an Abstract Class, the Non-Abstract Class should provide all the implementations for the inherited Abstract Method.

1. Interface is nothing but Pure Abstract Class ie Interface can contain only the function declaration.
2. All the members of the interface are Public by Default and you cannot provide any access modifiers.
3. When a class is inherited from the interface, the inherited class should provide actual implementations for the inherited members.

What is the main difference between pointer and delegate with examples?
Delegates in c# are very similar to function pointers in c++ the only difference is that delegates are type safe due to these are CLR targated under .net framework, while function pointers in c++ are not type safe.
A Delegate in c# allows to pass a method of class to object of other class.
Rules in Deligation:-
1. In order to create a Delegate a Signature(parameter) must match Signature of object.
2. Define all the methods which has the same Signature as Deligate define.
3. Create the deligate object & pass the method as a parameter to Deligate.
4. Now call the encapsulated method using deligated object.
The best example of Deligation model is ADO.NET Architecture & Event Handling in windows environment.
Simply say Delegate is a strongly typed function pointer and pointer holds reference to a variable or pointer is a variable which holds the address of another variable.

Delegate to function--- is same as---- pointer to object. Delegates are function pointers. They are used when the function which needs to be called is not know at compile time. Pointers, on the other hand, are used to point to variables or object references in unsafe code.

What is managed code?Skill/Topic: Intermediate
A) Code managed ouside the IL
B) Code which can't be managed by the IL
C) Code written in VB.NET
D) Code to be compiled by IL

D) is the correct choice. Managed code is the code written in one of 20 high level programming languages available for use with .Net framework which is compiled into IL during the first level of compilation.

Ans : (A) The managed code are the code which are managed by the CLR not by the executalble code itself.(Ref.- MCSD Training Kit Developing Web Applicaion of PHI)
A is correct ans,

Ex: vbc compiler is compile to vb.net application in to MSIL(IL) code
csc compiler is compile to c#.net application code in to MSIL code
DotNet support multiple languages , each compiler will convert MSIL(IL) Code only. after CLR execute the code in memory before taking code in CLR, JIt will compile machine understandble code.

Related Posts

No comments :


Aired | The content is copyrighted and may not be reproduced on other websites. | Copyright © 2009-2016 | All Rights Reserved 2016

Contact Us | About Us | Privacy Policy and Disclaimer