Monday, 8 December 2014

Object-Oriented Programming – .NET Interview Questions and Answers

13. What is the difference between a class and a structure?
Class:
  1. A class is a reference type.
  2. While instantiating a class, CLR allocates memory for its instance in heap.
  3. Classes support inheritance.
  4. Variables of a class can be assigned as null.
  5. Class can contain constructor/destructor.
Structure:
  1. A structure is a value type.
  2. In structure, memory is allocated on stack.
  3. Structures do not support inheritance.
  4. Structure members cannot have null values.
  5. Structure does not require constructor/destructor and members can be initialiazed automatically.
14. What are similarities between a class and a structure.
Structures and classes are the two most important data structures that are used by programmers to build modular programs by using OOP languages, such as Visual Basic .NET, and Visual C#. The following are some of the similarities between a class and a structure:
  • Access specifiers, such as publicprivate, and protected, are identically used in structures and classes to restrict the access of their data and methods outside their body.
  • The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.
  • Both can have constructors, methods, properties, fields, constants, enumerations, events, and event handlers.
  • Both structures and classes can implement interfaces to use multiple-inheritance in code.
  • Both structures and classes can have constructors with parameter.
  • Both structures and classes can have delegates and events.
15. What is a multicast delegate?
Each delegate object holds reference to a single method. However, it is possible for a delegate object to hold references of and invoke multiple methods. Such delegate objects are called multicast delegates or combinable delegates.
16. Can you declare an overridden method to be static if the original method is not static?
No. Two virtual methods must have the same signature.
17. Why is the virtual keyword used in code?
The virtual keyword is used while defining a class to specify that the methods and the properties of that class can be overridden in derived classes.
18. Can you allow a class to be inherited, but prevent a method from being overridden in C#?
Yes. Just declare the class public and make the method sealed.
19. Define enumeration?
Enumeration is defined as a value type that consists of a set of named values. These values are constants and are called enumerators. An enumeration type is declared using the enum keyword. Each enumerator in an enumeration is associated with an underlying type that is set, by default, on the enumerator. The following is an example that creates an enumeration to store different varieties of fruits:
enum Fruits {Mango, Apple, orange, Guava};
In the preceding example, an enumeration Fruits is created, where number 0 is associated with Mango, number 1 with Apple, number 2 with Orange, and number 3 with Guava. You can access the enumerators of an enumeration by these values.
20. In which namespace, all .NET collection classes are contained?
The System.Collections namespace contains all the collection classes.
21. Is it a good practice to handle exceptions in code?
Yes, you must handle exceptions in code so that you can deal with any unexpected situations that occur when a program is running. For example, dividing a number by zero or passing a string value to a variable that holds an integer value would result in an exception.
22. Explain the concept of constructor?
Constructor is a special method of a class, which is called automatically when the instance of a class is created. It is created with the same name as the class and initializes all class members, whenever you access the class. The main features of a constructor are as follows:
  • Constructors do not have any return type.
  • Constructors can be overloaded.
  • It is not mandatory to declare a constructor; it is invoked automatically by .NET Framework.
23. Can you inherit private members of a class?
No, you cannot inherit private members of a class because private members are accessible only to that class and not outside that class.
24. Does .NET support multiple inheritance?
.NET does not support multiple inheritance directly because in .NET, a class cannot inherit from more than one class. .NET supports multiple inheritance through interfaces.

No comments:

Post a Comment