Pages

General Puzzle1

This is very simple code which every one might have faced but you didn't have noted this. Is that possible to create a static void main with int as return type like code shown below?


class Program
    {
        static int Main(string[] args)
        {
            return 0;
        }
    }
And is it possible to pass int args instead of string args like this?


class Program
    {
        static void Main(int[] args)
        {
        }
    }

Please dont Execute code.....

Oops Puzzle1

Object Oriented Programming seems to be tricky usually. This Puzzle is based on Inheritance.

Consider there are 2 Classes
Class A and Class B

class Program
    {
        
        static void Main(string[] args)
        {
            ClassA _objClassA = new ClassB();
            _objClassA.Function1(); 
        }
    }

public Class ClassA
{
    public void Function1()
    {
         Console.WriteLine("ClassA...Function1()");
    }
}
public class ClassB:ClassA
{
    public void Function1()
    {
         Console.WriteLine("ClassB...Function1()");
    }
}


What is the Result of the above Program.
Please do not execute this program. Try to solve by looking code.

SQL Puzzle1


Very Recently i faced one Problem, struggled a lot to Find Solution, but didn't find that solution yet. So trying to share that Problem with you all.
Consider there are two tables. Table 1 and Table 2.

Table 1 Contain Following Details


Question_IdQuestion
1Question1
2Question2
3Question3
4Question4

Table 2 Contain Following Details

Question_IdOption_IdOptions
111Q1Option1
112Q1Option2
113Q1Option3
114Q1Option4
221Q2Option1
222Q2Option2
223Q2Option3
224Q2Option4
331Q3Option1
332Q3Option2
333Q3Option3
334Q3Option4
335Q3Option5
336Q3Option6
441Q4Option1
442Q4Option2
443Q4Option3
444Q4Option4
445Q4Option5

Now we have to write a Query that should return a Table in a Below Stated format.

IDQuestionOption
1Question1
11Q1Option1
12Q1Option2
13Q1Option3
14Q1Option4
2Question2
21Q2Option1
22Q2Option2
23Q2Option3
24Q2Option4
3Question3
31Q3Option1
32Q3Option2
33Q3Option3
34Q3Option4
35Q3Option5
36Q3Option6
4Question4
41Q4Option1
42Q4Option2
43Q4Option3
44Q4Option4
45Q4Option5

Condition:
Never use UNION and LOOP


Thank you

SQL SELECT Puzzle

This is one of the good puzzle i heard from my Interview.
When i went for an Interview, interviewer asked me one good Puzzle in SQL , Quite Simple but really very Good Puzzle. 
Question:
    Consider a Local Variable,
DECLARE @Name Varchar(50)=NULL
SELECT FirstName+ ' '+LastName FROM Employee WHERE FirstName=@Name
Question is very simple,
When value is passed through that variable then it should execute complete SELECT statement with WHERE Clause, if value is not passed then SELECT  statement should execute without WHERE Clause.
Eg:
 Consider there are 5 Records in that Employee Table

ID FirstName LastName EmailId ContactNo
1 Anand Kumar anand1@gmail.com 9898989898
2 Anderson anderson@gmail.com 9898989898
3 Arnold schwarzenegger arnold@yahoo.com 9898989898
4 David Blaine david@gmail.com 9898989898
5 Ranbir Kapoor Ranbir@gmail.com 9898989898

If we pass @Name='Anand' then it should return First Record with FirstName and LastName.
If we are not passing any value then obviously @Name is Null as it is declared as NULL, so it should return all records with FirstName and LastName.

Condition
         You should never use two set of Select Query (Some people will say we can use IF Statement and can write two set of  Queries, one with WHERE Clause and one without WHERE Clause-never do this).
Thank you

Executing one ASP.NET Page from Another

Very recently i faced one problem where i want to execute one ASP.NET Page from Another Page just by calling inside some Function. Let we take this Scenario.

I have changed my code to dummy code as i cant write my real code here due to Copyright Violation. First i will discuss about that Problem.

Consider there are two ASP.NET Pages, Page1.aspx and Page2.aspx

Page1.aspx :
protected void Page_Load(object sender,EventArgs e)

{

Function1();

Function2();

Function3();

}

protected void Function1()

{

Response.Write("Function1");

}

protected void Function2()

{

Response.Write("Function2");

//Call Page2.aspx

}

protected void Function3()

{

Response.Write("Function3");

}
Page2.aspx:
protected void Page_Load(object sender,EventArgs e)

{

Response.Write("Page2");

}
Now we need result like this,

Output:
Function1

Function2

Page2

Function3
How will you do this?

Beware of Implicit Datatype Conversion in SQL Server

Do you know SQL Server does Implicit Conversion in Queries? If you know that then you are safe. If you don't then please don't miss reading this Complete Article.

Now we'll create a Test Table and Insert Some Values, to understand this concept more clearly.


CREATE TABLE [dbo].[Test3]( [N1] [int] NOT NULL CONSTRAINT [PK_Test3] PRIMARY KEY,
[V1] [nvarchar](10) NULL, )
INSERT INTO Test3 VALUES(1,'1000')
INSERT INTO Test3 VALUES (2,'1001')
INSERT INTO Test3 VALUES (3,'1003')
INSERT INTO Test3 VALUES (4,'1004')
INSERT INTO Test3 VALUES (5,'200')
If we try to do
SELECT * FROM Test3 WHERE V1>1002
Will it Work? It will work. This is where Implicit Conversion is Happening. Whenever SQL Server finds Arithmetic Operator it will convert string to Number if it can do that. This Facility is not Provided in DB2. It will throw exception. But thats a good advantage in DB2. So that people will never get in trap.

Output for Above Query will be
N1 V1
3 1003
4 1004
5 200