oops Interview Question

  1. What is oop? 
Object oriented programming is a technique in which we focus on using objects for designing and building our application.

 Keep in your mind

Oop is not a new language. It is not a new thing which make us confuse. oops is a technique in which we make are programming so much efficient. It make our development so much easy. we make classes and objects is programming.

2.What is Object and Class?

Class is a template or blueprint which contain properties, members and method. Class define a particular structure of any thing.

Class myClass
{
              private int a; // class member variable
              private int b; // class member variable
        
              public int A{get; set; }//Property for variable a
              public int B{get; set; }//Property for variable b

              public void myFunction()        // Method of class 
              {
                               // your code goes here
              }
}

Object is the instance of class.We initialise the object using new keyword.     
myClass obj = new myClass();
Now methods and properties can be accessed from any where using this object. Now I have created this object I can access it
                         obj.myFunction() // function call
                         obj.A = 10; // Set a property using my object;
                         int val = obj.A // Get a property using my object.

3.Properties of oops ?

                        Main properties of oop listed below
                       >  Encapsulation
                       >  Abstraction
                       >  Inheritance
                       >  Polymorphism

4. What is encapsulation ?


Wrapping up data members, method and properties into a single unit (Class) is called encapsulation. Everything hides in the class. We give a proper way that is object. this can access data members and all public methods.

5. What is abstraction  with real example?

Hide all other information and provide only a relevant information is abstraction
Example:
A car itself a well define object which is composed of several smaller objects like gear system, steering mechanism, engine etc. But for human car is single object.

6. What is Polymorphism with real example ?

Polymorphism mean "having several behaviour" One thing having different behaviour is known as polymorphism.
Example:
A man is in the market he may be customer or trader or worker. Same man sitting in class as a student and if goes for interview he act as interview. He is showing different behaviour on different places. So he achieving polmorphism.

 It can achieved through "overloading and overriding".

7. What Inheritance ?
 In object-oriented programming, inheritance enables new objects to take on the properties of existing objects. A class that is used as the basis for inheritance is called a superclass or base class. A class that inherits from a superclass is called a subclass or derived class. The terms parent class and child class are also acceptable terms to use respectively. A child inherits visible properties and methods from its parent while adding additional properties and methods of its own.
Subclasses and superclasses can be understood in terms of the is a relationship. A subclass is a more specific instance of a superclass.
For example, an orange is a citrus fruit, which is a fruit. A shepherd is a dog, which is an animal.
8. Difference between encapsulation and abstraction ?

                          Abstraction is a process in which you "Throw away" unnecessary details from                                  entity and keep only relevant data or properties.
                            Example of car
                            Encapsulation in "Binding" of data members, operations and method etc.

9. Types of Inheritance ?
                        > Single Inheritance 
                            


                        
                       > Multiple Inheritance

                   
                       > Multilevel Inheritance
                     

                       > Hierarchical Inheritance
                     
                       > Hybrid Inheritance
                       

10. Access modifiers of inheritance ?

List of access modifier are
> Public
              All the members remain as it is in derived class as in the base class
> Protected
              If we inherit a class using protected keyword. All the member of base class become                              protected only public and private members remain as private
> Private
              If we inherit a class privately. All the member of base class become private either public or                 protected.

11. How to achieve polymorphism ?

Polymorphism is achieved by "Overloading and overriding"

12. Difference between overloading and overriding ?

Overloading:


  • Overloading happen in compile time
  • It happen in same class
  • class having more than one function with same name but different in parameter list or differ in parameter arrangement and also may differ its return type.
  • Return type doesn't matter in overloading
Overriding
  • It happens in runtime
  • It happen in child class
  • Declare a method in child class that already present in parent class or its prototype present in it
  • Same like parent method
  • Parameters,Return type and name cannot be changed.


13. How to stop a class to inherited ?

by adding "Sealed " keyword with class.

14. Abstract class and interface ?

Interface:

  • Support multiple inheritance
  • Doesn't contain data members
  • Doesn't contain constructors
  • only contain methods prototypes
  • can't have access modifier only public which is by default.
  • can't have static members
Abstract class:

  • can't support multiple inheritance
  • Does contain data member
  • Does contain constructors
  • Contain complete methods and prototypes also
  • can have access modifier
  • Can have static members

15. Can we inherit static class ?

No

16. what are static constructors ?

Static constructors are same like other constructors but static constructors only initialise the static members only.

17. why we make private constructor?

To stop object initialisation we make constructors private. In singleton pattern we make our constructor private.

18. Difference between constructor and destructor ?

Constructor is used to initialise the instance of a class. 
Constructor is Called when new instance of a class is created. 
Destructor destroys the objects when they are no longer needed. 
Destructor is called when instance of a class is deleted or released


19. Difference between final and sealed keywords ?

Final is Java keyword
Sealed is C# keyword
both are used to make class uninheritable


20. Difference between Final, finally and finalize keywords ?

Final is java keyword to make a class unInheritable
finaly is used with try catch block
finalize is the method of destructor

21. what is concrete class ? 

An abstract class is meant to be used as the base class from which other classes are derived. The derived class is expected to provide implementations for the member functions that are not implemented in the base class. A derived class that implements all the missing functionality is called a concrete class 

22. Difference between static constructors,private constructors and other constructors ?

Static constructors are same like other constructors but static constructors only initialise the static members only.

To stop object initialisation we make constructors private. In singleton pattern we make our constructor private.

Constructor is used to initialise the instance of a class. Constructor is Called when new instance of a class is created. 

23. Problems with multiple inheritance ?

Diamond problem can be occurred. This is the main problem with multiple inheritance


24. Difference between virtual and abstract keyword ?

Virtual methods can be override. there is option either override or not
 Abstract methods must be override. Overriding is compulsory in this case.

25. Can I initiate object of abstract class ? If yes . why ? If no . why ?

we can't initiate object of abstract class. some of its members of abstracts.

26. Difference between private/protected/public/internal/internal protected ?

Private can be accessed only with in a class.
Protected can be accessed with in a class and anywhere from its child classes
Public can be accessed from anywhere.
internal can be accessed within same assembly
internal protected  can be accessed within assembly and anywhere from its child class

27. What is void keyword ?

void is a method keyword which return nothing. this applies to the function from which we don't need anything.

28. Difference between class and structure ?

Class:

  • Class is a reference type
  • class support inheritance
  • we can make default constructor
Structure:
  • Structure is value type
  • can't be inherited
  • can't make default constructors

29. Can we make constructors in structure ?

Yes we can make parameterized constructors in structures but we can't make default constructors. 


30. Can we make default constructor in structure  and why?

No we can't make default constructor because structure can't allow us to do so

31. what are partial class and partial functions ?
32. What is composition, Aggregation and Association ? 

No comments:

Post a Comment

Leave Your response