Monday, February 23, 2009

C# InterviewQuestions- Part 3

Interview Questions

21. What are the properties of constant members?
a ) 1. They should use the modifier ‘ const’
2. Their value should be given when they are defined (it can’t be changed later)
3. They are implicitly static.

22. Why we use this keyword?
a ) This is used to distinguish local and instant variables that have the same name.

23. Does a copy of a static variable is created every time a class is instantiated?
a ) No.

24. What is a partial class?
a ) A partial class is a class which resides in multiple files. It should use 'partial' keyword. Generally it is used in situations where multiple developers need acess to the same class.

25. What is the name of the class from which all .net classes are derived?
a ) System.Object

26. Does c# support global variables?
a ) No. all declarations must be done inside a class.

27. Differences between property and indexer?
a ) Property
A property can be static member
The get accessor of a property corresponds to a method with no parameters
An indexer is always an instant member
The get accessor of an indexer corresponds to the same formal parameter list as the indexer.

28. Differences between overloading and overriding?
a ) Overloading
1. used we want a method with more than one definition with in the same scope
2. in overloading, methods will have same name but different number, types, order of arguments.
1. this is used when we have parent, child classes.
2. in overriding , methods will have same name with same arguments and same return type.

29. what is the order of the constructor execution in inheritance?
a ) They are executed from top(parent class) to bottom(child class)

30. what is the order of the destructors execution in inheritance?
a ) They are executed from bottom(child class) to top(parent/base class)

Monday, February 16, 2009

C# InterviewQuestions- Part 2

Interview Questions

11. Can we use access modifier on static constructor?
a ) No

12. How many static constructors we can declare for a single class?
a ) A class can have only one static constructor.

13. What is method overloading?
a ) It is a process of creating methods that have the same name but with different parameter lists and different definitions. This method overloading is used when you want your methods to do same tasks but using different input parameters.

14. What happens when we use ‘private’ modifier to a constructor of a class?
a ) When we declare a constructor of a class as private then we can’t create a objects of that class and we can not use that class as a base class for inheritance

15. Can we overload constructors?
a ) Yes

16. What are instant variables?
a ) Class variables are known as instant variables. Instant variables are different for each object and they are accessed using the objects. When ever a class is instantiated a new copy of the each of the instant variable is created.

17. What are static variables?
a ) These are also known as class variables. Static variables are common for all objects of a class. Only one copy of static variables will be created for all the objects of a class.

18. What are the differences between structure and class
a ) Structure:
It is value type
It is stored on stack
Does not support inheritance
Suitable for small data structure
It is reference type
It is stored on heap
Supports inheritance
Suitable for complex data structures

19. What is a value type? Give examples
a ) It stores value directly. Value types are stored on stack. Separate memory will be given for each instant of a value type.
Ex: int, float, char, struct

20. What are reference type?
a ) It stores a reference to the value. They are stored on heap.
Ex: class, string, array.

Tuesday, February 10, 2009

C# InterviewQuestions- Part 1

Interview Questions

1. What are the main properties of object oriented programming?
a ) There are mainly 3 properties
1. Encapsulation/Abstraction
2. Inheritance
3. Polymorphism

2. What is encapsulation?
a ) This gives a way to hide the internal details of an object from its users.

3. What is inheritance ?
a ) It provides a way to build/create new classes using existing classes.

4. What is polymorphism?
a ) It provides a way to take more than one form.

5. What is a constructor?
a ) Constructor enables an object to initialize itself when it is first created.

6. What are the properties of a constructor?
a ) They are
1. They should have the same name as that of a class
2. They do not specify a return type.
3. we can use access modifiers to constructor.

7. How many types of access modifiers are there ?
a ) 5 types
1. Private
2. Public
3. Protected
4. Internal
5. Protected Internal

8. What access modifiers we can use for a class?
a ) We can use 2 access modifiers
1. Public
2. Internal

9. What is the default access modifier of a class?
a ) It is Internal

10. What is the default access modifier for class members?
a ) It is Private

Tuesday, February 3, 2009


Filling dataset with data from more than one table
To Fill dataset with data from more than one table from database and to
bind that data to datagrid we need following code

1. In .aspx file you need to write

<asp:datagrid id="grid1" runat="server" autogeneratecolumns="True">
<asp:datagrid id="grid2" runat="server" autogeneratecolumns="True">

Here we are specifying 2 datagrids to display data from 2 Tables.

2. In .aspx.cs file

1. First we need to open a connection

SqlConnection newconn = new SqlConnection(“Server=xxx;
InitialCatalog=xxx; UserId=xx;Password=xxx”);;

In the above statement server= xxx means we are specifying server
name.InitialCatalog means database name from which we want to
retrive data. Userid, password are userid and passwords of user
for sql server.

2. Next we need to specify the sql statements(required operation)

DataAdapter da = new SqlDataAdapter("select * from firsttablensme;
Select * from secondtablename”, newconn)

note: here instead of command object we are directly using
DataAdapter.If you want you can use command object also.

In the above statement we are selecting data from two tables.
Newconn is the connection object.

3. Now, we need to fill the dataset

ds = new DataSet();
da.Fill(ds, “firsttablename”);
da.Fill(ds, “secondtablename”);

Here we are using Fill() method of dataadapter to fill the Dataset.

4. Now to bind this data to datagrid we need following code

grid1.DataSource = ds;
grid1.DataSource = ds.Tables[0];

grid2.DataSource = ds.Tables[1];

Here DataSource property represents the datasource of the datagrid and
DataBind() method used to bind data to the datagrid.

Note: you can also use caption property of datagrid to specify the table caption
Ex: grid1.caption = “xxx”
here Xxx represents cation for the table displayed by datagrid.

That’s it.