OOPS

1.      What is namespace?
      Namespace is a logical container of types like class, structure, interface, enum and delegate. We use it for basically two reasons that is
  •  Grouping of related types or items so that we can access them easily.
  •  To overcome the problem of naming collision that is multiple types where the same name which can be defined by placing them under different namespaces.


2.       What is class?
      Class can be defined as a primary building block of oop. It also serves as a template that describes the property, state and behaviors common to a particular group of object.

3.       What is object?
    An object is a basic unit of system and an entity that stores the actual value of a class. Attributes and behavior of an object are defined by the class definition. Objects are the member of a class.

4.      What is the relationship between a class and object?
    A class act as a blue print that defines the properties, states and behaviors that are common to a number of objects. An object is an instance of the class. For example if vehicle is the class than Car, Bus are the objects of that class. You can create any number of objects for the class.

5.       Explain the Features of oops?
The basic features of OOPS are
  • Abstraction:-  It refers to the process of exposing only the relevant and essential data to the users without showing unnecessary information.
  • Polymorphism:-  Allows you to use an entity in multiple forms. Changing the behavior of an entity according to the input that is supply to them is also known as polymorphism that is whenever the input changes automatically the output on behavior also changes.
  • Encapsulation:-  prevents the data from unwanted access by binding of code and data in a single unit called object.
  • Inheritance:- According to this property a child class obtains all the features defined in its parent class. It provides the reusability of code and eliminates the use of redundant code.


6.       What is abstract class and abstract method?
    An abstract class is a class that cannot instantiated and is always used as a base class. An abstract class also should be declared using abstract modifier. Abstract class is never useful to itself because creating an object of abstract class is not possible. Abstract class is always public and you must declare at least one abstract method in the abstract class.
Example:
                abstract class Math
                {
                                public  abstract void Add(int  x,  int  y);
                }
Abstract method is without any method body is known as known as abstract method, which contains only the deceleration of method.

7.       What is interface?
    An interface is a template that contains only the signature of methods. The signature of method contains the number of parameters, the type of parameter and the order of parameters.  An interface has no implementation on its own because it contains only the definition of methods without any method body. An interface is defined using the Interface keyword.

8.       What is method?
    Method is the building blocks of a class in which they are linked together to share and process data to produce the result. In other words a method is a block of code that contains a series of statements and represents the behavior of a class. While declaring a method you need to specify a access specifier,  the return value, the name of the method and the method parameters. All these combined together is called signature of the method.

9.       What is a property?
    A property is a way to expose an internal data element of a class in a simple manner. You can create a property by defining an externally available name and then writing the set and get property accessors. The get property accessor is used to return the property value and the set property accessor is used to assign a new value to the property.

1     What is inheritance and its types.
    Inheritance is the process of establishing parent-child relationship between classes so that the members of one class can be consuming in other classes.
Example:
                class Class1
                {
                                members
                }
                class Class2:Class1
                {
                                consume parents members
                }
Types of Inheritance:-
  • Single Inheritance:- Contains one base class and one derived class.
  • Multiple Inheritance:- Contains several base classes and a derived class.
  • Multilevel Inheritance:- Contains a class derived from a derived class.
  • Hierarchical Inheritance:- Contains one base class and multiple derived classes of the same base class.


1     What is constructor?
    Constructor is a special method of a class which is called automatically when the instance of a class is created. it is created with the same name as the class and initializes all class members whenever you access the class. The main features of a constructor are as follows:
  •  Constructors do not have any return type.
  •  Constructors are always public.
  •  It is not mandatory to declare a constructor it is invoked automatically by .NET framework.


           What is destructor?
    A destructor is a special method for a class and is invoked automatically when an object is finally destroyed. The name of the destructor is also same a s that of the class but is followed by a prefix tilde (~). Destructor is used to free the dynamic allocated memory and release the resources. You can however implement a custom method that allows you to control object destruction by calling the destructor.
Features of Destructor:
  •  Destructors do not have any return type.
  •  Similar to constructors, destructors are also always public.
  •  Destructors cannot be overloaded.


1      Why the virtual keyword is used in the code?
    The virtual keyword is used while defining a class to specify that the method and the properties of that class can be overridden in derived classes.

1     What is delegate?
    A delegate is similar to a class that is used for storing the reference to a method and invoking that method at run time, as required. A delegate can hold the reference of only those methods whose signature is same as that of the delegate. Some of the examples of delegate are type-safe functions, pointers, callbacks.
Example:
                public delegate void AddDel (int  x,  int  y);
                public delegate string SayDel (string name);
                class DelegateDemo
                {
                                public void Add(int x, int y)
                                {
                                                Console.WriteLine(x+y);
                                }
                                public static string SayHello(string name)
                                {
                                                return "Hello" + name;
                                }
                                static void Main()
                                {
                                                DelegateDemo obj = new DelegateDemo();
                                                AddDel ad = new AddDel(obj.Add);
                                                SayDel sd = new Saydel(say Hello);
                                                ad(100,50);
                                                ad(510,400);
                                                ad(450,200);
                                                Console.WriteLine(sd("xxx"));
                                                Console.WriteLine(sd("yyy"));
                                                Console.WriteLine(sd("zzz"));
                                                Console.ReadLine();
                                }
                }

1     Define an event?
    Whenever an action takes place in a class that class provides a notification to other classes or objects that are assigned to perform particular tasks. These notifications are called events. An event can be declared with the help of the event keyword. For example when a button is clicked the class generates an event called click.

1     What is the syntax for inherit from a class in c#?
    When a class is derived from another class then the members of the base class become the member of the derived class. The assess modifier used while accessing members of the base class specifies the access status of the base class members inside the derived class.
Syntax:
                class MyNewClass : MyBaseClass

1     Can you declare a private class in a namespace?
    The classes in a namespace are internal by default. However you can explicitly declare them as public only and not as private, protected or protected internal. The nested classes can be declared as private, protected or protected internal.

1     Define an array?
    An array is defined as a homogeneous collection of elements, stored at contiguous memory location which can be referred by the same variable name. All the elements of an array variable can be accessed by index values. An index value specifies the position of a particular element in an array variable. The index value of the first element is 0 (Zero).

1     What is collection and generics?
    A collection can be defined as a group of related items that can be referred to as a single unit. The System.Collections namespace provides you with many classes and interfaces. Some of them are ArrayList, List, Stack, ICollection, IEnumerable and IDectionary. Generics provide the type safety to your class at the compile time. While creating a data structure you never need to specify the data type at the time of declaration. The System.Collections.Generic namespace contains all the generic collections.

2     What are stack and queues?
    Stack refers to a list in which all items are accessed and processed on the Last-In-First-Out (LIFO) basis. In a stack elements are inserted (push operation) and deleted(pop operation) from the same end called top. Queues refer to a list in which insertion and deletion of an item is done on the First-In-First-Out (FIFO) basis. The items in a queue are inserted from one end called the rear end and are deleted from the other end called front end of the queue.

2     What are structures?
    Structure is a heterogeneous collection of elements referenced by the same name. A structured is declared using the struct keyword. The following is an example that creates a structure to store an employee’s information.
                struct emp
                {
                                fixed int empId[15];
                                fixed char name[30];
                                fixed char addr[50];
                                fixed char dept[10];
                };
    The above example defines a structure emp and the members of the structure specify the information of an employee.

2     Define enumeration?
    Enumeration is defined as a value type that consists of a set of named values. These values are constants and are called enumerators.  An enumeration type is declared using the enum keyword. Each enumerator is an enumeration is associated with an under laying type that is set by default, on the enumerator. The following is an example that creates an enumeration to store different varieties of fruits:
               
                          enum Fruits {Mango, Apple, Orange, Guava};

    In the above example an enumeration Fruits is created where number 0 is associated with Mango, number 1 is with Apple, number 2 is with Orange and number 3 is with Guava. You can access the enumerators of an enumeration by these values.

2    What is hash table?
    Hash table is a data structure that implements the IDictionary interface. It is used to store multiple items and each of these items is associated with a unique string key. Each item can be accessed using the key associated with it. In short hash table is an object holding the key-value pairs.

2     What is the function of Try-Catch-Finally block?
    The try block encloses those statements that can cause exception and the catch block handles the exception, if it occurs. Catch block contains the statements that have to be executed, when an exception occurs. The finally block always executes, irrespective of the fact whether or not an exception has occurred. The finally block is generally used to perform the cleanup process. If any exception occurs in the try block the program control directly transfers to its corresponding catch block and later to the finally block. If no exception occurs inside the try block then the program control transfers directly to the finally block.

No comments:

Post a Comment