Tuesday, October 29, 2013

C#.net Interview Questions

1. What is a Framework?
A framework is software which masks the functionality of the operating system and makes the code to execute under its control. Which provides basic features like?
·       Platform Independency
·       Security
·       Memory Management.
2. What are the features of C#2.0?
·       Partial classes which allows class implementation a cross more than one source file. This permits splitting up very large classes.
·       Generics or Parameterized types.
·       Static classes that can’t be instantiated
·       Null able value types which provides improved interaction with SQL Databases.
3. Features of C# 3.0
·       LINQ(Language Integrated Query)
·       Object initializes and Collection Initializes
·       Anonymous Types
·       Implicitly typed variables(Using Var Keyword)
·       Lambda expressions
·       Automatic Properties
·       Extension Methods
·       Partial Methods
4. Implicitly Typed Variables
This is a new feature which allows declaring a variable using the keyword “VAR” .The data type of variable is decided according to the value assigned to the variable.
Example:
Var x=100 integer
Var s=”hello” String
5. Boxing and UN Boxing
When a value type is stored as reference type in the heap memory we called it as a boxing
E.g. Object OBJ=100;
If the value type in the heap is converted and send back to stack we called as UN boxing
E.g. int x=convert.toInt32 (OBJ);
6. Object Oriented Programming
A language to be called as object oriented needs to satisfy a set of basic principles like
·       Encapsulation
·       Abstraction
·       Inheritance
·       Polymorphism
Encapsulation:
According to this the code in an object oriented language as to be warped in a container called as class which provides security to the class.
Abstraction:
It is an approach which talks about hiding the complexity by providing a set of interfaces to consume the functionality.
Inheritance:
According to this member of the class can be accessed by another class if they have parent child relationship.
Polymorphism:
Entities behaving in different ways depending upon the input they received are known as polymorphism
7. What is a constructor?
·       A constructor is a special method available under every class responsible for initializing the variables of a class
·       The name of the constructor is same as class name
·       A constructor method never returns a value
·       Without a constructor a class never executes.
8. Class Members
Whatever we define under a class were known as class members like variables, methods, constructors
9. Instance variables vs. static variables
A variable declared using static modifier or declared under static block were known as static variable rest of all “Instance variable”
Instance variable initialized whenever the object of the class get created. So the minimum or the maximum no of times it gets initialized will be Zero or N.
Where as a static variable gets initialized once the execution of the class starts. So the minimum and maximum no of times it gets initialized is only one.
10. Instance constructor and static constructor
·       A constructor declared using static modifier is a static constructor, rest all instance constructors.
·       Static constructor responsible for initialize the static variable and instance constructor is initializing instance variable.
·       A static constructor gets called one and only once in the execution of a class i.e. when the class execution starts.
·       Whereas instance constructor gets called zero (or) ‘N’ times i.e. each time the object of the class gets created.
·       As we can pass parameters to an instance constructor it cannot be done in the case of static constructor because a static constructor never takes parameters or cannot be parameterized.
11. Extension Methods
It was a new concept added in c#3.0 specification that allows you to extends the functionalities of class i.e. adding methods under the class even if you don’t have source code or you don’t have the permission to edit a class
The advantage of this approach is that after adding the method to a class you can call the methods using object of the class.
·       Extension methods can be define only under static class
·       Because they were defined under the static classes they will be defined as static methods but while consuming should be access as instance methods only
·       The first parameter of an extension method is the linker parameters which specifies to which class the method belongs prefixed with ‘this’ keyword. While calling the parameter will not be consider.
·       If you want any parameters to extension method while defining they should start from 2nd parameter.
Syntax:
Public static void test (this original obj, int x){}
12. Overloading
This allows defining of multiple methods or constructors with the same name by changing their parameters’ or signatures.
Overloading is an approach which allows providing multiple behaviors to a method
Example: Write and write line methods of the console class which were provided with different overloads to print different type of values.
Change in signature can be any of the following
·       Change the number of parameters being passed to a method
·       Type of parameters being passed to a method
·       Change the order of the parameter passed to a method
Example:
Class loadDemo
{
          Public void show ()
            {
                   Console.writeline(1);
            }
       Public void show (int a,int b)
          {
                   Console.writeline(a+b);
          }
}
13. Method Overriding
If a method of parent class is redefined in the child class with the same signature, we called it as “Method Overriding”.
Example:
Class parent
{
                Public void test()
                {
                                Console.writeline(1);
                }
                Public virtual void test(int x,int y)
                {
                                Console.writeline(x+y);
                }
}
Class child:parent
{
                Public void method1()
                {//some method
         }
                Public override void test(int x,int y)
{
                 //do something
}
}

14. Difference between Overloading and Overriding.
Overloading                                                  Overriding
1. This allows defining multiple    1. This allows defining multiple
Methods with the same name     methods with the same name
by changing their signature.          And same signature
2. This can be performed with   2. This can be performed with in
In the class or child class                 the child class only.
3. When performed under the   3.To perform this you require
Child class doesn’t require any     on explicit permission from
permission from the parent.          the parent.
15. How does the parent class give the permission to the child class to override or re-define in the method?
The parent class gives the permission to the child class to re-define the method by declaring the method as “virtual” now the child class can override the method, on optional basis using the override modifier.
Example:
Public virtual void test (int x) {}//parent class method
Public override void test (int x) {}//child class override method
16. After overriding a method under a child class can we invoke the virtual method of parent from the child class.
Yes. This can be done in two ways
1.   Create the object of the parent class under the child class to invoke the virtual method.
2.   Using the base keyword also you can invoke the virtual method of parent from child after overloading.
17. Can we rewrite a method under child class without permission from parent?
Yes, we can re-write a method under child without the permission from parent also (i.e. not declared as virtual) but we called this a “Method –Hiding”.
In case of overriding we use the override modifier to re-write the method
Where as in case of method-hiding we use New modifier for rewriting the method.
18. How to restrict a class not to be inherited by any other class.
If we want to restrict a class not to be inherited by any other class it should be declare using sealed modifier.
19. Abstract methods and Abstract Classes.
A method without any body is known as an abstract method what it has only the signature of the method. The class under which you define these abstract methods is called as abstract class. Abstract class can contain non abstract methods also.
Example:
Abstract Class math
{
                Public void test ()
                {//do something
                }
                Public abstract void add (int a,int b);
}
20. Interfaces
In object oriented programming we can also declare abstract members under another container called as interface apart from abstract class. But these interfaces can contain only abstract members in it.
As we know that multiple inheritances is not supported through classes, but it was still supported with “interfaces” i.e. a class can have only one immediate base class to it but can have multiple immediate interfaces are parent.
21. Partial classes
It was an approach of defining a class in more than one file i.e. u can split in class into multiple files. Partial classes provide the following benefits.
·       Splitting  up huge volume of code into multiple files which makes easy to manage
·       Multiple programmers can work on the same class at a time.


Example:
Class parts                              partial class parts                                                   partial class parts
{                                                     {                                                                                      {
Method1 ()                                    Method1 ()                                                                              Method3 ()
Method2 ()                                   Method2 ()                                                                              Method4 ()
Method3 ()                              }                                                                                          }
Method4 ()
}
22. How to restrict a class not to be accessible to any other class.
This can be done by declaring the constructor of the class as private.
23. How to restrict a class not to be inherited to any other class?
This can be done by declaring class as sealed.
24. How to restrict a class not to be accessible for any other class to consume it by creating its object.
This can be done by declaring constructor of the class as protected.
24 Members of the class
As we were aware a class is a collection of members and these members can be of various types like
·       Fields(variables)
·       Methods
·       Constructors
·       Properties
·       Delegates
·       Events
·       Enumerators
Properties: when you want to access to values of a class outside the class this can be done in two ways.
1.   Public variables
2.   Properties
In case of variables if access is provided the user has a chance of getting as well as setting the values the property can be restricted in three ways
1.   Read write Property: Which provides Get and Set Access
2.   Read Only Property: Which provides only Get Access
3.   Write Only Property: Which provides only Set Access
Enumerations:
It is a type with a set of predefined values you can use these values while defining enumerated properties
Enumerated property in the sense a property which you can restrict the values being assigned with the values declared under enumeration.
25. Delegates
These were pointers to methods whenever we call a method to execute a method it creates a “stack” and destroys. Each time the method is called a stack is created and destroyed.
If you want to call a method for multiple times without multiple stacks getting created and destroyed make use of a “Delegate”.
Syntax:        
<Modifiers> delegate<type/void><name> [<parameters>]
Delegates are of two types
1.   Single cast Delegates
2.   Multi cast Delegates
If a delegate is used for calling a single method is called as single cast delegate
If a delegate is user for calling more than one method is known as “Multi cast delegate”.
Note: If we want to call more than one method with delegate the Input/output parameters of all the methods should be same.
Example:
Class math
{
                                       Public void add (int x,int y)
                                       {
                                                Console.writeline(“add”,(x+y));
                                       }
                                       Public void sub (int x,int y)
                                       {
                                                Console.writeline(“sub”,(x-y));
                                       }
                                       Public void mul(int x,int y)
                                       {
                                                Console.writeline(“mul”,(x*Y));
                                       }
                                       Public delegate void multidel(int x,int y);
                                       Static void main ()
                                       {
                                       Math obj= new math ();
                                       Multidel md= new Multidel(obj.add);
                                       Md+=obj.sub;
                                       Md+=obj.mul;
                                       Md+=obj.mul;
                                       Md(150,25);
                                       Console.writeline();
                                       }
}
26. Why it is must for a child class to provide the implementation for all abstract methods of its parents?
We can never create the object of a class which contains any abstract method in it. An abstract class is never useful to itself it can be consumed only by the child class that to after providing the implementation for abstract methods of the parent.
27. How were exceptions implemented in our Language?
Every exception was implemented in the form of a class under the system namespace mostly the parent class for all the exception is the class exception.
28. How does an exception come into picture?
In the process of execution whenever the CLR recognized a problem in the code will invoke the exception manager which recognizes the type of exception and creates the object of related exception class and through it.
The object thrown by the exception manager will abnormally termination the program and displays related error-message.
29. How can we handle an exception?
We can handle an exception using blocks of code known as “try-catch blocks”.
Finally Block: This was another block of code which can be paired with try. The statements we write under this block will execute at any code i.e. when an exception occurred or not.
30. Collections
·       These were objects similar to arrays but can hold a set of dissimilar type values
·       Arrays are fixed length where as collections are variable length which can increase the size when new values being adding to the collection
·       Under .net we were provided with no of collection classes like stack, queue, linked list, sorted list, array list etc which were available under the System. Collection name space
31. Array List
·       These were same as array, which can hold dissimilar type values and also dynamic in size.
·       While creating the object of any collection we can use its default constructors of a parameterized constructor using which you can specify the initial capacity.
·       When the default constructor was used the initial capacity will be zero by default which becomes Four the first value gets added from then it keeps on doubling the capacity whenever required
32. What is the difference between array and array list?
             Array                                                   Arraylist
1. It is similar type values             1. It is a dissimilar type values
2. It is fixed length i.e. fixed size 2.It is variable length i.e.
                                                             Dynamic in size.
3. It is under system namespace 3.It is under collection     
                                                              Namespace  
Example: on stack
Class program
{                                      Static void main (string [] args)
                                       {
                                       Stack s= new Stack ();
                                       s. push(10);s. push(“hello”);s. push(12.35);
                                       foreach(object obj in s)
                                       console.writeline(obj+””);
                                       console.writeline(s.pop());
                                       foreach(object  obj in s)
                                       console.writeline(obj+””);
                                       console.read();
                                       }
}
Example: Array List
Using System. Collections;
Namespace call project
{
                                       Class class1
                                       {
                                                Static void main (string [] args)
                                                {
                                                                Array List list= new Array List ();
                                                                Console. writeline(list.capacity);
                                                                List.add(10);
                                                                Console.writeline(list.capacity);
                                                                List. Add(45.6); List.add(20);
                                                                List.add(false);
                                                                List .add(“hello”);
                                                                Foreach(object obj in list)
                                                                Console.write   (obj+” ”);
                                                                Console.read();
                                                }
                                       }
}               
33. Generic collection. These were newly added in the, net 2.0 specifications which were similar to collection in size or capacity but can hold only similar type of values like an Array.
Generic collection classes are available under system. Collections generic namespace while creating object of a generic collection we can specify the type of value it can hold
Syntax: stack<type t>
Example:
Class class2
{
                                       Static void main (string [] args)
                                       {
                                                List <int> list = new List<int>();
                                                List.Add(10);List.Add(20);List.Add(30);
                                                Foreach(int i in List)
                                                Console.write(i+””);
                                                Console.read();               
                                       }
}
34. Assemblies
·       An assembly is a unit of file that contains IL code corresponding to your project when it was complied.
·       The name of the assembly will be the same as project name from which it was created.
·       Assemblies were known as “unit of deployment” because once the application development is done what we install on client machine is assemblies only
·       An assembly file can have an extension of either exe or dll.
Assemblies are of two types
1.   Private Assembly
2.   Shared Assembly
Private Assembly:
By default every assembly is private, if the reference of these assemblies was added to any project; a copy of the assembly is created and given to the project, so each project maintains a private copy of the assembly.
Shared Assembly:
An assembly that was copied in to GAC (Global Assembly Cache) was known as “Shared Assembly”. If an assembly was shared multiple copies will not be created even if being consumed by multiple projects, only a single copy under GAC serves the entire project.
GAC was a folder under the windows Folder:
C:\windows\assemblyß-------GAC Folder
Note: All the BCL were shared dll assemblies so we can find then under the GAC.
35. What assemblies are copied in to GAC?
We can copy only strong named assemblies in to GAC.
36. What is a strong named Assembly?
An assembly which has 3 attributes like name, version and public key token value were known as “Strong Named Assemblies”.
37. How to generate a public key token?
To generate a public key token value we were provided with a tool strong name utility, which is a command like tool that should be used from Visual Studio command prompt as follows
Sn –k<filename>
Example: sn –k key.snk
38. What assembly contains internally?
An assembly internally contains 3 major things in it.
·       Manifest Info
·       Type Metadata
·       IL Code
Manifest Info: It contains information about the attributes that are associated with an assembly like Title, Description, Company, and Version.
Type Meta data: It contains the information about all the types under Assembly like Namespace, Classes, and their members Structures, Interfaces etc. Type metadata only describes about contents of an assembly so an assembly can be called as self describing unit of execution.
IL Code: These are language instructions we have defined which can be understood by CLR.

No comments:

Post a Comment