Friday, May 23, 2014

Sealed and Partial classes of C#.Net Interview Questions(16)

Sealed and Partial classes of C#.Net Interview Questions(16)


1.What is sealed class?
A)Sealed is a keyword.It prevents other classes from inheriting from it.
                                       (or)
A class from which it is not possible to derive a new class is known as sealed class.To make any class as sealed we use sealed keyword.

2.What is the purpose of sealed class?
A)Purpose of sealed class is used to avoid inheritance.

3.List for which we can use sealed keyword?
A)Sealed keyword can be used with classes,instance methods and properties.

4.Can sealed class used as a base class?
A)No.Sealed class never be used as base class.
5.List the different entities that are not present in sealed class?
A)Following are the entities that are not present in the sealed class.They are:
         Ø  Sealed class cannot contain virtual functions
         Ø  Sealed class cannot contain abstract functions
         Ø  Sealed class cannot contain Protected members

6.Diffrence between an abstract class and a sealed class?
SNo
Abstract Class
Sealed Class
1
A class which contains one or more abstract functions is known as an abstract class
A class from which it is not possible to derive a new class is known as a sealed class
2
Abstract class contains abstract and Non abstract functions
Sealed class contains Non-abstract methods
3
Creating a new class from an abstract class is compulsory to consume it
It is not possible to create an object a new class from sealed class.
4
Use abstract keyword to make any class as abstract
Use sealed keyword to make any class as sealed
5
An abstract class cannot be the bottom most class with in the inheritance hierarchy
Sealed class should be the bottom most class with in the inheritance hierarchy
6
An abstract class should be used as base class only
A Sealed class cannot be used as base class

7.What is Partial class?
A)A class which code can be written in more than one files with in the same name is called as partial class.We can use partial keyword for making a class a partial class.
8.List the situations where we can use partial classes?
A)Following are the situations for usage of partial classes.They are:
        Ø  When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.
        Ø  When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.
9.What is the advantage of partial class?
A)Partial classes will provide flexibility in application development.

10.What is Partial Method?
A) A partial class or struct may contain a partial method. One part of the class contains the signature of the method. An optional implementation may be defined in the same part or another part. If the implementation is not supplied, then the method and all calls to the method are removed at compile time.

11. Is it possible to create partial structs, interfaces and methods?
A)Yes, it is possible to create partial structs, interfaces and methods. We can create partial structs, interfaces and methods the same way as we create partial classes.

12.Can you create partial delegates and enumerations?
A)No, you cannot create partial delegates and enumerations.

13. Can different parts of a partial class inherit from different interfaces?
A) Yes, different parts of a partial class can inherit from different interfaces.

14. Can you specify nested classes as partial classes?
A) Yes, nested classes can be specified as partial classes even if the containing class is not partial.

15. The following are the points to keep in mind when creating partial methods?
A)
1. Partial method declarations must begin partial keyword.
2. The return type of a partial method must be void.
3. Partial methods can have ref but not out parameters.
4. Partial methods are implicitly private, and therefore they cannot be virtual.
5. Partial methods cannot be extern, because the presence of the body determines whether they are defining or implementing.

16. What is the use of partial methods?
A)Partial methods can be used to customize generated code. They allow for a method name and signature to be reserved, so that generated code can call the method but the developer can decide whether to implement the method.

0 comments:

Post a Comment