Object-Oriented Programming - .NET Interview Questions and Answers
What are methods?
Methods are the building blocks of a class, in which they are linked together to share and process data to produce the result. In other words, a method is a block of code that contains a series of statements and represents the behavior of a class. While declaring a method you need to specify the access specifier, the return value, the name of the method, and the method parameters. All these combined together is called the signature of the method.
What is a namespace?
Namespace is considered as a container that contains functionally related group of classes and other types.
Do events have return type?
No, events do not have return type.
What is the function of the Try-Catch-Finally block?
The try block encloses those statements that can cause exception and the catch block handles the exception, if it occurs. Catch block contains the statements that have to be executed, when an exception occurs. The finally block always executes, irrespective of the fact whether or not an exception has occurred. The finally block is generally used to perform the cleanup process. If any exception occurs in the try block, the program control directly transfers to its corresponding catch block and later to the finally block. If no exception occurs inside the try block, then the program control transfers directly to the finally block.
How can you prevent a class from overriding in C# and Visual Basic?
You can prevent a class from overriding in C# by using the sealed keyword; whereas, the NotInheritable keyword is used to prevent a class from overriding in Visual Basic.
What are abstract classes? What are the distinct characteristics of an abstract class?
An abstract class is a class that cannot be instantiated and is always used as a base class.
The following are the characteristics of an abstract class:
- You cannot instantiate an abstract class directly. This implies that you cannot create an object of the abstract class; it must be inherited.
- You can have abstract as well as non-abstract members in an abstract class.
- You must declare at least one abstract method in the abstract class.
- An abstract class is always public.
- An abstract class is declared using the abstract keyword.
The basic purpose of an abstract class is to provide a common definition of the base class that multiple derived classes can share.