Friday, 5 July 2013

DOTNET CSHARP OOP QUESTIOSN AND ANSWER - K MANOHAR GOUD

9866550012

Explain the use of abstraction with an example using C#.NET.

Abstraction is used to create a common set of methods that might have different specific implementations by subclasses. Abstract class cannot be instantiated and consists of abstract methods without any implementations. Classes inheriting from abstract class must implement all the methods in abstract class.


Public abstract class Shape
{
     Private float _area;
     Public Float Area
     {
          Get{return _area;}
          Set{_area=value;}
     }
     Public abstract void CalculateArea();
 }  
     Class Rect:Shape
     {
           Private float _height;
           Private float _width;
           Public Rect(float height, float width)
           {
                   _height = height;
                   _width = width;
           }
           Public Float Height
           {
                 Get{return _height}
                 Set{_height=value;}
           }
           Public Float Width
           {
                  Get{return _width}
                  Set{_width=value;}
           }
           Public override void CalculateArea()
           {
                  This.Area=_height*_width;
      }
}

Explain the use of encapsulation.

Encapsulation hides the internal state and behavior of an object. Encapsulation if used with access modifiers such as private, public, protected. It provides a way to protect data.

public class MyClass
{
       private string name;
       public string Name
       {
            get
            {
                 return name;
            }
            set
            {
                   name = value;
            }
      }
}
public class main
{
        public static int Main(string[] args)
        {
               MyClass myclass = new MyClass();
               myclass.name = "Communication";
               Console.WriteLine("The name is :{0}", myclass.Name);
               return 0;
       }
}

See use of encapsulation through properties for accessing and setting the values.


Differentiate between instance data and class data

Class data in terms of static class is the data that particular class holds in its structure. Instances can refer to different objects of the same class that hold different values using the same class structure in memory heap.


Explain the significance of static method

Static methods are used when we want only one copy of that method to perform action and remain active at a single point in time.
Imagine a scenario where you need a class to connect to the database once and remain as it is for the entire application.
public static class MyDbConnection
{
     public static void ConnectToDb()
     {
           //code to connect to database
     }
}
public void SomeMethod()
{
      MyDbConnection.ConnectToDb();
}

You don’t want different instances of this class to be created and connect to the database again and again, hence make a static method in a static class.

Explain the uses of boxing and unboxing.

Boxing and Unboxing are used to convert value types into reference types and vice versa. Developers often need to make some methods generic and hence create methods that accept objects rather than specific value types. The advantage of reference types is that they don’t create a copy of the object in memory, instead pass the reference of the data in the memory. This uses memory much more efficiently, especially, if the object to be passed is very heavy.

public class MyClass
{
      public void MyClass()
      {
      }
      public void MyMethod()
      {
             int intVar1 = 1; // i is an integer. It is a value type variable.
             object objectVar = intVar1;
             // boxing occurs. The integer type is parsed to object type
            int intVar2 = (int)objectVar;
            // unboxing. The object type is unboxed to the value type
      }
}


Explain how to use the finally statement by providing sample code using C#.NET.

Finally statement is executed always irrespective of any condition or error occurring in a method. It is often used to cleanup code.

E.g.:
public void MyMethod()
{
      try
      {
              //code to connect to database
              //code to perform action
       }
       catch (Exception ex)
        {
             //handle exception
        }
        finally
        {
                    //code to disconnect database.
        }
}


Explain steps to prepare culture-specific formatting.

The NumberFormatInfo class is used to define how symbols, currencies etc are formatted based on specific cultures.

E.g.:
using System;
using System.Globalization;
public class TestClass
{
       public static void Main()
       {
             int i = 100;
             // Creates a CultureInfo for English in the U.S.
             CultureInfo us = new CultureInfo("en-US");
             // Display i formatted as currency for us.
             Console.WriteLine(i.ToString("c", us));

             // Creates a CultureInfo for French.
             CultureInfo fr = new CultureInfo("fr-FR");
             // Displays i formatted as currency for france.
             Console.WriteLine(i.ToString("c", fr));
       }
}




No comments:

Post a Comment