Friday 6 January 2017

Oops concept questions Part-1

Ques: What is object in c# ?
An object is an instance of a class. The new operator is used to create an object of a class. When an object of a class is instantiated, the system allocates memory for every data member of that class.

Ques: What is class ?
Class is a template or blueprint that is used for creating an object.
Class is reference type.

Ques: What are the different type of class in c# ?
There are four types of classes in C# .Net
1. Abstract class
2. Sealed class
3. Static class
4. Partial class

1) Abstract class - An Abstract Class means that, we can not create instance of this class, but can create instance of derivation of this class.Abstract class always act as base class. for creating Abstract Class the abstract keyword use.

2) Sealed class - A sealed class is a class which cannot be inherited. A sealed class cannot be a base class. The modifier abstract cannot be applied to a sealed class. By default, struct (structure) is sealed.Sealed Class is denoted by the keyword sealed.

3) Static class - A Static Class is one which cannot be instantiated. The keyword new cannot be used with static classes.

4) Partial class - Partial Class allows its members – method, properties, and events – to be divided into multiple source files. At compile time these files get combined into a single class.

Ques: What are the main OOP’s concepts ?
Abstraction, Encapsulation, Inheritance and Polymorphism are the main OOP’s concepts.

Ques: What is Encapsulation ?
Encapsulation is way to hide data, properties and methods from outside the class scope.
for Encapsulation private modifier keyword usually used.

Ques: How we can implement Encapsulation in class ?
by using "private" access access modifier
ex:
public Class Car
          {
                private enginstype="";
                private void CalculateSpeed(){
                Console.WriteLine("speed of car 300 m/h");
          }
}

Ques: What is Abstraction ?
Abstraction is a mechanism which represent the essential features without including implementation details.Abstraction Showing Whats Necessary to client

Ques: What is Polymorphism ?
Polymorphism means having more than one form.it is one of the most important future of object-oriented programming
                                                   or
Polymorphism is the ability of objects of different types to provide interface for implementations of methods.

Ques: What are different types of polymorphism in C#
There are two type of polymorphism
1. Compile time Polymorphism or Early Binding (Method Overloading)
2. Runtime Polymorphism or Late Binding (Method Overriding)


Ques: What is Inheritance ?
 Inheritance provide you to create new classes (child class) that reuse, extend, and modify the behavior that is defined in other classes (Parent class).

Ques: What is Constructor ?
A constructor is an instance method which is the same name as the class.
                                                   or
Constructor is a method which executed automatically when we instant object of that class.
it used to initialize the filed of class before using of  an object.
  • A class can have many number of constructors.
  • A constructor doesn't have any return type.
  • A constructor can be called another constructor by using "this" keyword.

Ques: What are type of Constructor ?
Default Constructor: In default constructor does not have any input parameter.
Parametrized Constructor:In parameterized constructor having one or more parameters.
Static Constructor: It invoked only during the first initialization of instance. It is used to initialize static fields of the class
Private Constructor:A class with private constructor cannot be inherited.
Copy Constructor:A constructor that contains a parameter of same class type is called as copy constructor.

Ques: Define Destructor ?
Answer:
Destructor is a method which is automatically called when the object is destroyed.
Destructor name is also same as class name but with the tilde symbol before the name.


Ques: What is the difference between a class and a structure ?
Answer:
Class:
A class is a reference type.
While instantiating a class, its instance allocated on heap memory .
Classes support inheritance.
Variables of a class can be assigned as null.
Class object cannot be created without using the new keyword.
Class  can be abstract.
Classes are usually used for large amounts of data.

Structure:
A structure is a value type.
In structure, memory is allocated on stack.
Structures does not support inheritance.
Structure members cannot have null values.
Structure object can be created without using the new keyword.
A structure can't be abstract.
structure are usually used for smaller amounts of data.


Ques: What is namespace ?
A namespace provides a fundamental unit of logical code grouping.

Ques: What is Sealed class ?
Answer: A sealed class is a class that does not allow inheritance.
Some object model designs need to allow the creation of new instances but not inheritance, if this is the case, the class should be declared as sealed.

1 comment:

Thank you for comment