Monday, March 25, 2013

OOps interview Questions

OOps interview Questions


1) What is meant by Object Oriented Programming? 

     OOP is a method of programming in which programs are organised as cooperative collections of objects. Each object is an instance of a class and each class belong to a hierarchy.

2) What is a Class? 
     Class is a template for a set of objects that share a common structure and a common behaviour. The keyword class in C# indicates that we are going to define a new class (type of object)

3) What is an Object? 

   Object is anything that is identifiable as a single material item.  Object is an instance of a class. It has state,behaviour and identity. It is also called as an instance of a class.  

4) What is an Instance? 

     An instance has state, behaviour and identity. The structure and behaviour of similar classes are defined in their common class. An instance is also called as an object.

5) What are the core OOP’s concepts? 

     Abstraction, Encapsulation,Inheritance and Polymorphism are the core OOP’s concepts.

6) What is meant by abstraction? 

     Abstraction defines the essential characteristics of an object that distinguish it from all other kinds of objects. Abstraction provides crisply-defined conceptual boundaries relative to the perspective of the viewer. Its the process of focussing on the essential characteristics of an object. Abstraction is one of the fundamental elements of the object model.

7) What is meant by Encapsulation? 

     Encapsulation is the process of compartmentalising the elements of an abtraction that defines the structure and behaviour. Encapsulation helps to separate the contractual interface of an abstraction and implementation.

8) What is meant by Inheritance? 

  It provides a convenient way to reuse existing fully tested code in different context thereby saving lot of coding. Inheritance is a relationship among classes, wherein one class shares the structure or behaviour defined in another class. This is called Single Inheritance. If a class shares the structure or behaviour from multiple classes, then it is called Multiple Inheritance. Inheritance defines “is-a” hierarchy among classes in which one subclass inherits from one or more generalised superclasses.

9) What is meant by Polymorphism? 

     Polymorphism literally means taking more than one form. Polymorphism is a characteristic of being able to assign a different behavior or value in a subclass, to something that was declared in a parent class.

10) What is an Abstract Class? 

     Abstract class is a class that has no instances. An abstract class is written with the expectation that its concrete subclasses will add to its structure and behaviour, typically by implementing its abstract operations.

There are scenarios in which it is useful to define classes that is not intended to instantiate; because such classes normally are used as base-classes in inheritance hierarchies, we call such classes abstract classes. 

Abstract classes cannot be used to instantiate objects; because abstract classes are incomplete, it may contain only definition of the properties or methods and derived classes that inherit this implements it's properties or methods. 

Static, Value Types & interface doesn't support abstract modifiers. Static members cannot be abstract. Classes with abstract member must also be abstract.


11) What is an Interface? 

An interface is a contract & defines the requisite behavior of generalization of types. 

Interface is an outside view of a class or object which emphaizes its abstraction while hiding its structure and secrets of its behaviour.
An interface mandates a set of behavior, but not the implementation. Interface must be inherited. We can't create an instance of an interface
An interface is an array of related function that must be implemented in derived type. Members of an interface are implicitly public & abstract. An interface can inherit from another interface.
12) What is a base class? 


     Base class is the most generalised class in a class structure. Most applications have such root classes. In Java, Object is the base class for all classes.

13) What is a subclass? 

     Subclass is a class that inherits from one or more classes.

14) What is a superclass? 

     superclass is a class from which another class inherits.

15) What is a constructor? 

     Constructor is an operation that creates an object and/or initialises its state.

16) What is a destructor? 

     Destructor is an operation that frees the state of an object and/or destroys the object itself.

17) What is meant by Binding? 

     Binding denotes association of a name with a class.

18) What is meant by static binding? 

     Static binding is a binding in which the class association is made during compile time. This is also called as Early binding.

19) What is meant by Dynamic binding? 

     Dynamic binding is a binding in which the class association is not made until the object is created at execution time. It is also called as Late binding.

20) Define Modularity? 

     Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.

21) What is meant by Persistence?

     Persistence is the property of an object by which its existence transcends space and time.

22) How to prevent a class from being inherited?

In order to prevent a class in C# from being inherited, the keyword sealed is used. Thus a sealed class may not serve as a base class of any other class. It is also obvious that a sealed class cannot be an abstract class.
//C# Example
sealed class MyClass
{
    public int x;
    public int y;
}
No class can inherit from MyClass defined above. Instances of ClassA may be created and its members may then be accessed.


23) What is Polymorphism?

Polymorphism means one interface and many forms. Polymorphism is a characteristics of being able to assign a different meaning or usage to something in different contexts specifically to allow an entity such as a variable, a function or an object to have more than one form. 

There are two types of Polymorphism. 

Compile time: function or operator overloading 
Runtime: Inheritence & virtual functions


24) What is Abstract method?

Abstract method doesn't provide the implementation & forces the derived class to override the method.

25) What is Virtual method?

Virtual Method allows a  derived class with the option to override it.

25) Can Struct be inherited?

No, Struct can't be inherited as this is implicitly sealed.


26) What is Static field?

To indicate that a field should only be stored once no matter how many instance of the class we create.

27) What is Static Method?

It is possible to declare a method as Static provided that they don't attempt to access any instance data or other instance methods.



28) What is Virtual keyword?

This keyword indicates that a member can be overridden in a child class. It can be applied to methods, properties, indexes and events.


29) What is New modifiers?

The new modifiers hides a member of the base class. C# supports only hide by signature.


30) What is Sealed modifiers?

Sealed types cannot be inherited & are concrete. 
Sealed modifiers can also be applied to instance methods, properties, events & indexes. It can't be applied to static members. 
Sealed members are allowed in sealed and non-sealed classes.

31) When to use Interface over abstract class?

Abstract Classes: Classes which cannot be instantiated. This means one cannot make a object of this class or in other way cannot create object by saying 
ClassAbs abs = new ClassAbs(); where ClassAbs is abstract class. 
Abstract classes contains one or more abstarct methods, ie method body only no implementation. 
Interfaces: These are same as abstract classes only difference is that we can only define method definition and no implementation. 
When to use what depends on various reasons. One being design choice. 
One reason for using abstarct classes is we can code common functionality and force our developer to use it. I can have a complete class but I can still mark the class as abstract. 
Developing by interface helps in object based communication.

32) What is pure virtual function?

When you define only function prototype in a base class without and do the complete implementation in derived class. This base class is called abstract class and client won’t able to instantiate an object using this base class. 

A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be "pure" using the curious "=0" 
syntax: 
class Base { 
public: 
void f1(); // not virtual 
virtual void f2(); // virtual, not pure 
virtual void f3() = 0; // pure virtual 
};


33) Can we specify the access modifier for explicitly implemented interface method?

No, we can't specify the access modifier for the explicitly implemented interface method. By default its scope will be internal.

34) What is Protected access modifier in C#?

The protected keyword is a member access modifier. It can only be used in a declaring a function or method not in the class ie. a class can't be declared as protected class. 

A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declare this member. In other words access is limited to within the class definition and any class that inherits from the class 

A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type. 

35) What is Public access modifier in C#?

The public keyword is an access modifier for types and type members ie. we can declare a class or its member (functions or methods) as Public. There are no restrictions on accessing public members.

36) What is Private access modifier in C#?

The private keyword is a member access modifier ie. we can't explicitly declare a class as Private, however if do not specify any access modifier to the class, its scope will be assumed as Private. Private access is the least permissive access level of all access modifiers. 

Private members are accessible only within the body of the class or the struct in which they are declared. This is the default access modifier for the class declaration. 

37) What is Internal access modifier in C#?

The internal keyword is an access modifier for types and type members ie. we can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll). In other words, access is limited exclusively to classes defined within the current project assembly. 

38 ) What is Protected Internal access modifier in C#?

Protected Internal is a access modifiers for the members (methods or functions) ie. you can't declare a class as protected internal explicitly. The members access is limited to the current assembly or types derived from the containing class. 

Protected Internal means the method is accessible by anything that can access the protected method UNION with anything that can access the internal method. 

39) What all classes bear Default Access modifiers in C#?

a) An enum has default modifier as public 

b) A class has default modifiers as Internal .
 It can declare members (methods etc) with following access modifiers: 
public 
internal 
private 
protected internal 

c) An interface has default modifier as public 

d) A struct has default modifier as Internal .
It can declare its members (methods etc) with following access modifiers: 
public 
internal 
private 

e) A methods, fields, and properties has default access modifier as "Private" if no modifier is specified.


40) What is method overloading?
Method overloading occurs when a class contains two methods with the same name, but different signatures.Method overloading allows us to write different version of the same method in a class or derived class. Compiler automatically select the most appropriate method based on the parameter supplied. 

Example:

public class AddClass

{

    public int Add(int a, int b)

    {

        return a + b;

    }

   public int Add(int a, int b, int c)

    {

        return a+b+c;

    }     

}
To call the above method, you can use following code. 

AddClass ad= new AddClass();

int number = ad.Add(2, 3) // result =5

int number1 = ad.Add(2, 3, 4) // result = 9

Rules for Overloading
There must be changes in either return type,or number of parameters or type of parameters.
You can't have a overload method with same number parameters but different return type. In order to create overload method, the return type must be the same and parameter type must be different or different in numbers.

41) What is Overriding?

Method overriding is a feature that allows to invoke functions (that have the same signatures) and that belong to different classes in the same hierarchy of inheritance using the base class reference. In C# it is done using keywords virtual and override

42) What is Method Overriding? How to override a function in C#?

Use the override modifier to modify a method, a property, an indexer, or an event. An override method provides a new implementation of a member inherited from a base class. The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method. 
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.

43) Can we call a base class method without creating instance?
Yes. But .. 

* Its possible If its a static method. 

* Its possible by inheriting from that class also. 

* Its possible from derived classes using base keyword.


44) In which cases you use override and new base?

Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.

45) Difference between new and override keyword?

Consider the following program. 

using System;

 class Program
    {
        public class BaseClass
        {

            public virtual void func1()
            {

               Console.WriteLine("Base Class function 1.");

            }



            public virtual void func2()
            {

               Console.WriteLine("Base Class function 2.");

            }



            public void func3()
            {

               Console.WriteLine("Base Class function 3.");

            }

        }



        public class DeriveClass : BaseClass
        {

            public new void func1()
            {

               Console.WriteLine("Derieve Class fuction 1 used new keyword");

            }



            public override void func2()
            {

               Console.WriteLine("Derieve Class fuction 2 used override keyword");

            }



            public void func3()
            {

               Console.WriteLine("Derieve Class fuction 3 used override keyword");

            }



        }

        static void Main(string[] args)
        {
            BaseClass b = new BaseClass();

            b.func1();



            DeriveClass d = new DeriveClass();

            d.func1();



            //Calls Base class function 1 as new keyword is used.

            BaseClass bd = new DeriveClass();

            bd.func1();



            //Calls Derived class function 2 as override keyword is used.

            BaseClass bd2 = new DeriveClass();

            bd2.func2();

            Console.Read();

        }
    }




Now the difference is 

new: hides the base class function. 
Override: overrides the base class function. 


BaseClass objB = new DeriveClass();


If we create object like above notation and make a call to any function which exists in base class and derive class both, then it will always make a call to function of base class. If we have overidden the method in derive class then it wlll call the derive class function. 

For example… 


objB.func1(); //Calls the base class function. (In case of new keyword)

objB.func2(); //Calls the derive class function. (Override)

objB.func3(); //Calls the base class function.(Same prototype in both the class.)

Note: 
// This will throw a compile time error. (Casting is required.) 

DeriveClass objB = new BaseClass(); 


//This will throw run time error. (Unable to cast) 

DeriveClass objB = (DeriveClass) new BaseClass(); 

0 comments:

Post a Comment