You are on page 1of 12

1. What is an Abstract Class.?

Ans: The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class. The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Abstract classes have the following features: An abstract class cannot be instantiated. An abstract class may contain abstract methods and accessors. It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

2. What is the Keyword used in property. Ans: A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Keywords used in property are : get() and set() A get property accessor is used to return the property value, and a set accessor is used to assign a new value. 3. What is an Interface? Ans: Interface defines a contract. A contract between the creator of the interface and the implementer of the class that implements the interface, part of its aim is to tell the implementer what he/she should implement in their concrete class. An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition. Interface contains only operations, not implementation.

4. Does C# support multiple inheritances? Ans: No but it is possible to create multiple inheritance by using interface. 5. What can we do in C# to do kind of multiple inheritances? Ans: in c #, we cant derive class from multiple classes. However, it is possible to derive class from multiple interfaces. So, we can create multiple inheritance by using inheritance. For Ex, private Class derivedClass:baseClass , InterfaceX , InterfaceY { ......................................................................;;;;; }

6. What can we do restrict a Class to be inherited? Ans: This can be done by declaring the class as sealed class. For ex, sealed class Abc { -ststements } class Xyz : Abc //invalid(we can't inherited a sealed class) 7. What is a Delegate? Ans: Delegate is a pointer to a method. Delegate is a type which holds the method(s) reference in an object. A delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure.

8. What is method overloading? Ans: C# allows us to define multiple functions with the same name differing in the number type and order of arguments. For Ex, string Substring (int startIndex) string Substring (int startIndex, int length) 9. What is method overriding? Ans: Creating a method in derived class with same signature as a method in base class is called as method overriding. Same signature means methods must have same name, same number of arguments and same type of arguments. Method overriding is possible only in derived classes, but not within the same class. When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used. To allow the derived class to override a method of the base class, C# provides two options, virtual methods and abstract methods. 10. What is Reflection? Ans: Through Reflection, a program collects and manipulates its own metadata. It is a powerful mechanism to introspect the assemblies and objects at runtime. The Reflection APIs are contained in the System.Reflection namespace. 11. What is IDisposable? Ans: The primary use of IDisposable interface is to release unmanaged resources. Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed.

If the Dispose method is called, it frees the resources of the object, making finalization unnecessary. Dispose should call SuppressFinalize so the garbage collector does not call the finalizer of the object.Fmenifest 12. Difference b/w Abstract Class and Interface. Ans: Abstract Class Interface An abstract class is a special kind of An interface is not a class. It is an class that cannot be instantiated. entity that is defined by the word Interface. An interface is a reference type containing only abstract members. A class may inherit only one abstract A class may inherit several interfaces. class. An abstract class can provide complete, default code and/or just the details that have to be overridden. An abstract class can contain access modifiers for the subs, functions, properties Execution speed is fast An abstract class can have fields and constrants defined An interface cannot provide any code, just the signature. An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public Requires more time to find the actual method in the corresponding classes. No fields can be defined in interfaces

13. Can you have a public field in an Interface? Ans: Interfaces members are automatically public, and they cannot include any access modifiers. 14. Scenario based question on Constructor when a base class Constructor is called in child class, what is the sequence of events. Which info is displayed first? Ans: Base class constructors get called before derived class constructors, but derived

class initializers get called before base class initializers.


15. What is a sealed class, where you use it? Ans: A sealed class cannot be inherited. It is an error to use a sealed class as a base class. Use the sealed modifier in a class declaration to prevent inheritance of the class. 16. Where we use using other than namespace? Ans: The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. 17. What is the Difference between Array and ArrayList? Ans: Array Arraylist

The capacity of an Array is fixed


An Array is a collection of similar items An Array can have multiple dimensions Array is in the System namespace Char[] vowel=new Char[];

ArrayList can increase and decrease size dynamically


ArrayList can hold item of different types ArrayList always has exactly one dimension ArrayList is in the System.Collections namespace. ArrayList a_list=new ArrayList();

18. What is Hash List? Ans: Represents a collection of key/value pairs that are organized based on the hash code of the key. 19. What is the difference between Struct and a Class? Ans: Structure cant be inherited. But Class can be inherited. 1. Classes are reference types and structs are value types. Since classes are reference type, a class variable can be assigned null.But we cannot assign null to a struct variable, since structs are value type. 2. When you instantiate a class, it will be allocated on the heap.When you instantiate a struct, it gets created on the stack. 3. You will always be dealing with reference to an object ( instance ) of a class. But you will not be dealing with references to an instance of a struct ( but dealing directly with them ). 4. When passing a class to a method, it is passed by reference. When passing a struct to a method, its passed by value instead of as a reference. 5. You cannot have instance Field initializers in structs.But classes can have 20. What is the difference between String Builder and String? Ans: String are Immutable (Not Modifiable). If you try to modify the string it actually creates a new string and the old string will be then ready for garbage collection. StringBuilder when instantiated, creates a new string with predefined capacity and upto that capacity it can accodomate string without needing to create a new memory location for the string....i mean it is modifiable and can also grow as and when needed. Operation on StringBuilder object is faster than String operations, as the copy is done to the same location. Usage of StringBuilder is more efficient in case large amounts of string manipulations have to be performed. 21. What is the difference between Hash Table vs String Dictionary? Ans: Hash table Dictionary Is non-generic(you have to take care of Is generic(you can't insert any random type safety and cast it to appropriate object into it, and you don't have to data type when you take it out) cast the values you take out.)

In .Net Hashtable is thread safe for use by multiple reader threads and a single writing thread Use with web services

Dictionary offers no thread safety.

we can not use dictionary (generics) with web services

22. Difference between Dispose and Finalize? Ans: Finalize () is called by Garbage Collector implicitly to free unmanaged resources. The garbage collector calls this method at some point after there are no longer valid references to the object. There are some resources like windows handles, database connections which cannot be collected by the garbage collector. Therefore the programmer needs to call Dispose() method of IDisposable interface. Dispose () of IDisposable interface is called by the programmer to explicitly release resources when they are no longer being used. Dispose () can be called even if other references to the object are alive.

The important difference is timing. Finalize is called after the .NET garbage collector runs. That can take a while, depending on how frequently you allocate memory and in what generation the object lives. Dispose() is the standardized way to free up the unmanaged resources your object uses well before the finalizer gets a chance to do so.

23. What is method overload? Ans: C# allows us to define multiple functions with the same name differing in the number type and order of arguments. For Ex, string Substring (int startIndex) string Substring (int startIndex, int length) 24. Different access modifiers in c# Ans: Modifier Description public There are no restrictions on accessing public members. private Access is limited to within the class definition. This is the default access modifier type if none is formally specified protected Access is limited to within the class definition and any class that inherits from the class internal Access is limited exclusively to classes defined within the current project assembly protected Access is limited to the current assembly and types internal derived from the containing class. All members in current project and all members in derived class can access the variables. 25. Scope of protected internal Ans: protected internal means internal OR protected: The class is available within the same assembly.

The class is available to those who inherit from it from within the assembly or outside it. 26. Difference between arraylist and hashtable Ans: An ArrayList is a dynamic array that grows as new items are added that go beyond the current capacity of the list. Items in ArrayList are accessed by index, much like an array. The Hashtable is a hashtable behind the scenes. The underlying data structure is typically an array but instead of accessing via an index, you access via a key field which maps to a location in the hashtable by calling the key object's GetHashCode() method. Retrieving by key in Hashtable is faster than retrieving in Arraylist 27. What is the checked keyword used for Ans: The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions. 28. Difference between const and readonly Ans: A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, although a const field is a compile-time constant, the readonly field can be used for run-time constants. When using const, the value has to be set before compiling, and cannot be changed. Once you make your assembly, the value of the const is baked in there. If you want to change the value of the const, you must go back into your code, change the value, then recompile. If another assembly (Assembly B) wants to use the const in the first assembly (Assembly A), theconst is also baked in there as well. So, if you change the value of the const, not only will you have to rebuild your first Assembly (Assembly A), you will also have to build all other assemblies (Assembly B, C, D, ). Unlike const, when using readonly, the value is set when the instance is created. This means that you can specify the value of the readonly in the constructor of your class. Another difference between const and readonly, is that const are static by default, where withreadonly, you must define it as static (if you want it to be). As I mentioned above, when you change the value of a const, you must rebuild your assembly. So if the value can change, you should think about using readonly instead. 29. What is a static constructor and when is it used Ans: A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced. Static constructors have the following properties: A static constructor does not take access modifiers or have parameters.

A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. A static constructor cannot be called directly. The user has no control on when the static constructor is executed in the program. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file. Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method. 30. Whether performance of VB or C# is better 31. Class A 32. { i. Public int Test(int a, out int b) ii. { 1. b=a; 2. return a; iii. } } Does this program is going to compile , if compiles what is the value of a 33. How will call this method from inside the class and outside the class 34. What is Class Diagram Ans: In Visual Studio Ultimate, you can use a UML class diagram to describe data types and their relationships separately from their implementation. The diagram is used to focus on the logical aspects of the classes, instead of their implementation. To provide an implementation-independent description of the types that are used in a system and passed between its components. To clarify the glossary of terms used for communication between the application and its users, and in descriptions of the users' needs. They provide designers, architects and developers a graphical interface to manipulate methods, properties, fields, events for datastructures likeclasses,interfaces,structs,etc. In this article we will use this designer to create these datastructures without touching the code and while we are interacting with this designer we should note that the code will be generated and updated in the background. 35. How to make a method not to be inherited Ans: Use sealed keyword to declare those method. 36. What will happen, If we private access to the method in terms of Inheritance. Ans : Private method is not accessible in derived class. 37. Differences between abstract classes and Interface

38. Difference between Class and struct In terms Garbage Collection Ans: Creating a struct instance cannot cause a garbage collection (unless the constructor directly or indirectly creates a reference type instance) whereas creating a reference type instance can cause garbage collection. 39. Explain about Idisposable Ans: The System.IDisposable interface defines a method of releasing unmanaged resources which have been allocated. 40. What is the difference between Idisposable & Finalize Ans : A finalizer (aka destructor) is part of garbage collection (GC) - it is indeterminate when (or even if) this happens, as GC mainly happens as a result of memory pressure (i.e. need more space). Finalizers are usually only used for cleaning up unmanaged resources, since managed resources will have their own collection/disposal. Hence IDisposable is used to deterministically clean up objects, i.e. now. It doesn't collect the object's memory (that still belongs to GC) - but is used for example to close files, database connections, etc.

41. Class A { Public A() { 1. Console.WriteLine(Hello World1); } } Class B:A { Public B() { 2. Console.WriteLine(Hello World2); } } Class C { A a = new B(); } Explain the sequence of constructor firing Ans : First constructor for A is fired and then B is fired as the Class B is extended from A. 42. Class A { Public Void HelloWorld () { Console.WriteLine(Hello World1); } } Class B:A { Public void HelloWorld()

{ Console.WriteLine(Hello World2); } } Class C { A a = new B(); a.HelloWorld() } Which statement is going to printed. Ans : 43. Class A { Public static int I =10;Public int k = 10; Public static A() {} Public A() {} } Class B { A x = new A(); } Explain the Sequence of Events Ans : First Static constructor will be called and then default constructor will be called. 44. What is the difference between private and protected Ans : private will be accessed within the current class.But protected is accessible in current class as well as in derived class. 45. Can we implement multiple catch blocks Ans: Yes, we can implement catch blocks from most specific to most generic 46. How many catch blocks are going to fire an exception Ans : 47. The order of Catch blcoks Ans: From the most specific exceptions to the most generic ones. 48. C#, inheritance, interface ,VB. 49. Is there multiple Inheritance in C#. Ans :Yes, it supports using interfaces.In general, Multiple inheritance will be supported using two or more base classes. 50. Syntax of inheritance in C#. Ans : derived class :Base Class

51. Difference b/w C and C#. Ans: C is structed language

whereas C# is object

oriented. C# is case sensitive bt c is not. C is more on functions. C# is more on the designs. In C# Garbage collection is done by CLR. In C its not. C support pointer but C# is not
52. Can you have a public field in an Interface? Ans: Interfaces members are automatically public, and they cannot include any access modifiers. 53. Scenario based question on Constructor when a base class Constructor is called in child class, what is the sequence of events. Which info is displayed first? Ans : First Parent class constr is called first , then child class info is displayed later. 54. What is the difference between CTS and CLS? Ans : CTS will take care of datatype problems in .Net code compilation. Where CLS is responsible for Language Interoperablity. 55. Is VB.NET more performance efficient or C#? Ans : No, C# is more efficient. 56. Disadvantages of hosting assemblies in GAC, when to use private & when to use public.

Ans :

Loading assemblies from GAC mean less overhead and security that your application will always load correct version of .NET library You shouldn't ngen assemblies that are outside of GAC, because there will be almost no performance gain, in many cases even loss in performance. You're already using GAC, because all standard .NET assemblies are actually in GAC and ngened (during installation). Using GAC for your own libraries adds complexity into deployment, I would try to avoid it at all costs. Your users need to be logged as administrators during installation if you want to put something into GAC, quite a problem for many types of applications.

57. what is an Iunknown interface Ans: COM interfaces are used primarily by unmanaged code. If you need the C# you are writing to interoperate with unmanaged code, you can export it as COM interfaces: COM Interop with C# IUnknown is generally the base class for COM objects. 58. what is diff between internal and protected internal 59. How to make a method not to be inherited

60. What will happen, If we private access to the method in terms of Inheritance Ans: We cant use any of the private methods by creating object of that class. 61. what will be the access level of protected Internal Ans : Access is limited to the current assembly or types derived from the

containing class.
62. What is a mutex class Ans: A synchronization primitive that can also be used for interprocess synchronization. 63. What is Monitor Class Ans: Provides a mechanism that synchronizes access to objects. 64. Two interface I1 and I2 with a method definition X, Implemented by a class A, How to implement for both Interfaces. Ans : we can create class A to implement I1 and I2 by using multiple inheritance and provide definitions to both interfaces. 65. Can Interface can have return types Ans: Yes, interface can have return type. 66. Is it necessary to implement all the methods in an abstract class Ans : Yes - It is Necessary to implement all methods of the abstract class

in your class - If you want a concrete class that can be instantiated. If you dont plan on implementing all methods, then you have to declare your current class too as Abstract. Abstract classes are generally used where you want an amount of behaviour to be used by the class that extends the abstract class while at the same time giving options to the child class to provide a certain amount of behaviour itself.

67. What will be the access modifiers for an abstract method Ans : By default Abstract and public. 68. What is metadata? Ans: .NET metadata, in the Microsoft .NET framework, refers to certain data structures embedded within the Common Intermediate Language code that describes the high-level structure of the code. Metadata describes all classes and class members that are defined in the assembly, and the classes and class members that the current assembly will call from another assembly. 69. What is a Manifest? Ans: Assembly metadata is stored in the Manifest. Manifest contains all the metadata needed to do the following things: Version of assembly. Security identity. Scope of the assembly. Resolve references to resources and classes. The assembly manifest can be stored in a PE file either (an .exe or) .dll with Microsoft intermediate language (MSIL code with Microsoft intermediate language (MSIL) code or in a stand-alone PE file, which contains only assembly manifest information.

70. What is the difference between Idisposable & Finalize 71. When do you use Idisposal? If you implement Idisposal forUnmanaged code, will GC still takes care of freeing the objects? 72. Explain about Runtime Polymorphism Ans: By runtime polymorphism we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding . 73. Explain code for Implementing the finalize, Finally, Dispose in a C# class 74. Does C# support pointers Ans : No, by default C# does not support pointer arithmetic. However, by using the unsafe keyword, it is possible to define an unsafe context in which pointers can be used. 75. What is extension methods? Ans: An extension method is a special kind of static method that can be associated with a type, so that it can be called as if it were an instance method of the type. This feature enables you to, in effect, add new methods to existing types wi thout a new derived type, recompiling, or otherwise modifying the original type. Extension methods are defined as static methods within a static class. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with the using directive or if defined directly in the source code.

76. What is boxing and unboxing Ans: Boxing is a process of converting a variable from value type to ref type Unboxing is a process of converting from ref type to value type.

You might also like