Tuesday 29 October 2013

Interview FAQ on Interface in C#

What is Interface?

 An interface is collection of methods, properties, indexers and events with only signatures. Means In interface Methods, properties, indexers and events do not have their definition. So an Interface allows you to define behavioral characteristics and apply those behaviours to Class that implements this Interface. Example
public interface IHoliDay
    {

        List<DateTime> GetListOfHoliday();

    }
Or

interface IHoliDay
    {
          List<DateTime> GetListOfHoliday();

    }

If you use public List<DateTime> GetListOfHoliday() an error occurred.

Error  1      The modifier 'public' is not valid for this item       .

So there is no need of modifiers for Interface members. Interface members are automatically public and cannot be static.

Why Interface is came in existence?

 Interface is mainly used for two reasons
·         C# does not support multiple inheritances, so by using Interface, A class can implement multiple interfaces to achieve multiple inheritances.
public class HoliDay : Class1, Interface1,Interface2,Interface3
    {

    }

·         Data Hiding
You can call class members using Interface.
class Program
    {
        static void Main(string[] args)
        {
            //Hiding the Holiday class object
            IHoliday ii = new Holiday();
            ii.GetHoliDayList();
            Console.WriteLine(ii.val);
        }

    }

    public interface IHoliday
    {
        string val{get; set;}
        List<DateTime> GetHoliDayList();
       
    }

    public class Holiday : IHoliday
    {
        public String val{ get; set;}

        List<DateTime> hlist = new List<DateTime>();
        public  List<DateTime> GetHoliDayList()
        {
            //code
            val = "Class Members";
            Console.WriteLine("Calling by interface");
            return hlist;

        }
    }


Interface also used to make your code more reusable, maintainable, and scalable and provides the concept of component based programming.

Suppose you have two interfaces with method with same name. How to implement both in same class? 

User Intafacename.MethodName()
{
}
Example:
class Program
    {
        static void Main(string[] args)
        {
            //Hiding the Holiday class object
            IOfficeHoliday iioffice = new Holiday();
            iioffice.GetHoliDayList();
            IGuzzatedHoliday iiGuzzated = new Holiday();
            iiGuzzated.GetHoliDayList();
            //Console.WriteLine(ii.val);
        }

    }

    //first interface
    public interface IOfficeHoliday
    {
     
        List<DateTime> GetHoliDayList();
       
    }

    //second interface
    public interface IGuzzatedHoliday
    {
      
        List<DateTime> GetHoliDayList();

    }

    public class Holiday : IOfficeHoliday, IGuzzatedHoliday
    {
      

        List<DateTime> hlist = new List<DateTime>();

        //Method definintion for first interface
         List<DateTime> IGuzzatedHoliday.GetHoliDayList()
        {
            //code

            Console.WriteLine("IGuzzatedHoliday Method");
            return hlist;

        }

        //Method definintion for second interface
         List<DateTime> IOfficeHoliday.GetHoliDayList()
        {
            //code

            Console.WriteLine("IOfficeHoliday Method");
            return hlist;

        }
    }

//here if you use public with method definition in class that implements both of interfaces, an error occurred.

What is the use of is and as operator with respect to Interface concept?

 The is and as operators can be used to determine whether an interface is implemented by an object.
Holiday hh = new Holiday();
            IHoliday iholiday = hh as IHoliday;

            if (hh is IHoliday)
            {
                Console.WriteLine("Holiday class implemented IHoilday");
            }



Comments if you have other possible interview FAQ for Interface in C#.

Thursday 24 October 2013

SQL Server Inetrview FAQ 3

1.       How to find the maximum no of connection allowed in SQL Server?

 Select @@MAX_Connections

2.       How to select top 2 rows without using top?

 It can be achieved by using rowcount. For example
SET ROWCOUNT 2
SELECT *from tblName

3.       What is the purpose of SET ANSI NULLS ON?

SET ANSI NULLS ON is used to follow ANSI standerds.So if you are working with distibuted queries running across multiple server,You need to SET ANSI NULLS ON,to maintain compatibility for all servers.
For example: We should not use <> or != for checking NULL condition,It should be is NULL or is NOT NULL as per ANSI standerds.

4.       How to insert Multiple Rows in single query?

We can use Row Constructor as an example
INSERT INTO TABLENAME(COL1,COL2,COL3)
VALUES
('VAL1','VAL2','VAL3'),
('VAL11','VAL22','VAL33'),
('VAL111','VAL222','VAL333')

5.       Which type of column we can’t update using UPDATE?

TIMESTAMP type of column can’t be updated.

6.       How can you apply restrictions on database objects?

We can create constraints, triggers or rules and defaults to apply restrictions. Constraints are better than triggers and rules. Triggers and rules should only be used if constraints are not an option because triggers make overhead on system.

7.       CAST vs. Convert

Convert does everything that CAST does. The only difference is that CAST is ANSI/ISO compliant while CONVERT is not.

8.       What is the default port no of SQL server

SQL Server listen TCP port 1433 by default.

9.       What are DMVs?

DMV: Dynamic Management Views are used to monitor server state information as health of server instance, performance, connections.
For example:
SELECT * FROM sys.dm_os_wait_stats;
It will return operating system wait states.
SELECT * FROM sys.dm_exec_sessions;
It will return cureent sessions. Some other DMVs are
·         dm_broker_connections
·         dm_broker_forwarded_messages
·         dm_broker_queue_monitors
·         dm_cdc_errors
·         dm_cdc_log_scan_sessions
·         dm_clr_appdomains
·         dm_clr_loaded_assemblies
·         dm_clr_properties
·         dm_clr_tasks

10.   OLTP vs OLAP

OLTP: Online Transaction Processing (It is used for usual applications).Most of applications are OLTP based. It emphasizes on Update.

OLAP: (Online Analytic Processing)It is used for multidimensional queries and better approach for MIS and decision making systems. In Business Intelligence OLAP used. It emphasizes on Retrieval.

Saturday 19 October 2013

Cumulative SUM in SQL Server


Sometimes we need to find sum of first and next row in cumulative way.

Create table and insert data:

CREATE TABLE [dbo].[testsum](
      [name] [varchar](10) NULL,
      [val] [int] NULL,
      [ID] [int] NULL
) ON [PRIMARY]

insert into [testsum] (id,name,val)
values(1,'A',10),
(2,'B',20),
(3,'C',30)

Required Output:
ID    name  val   cumSum
1     A     10    10
2     B     20    30
3     C     30    60

To find cumulative sum first you need to self join on condition >=

select t1.*,t2.* from testsum t1 inner join testsum t2 on t1.ID>=t2.ID

output after join.
t1
t2
ID
name
val
ID
name
val
1
A
10
1
A
10
2
B
20
1
A
10
3
C
30
1
A
10
2
B
20
2
B
20
3
C
30
2
B
20
3
C
30
3
C
30

Group by ID and SUM.

select t1.id, t1.val, SUM(t2.val) as cumSum
from testsum t1
inner join testsum t2 on t1.id >= t2.id
group by t1.id, t1.val
order by t1.id

t1
t2
ID
name
val
ID
name
val
1
A
10
1
A
10
2
B
20
1
A
10
3
C
30
1
A
10
2
B
20
2
B
20
3
C
30
2
B
20
3
C
30
3
C
30

We reach to output:

id
val
cumSum
1
10
10
2
20
30
3
30
60