Friday, May 23, 2014

Polymorphism and Inheritance of C#.Net Interview Questions(54)

Polymorphism and Inheritance of C#.Net Interview Questions(54)







1.Define Polymorphism?
A) Polymorphism is derived from Greek word. Where Poly means many morph means mechanisms.Same function/operator will show different behaviors when passed different type of values or different number of values.
                                   (Or)
Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance. Polymorphism is a Greek word that means "many-shaped".
             Polymorphism can be classified into two types.

2.List the types of Polymorphism?
A) Polymorphism can be classified into two types.They are:
                 Ø  Static/Compile Time  Polymorphism/Early Binding
                 Ø  Dynamic/Run Time  Polymorphism/Late Binding

3.How static Polymorphism  can be achieved?
A) static Polymorphism can be achieved using
                 Ø  Function Overloading
           Ø  Operator Overloading
4.How Dynamic Polymorphism  can be achieved?
A)Dynamic Polymorphism can be achieved using
            Ø  Function Overriding
5.Define Function overloading?
A)Providing new implementation to a function with different signature is known as function overloading.

6.Can function overloading can be implemented in derived class?
A)No.Function overloading can be implemented with in the same class.

7.Define Function signature?
A)Signature of a function consists of
                     Ø  Name of the method
                     Ø  Types of parameters
                     Ø  Number of parameters
                     Ø  Modifiers
8.What is operator overloading?
A)Providing new implementation to a operator is known as operator overloading.

9.Is all operators supports operator overloading?
A)No.Some operators only supports operator overloading.

10.Define Method overriding or Function overriding?
A) Method overriding or Function overriding is providing new implementation to a function with same signature.

11.Can we apply Method overriding or Function overriding with in a same class?
A)Generally  Method overriding or Function overriding will be implemented with in the derived class.

12.What is the keyword used for method overriding?
A) The overridden base method must be virtual, abstract, or override.You cannot override a non-virtual or static method.

13.Is both the override method and virtual method have same accessibility?
A) Yes. Both the override method and the virtual method must have the same access level modifier.

14.List the keywords that cannot use to modify an override method?
A) We cannot use the new, static, or virtual modifiers to modify an override method.

15.Can we override non virtual methods?
A) We cannot override a non-virtual method. By default, methods are non-virtual.

16.What is the difference between function overloading and function overriding?
A)
SNo
Function Overloading
Function Overriding
1
Providing new implementation to a function with same name and different signature is known as function overloading
Providing new implementation to a function with same name and same signature is known as function overriding
2
Function Overloading will be done in the same class
Function overriding will be done in the base and derived classes
3
This is code refinement technique
This is code replacement technique
4
No separate keywords are used to implement function overloading
Use virtual keyword for base class function and override keyword in derived class function to implement function overriding
5
Used to implement static polymorphism
Used to implement dynamic polymorphism

 17)What is virtual keyword?
A) The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.

18.List the limitations of virtual modifiers?
A)Virtual modifiers cannot be used with the following modifiers.They are:
                         Ø  Static
                         Ø  Abstract
                         Ø  Private
                         Ø  Override

19.When we apply a virtual modifier on a static property in a program?
A)No.It gives an error.

20.What is New keyword?
A) In C#, the new keyword can be used as an operator, a modifier, or a constraint.

21.Define New Operator?
A) Used to create objects and invoke constructors.

22.Define New Modifier?
A) Used to hide an inherited member from a base class member.

23.Define New Constraint?
A) Used to restrict types that might be used as arguments for a type parameter in a generic declaration.

24.List the operators that cannot supports operator overloading?
A)
SNO
Operator
Name
1
.
Member Selection
2
.*
Pointer-to-member selection
3
::
Scope Resolution
4
?:
Conditional
5
#
Preprocessor convert to string
6
##
Preprocessor concatenate

25.Define inheritance?
A)Acquiring the properties of parent class to child class is called inheritance.
                                   (or)
Inheritance describes the ability to create new classes based on an existing class.

26.How many types of inheritance are there?
A)There are 5 types of inheritance are there.They are
                  Ø  Single Inheritance
                  Ø  Multiple Inheritance
                  Ø  Multilevel Inheritance
                  Ø  Hybrid Inheritance
                  Ø  Hierarcial Inheritance

27.How do you inherit from a class in C#?
A)Place a colon and then the name of the base class. Notice that it's double colon in C++.

28.Define Single Inheritance?
A)Creating a single new class from single base class is known as single inheritance.

29.Define Multiple Inheritance?
A)Creating a new class from two or more base classes is known as multiple inheritance.

30.Define Multilevel Inheritance?
A)Creating a new class from already derived class is known as multilevel inheritance.

31.Define Hybrid Inheritance?
A)It is the combination of both multilevel and multiple inheritance.

32.Is Multiple inheritance supported by c#.Net?
A)Yes.By using Interfaces we can achieve multiple inheritance in C#.Net.

33.When you inherit a protected class-level variable, who is it available to?
A)Derived Classes.

34.What's the top .NET class that everything is derived from?
A)System.Object.

35.Define Interface?
A)Collection of abstract functions in a class is known as interface.It is similar to a class.But unlike classes interfaces do not provide implementation.Interfaces can be preceded with interface keyword.

36.List the Properties of Interfaces?
A)An interface has the following properties:

         Ø  An interface is like an abstract base class. Any class or struct that implements the interface must implement all its members.

        Ø  An interface can't be instantiated directly. Its members are implemented by any class or struct that implements the interface.

        Ø  Interfaces can contain events, indexers, methods, and properties.

        Ø  Interfaces contain no implementation of methods.

        Ø  A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.

37.Can we inherit multiple interfaces in C#.Net?
A)Yes.We can inherit multiple interfaces in C#.Net.

38.Declare the structure of interface?
A)Acessspecifier interface interfacename
{
Returntype methodname();
}
E.G:
Public interface emp
{
Void bdata();
Void einfo();
}

39. List the entities that interface contains?
A) Interface contains following entities. They are:
              Ø  Abstract functions
              Ø  Properties
              Ø  Indexes
              Ø  Events

40. List the entities that interface cannot contains?
A) Interface cannot contain following entities. They are:
           Ø  No-Abstract functions
           Ø  Data Fields
           Ø  Constructors
     Ø  Destructors

41.Can an interface contains fields?
A)No,an interface cannot contains fields.

42. Can we create an instance of an interface?
A)No, we cannot create an instance of an interface.

43. What do you mean by "Explicitly Implemeting an Interface". Give an example?
A)If a class is implementing the inherited interface member by prefixing the name of the interface, then the class is "Explicitly Implemeting an Interface member". The disadvantage of Explicitly Implemeting an Interface member is that, the class object has to be type casted to the interface type to invoke the interface member.

44.Can we provide access specifiers for a interface members? 
A)No.Because defaultly interface members are public types.If we declare any accessspecifiers it gives an error while at program runtime.

45.What is the use of interfaces?
A)Interfaces are used to implement multiple inheritance in C#.Net.

46.Why can't you specify the accessibility modifier for methods inside the interface?
A)They all must be public. Therefore, to prevent you from getting the false impression that you have any freedom of choice,you are not allowed to specify any accessibility, it's public by default.

47.What is abstract class?
A)A class which contains one or more abstract functions is known as an abstract class.To make any class as abstract use abstract keyword.

48.What is abstract function? 
A)A function which contains only declaration/signature and doesn’t contain the implementation/body/definitions is known as abstract function.To make any function as abstract use abstract keyword.

49.Is abstract function should be overriding?
A)Yes.Overriding of an abstract function is compulsory.

50.Is abstract class can be instantiated?
A)No.An abstract class can’t be instantiated directly. 

51.What is Instantiation?
A)Creating an object for a class or initializing a class members is called instantiation.

52.Can we declare  access specifiers of an abstract class methods?
A)Yes.We can declare acess specifiers of an abstract class methods.By default abstract class functions are not treated as public and abstract.

53. What's the difference between an interface and abstract class?
A)Following are the differences between abstract and interfaces :-

SNO
Abstract Class
Interface
1
A Class which contains one or more abstract functions is known as abstract class.
A class which contains all abstract functions is known as an interface.
2
An Abstract class can contain non abstract functions.
An interface cannot contain non abstract functions
3
An abstract class can contain all members of class
An interface can contain only abstract functions,properties,indexers,events and cannot contain non abstract functions,data fields,constructors and destructors.
4
Use abstract keyword to create an abstract class
Use interface keyword to create an interface
5
An abstract class can’t be used to implement multiple inheritance.
An interface can be used to implement multiple inheritance.
6
By Default abstract class members are not public and not abstract
By default interface members are public and abstract
7
An abstract class cannot be instantiated directly
An Interface cannot be instantiated directly
8
Creating a new class from an abstract class is must to consume it
Creating a new class from an interface is must to consume it.

54. When do you absolutely have to declare a class as abstract?
A) 1. When at least one of the methods in the class is abstract.
2. When the class itself is inherited from an abstract class, but not all base abstract methods  have been overridden.


0 comments:

Post a Comment