Dot Net Interview question by Shivprasad Koirala
Dot Net Interview question by Shivprasad Koirala
| Introduction |
| .NET is fully object oriented platform that allow languages to take full advantage of these OO features. The features include: |
| Namespace |
| Classes |
| Abstract |
| Encapsulation |
| Overloading |
| New. |
| Overriding |
| Interfaces |
| Polymorphism |
| Let’s take a quick look at what each of this term means. |
| Namespace |
| Even though namespace is not really an OOPs feature, .NET use it extensively. Namespace is nothing but a logical container for various classes. Under given namespace class names must be unique. Namespace server two purposes – they provide logical organization of classes and they also avoid ambiguity. |
| Classes |
| Class is nothing but a template or blue-print for an entity. For example you may have a class that represents real life entity – Employee. The class will provide properties (Name, Age…) as well as actions (CalculateSalary, GoOnLeave…) of the entity. |
| Objects |
| Instances of classes are called as objects. For example there might be three instances of the Employee class mentioned above. They might represent individual employees – John, Bob and Tom. |
| Encapsulation |
| Each object typically deals with some kind of data or the other. Not all the data needs to be exposed to external systems. This can be controlled via data encapsulation. |
| Overloading |
| Overloading refers to the methods or functions having same name but varying parameters. The parameters should vary with respect to data types and order. |
| If no modifier is specified, the method is given private access. |
| Inheritance |
| Inheritance refers to extending functionality of existing class. Inheritance is useful when developing "object models" for your system. .NET supports only single inheritance. |
| Overriding |
| Overriding refers to the methods in the child class having the same signature (name as well as parameters) as of the parent class methods. |
| Interfaces |
| Interfaces are nothing but models of class properties and methods without any implementation. The class implements the interface. Once a class implements any interface it must implement all the properties and methods (although the implementation can be empty or null implementation). |
| Polymorphism |
| Polymorphism refers to the ability of the system to call correct implementation of methods with the same name. For example, Clerk as well as Manager class might have a method called CalculateSalary(). However, at runtime depending on whether the underlying object is of type Clerk or Manager correct version of the method is called. |
| Creating namespaces |
| Namespaces are created using keyword – Namespace (namespace in C#). Following example shows how to declare a namespace. |
| [VB.NET] |
| Namespace MyNamespace |
| … |
| End Namespace |
| [C#] |
| namespace MyNamespace |
| { |
| … |
| } |
| Note that one assembly can have one or more namespaces. Also, one namespace can span across multiple assemblies. You can create nested namespaces as follows: |
| [VB.NET] |
| Namespace MyNamespace |
| Namespace MuSubNamespace |
| … |
| End Namespace |
| End Namespace |
| [C#] |
| namespace MyNamespace |
| { |
| namespace MySubNamespace |
| { |
| … |
| } |
| } |
| If you are using VS.NET then the project name acts as the default namespace name. |
| Creating classes |
| Creating classes is similar to creating namespaces. |
| [VB.NET] |
| Public Class Class1 |
| … |
| End Class |
| [C#] |
| public class Class1 |
| { |
| … |
| } |
| Generally classes will be part of some of the namespace. |
| Creating Properties |
| Properties encapsulate data members of your class. Properties can be read-write, read only or write only. Here is how you create read-write properties: |
| [VB.NET] |
| Public Class Employee |
| private strName As String |
| Public Property Name As String |
| Get |
| return strName; |
| End Get |
| Set(value As String) |
| strName=value; |
| End Set |
| End Property |
| End Class |
| [C#] |
| public class Class1 |
| { |
| public string Name |
| { |
| string strName; |
| get |
| { |
| return strName; |
| } |
| set |
| { |
| strName=value; |
| } |
| } |
| } |
| Here, |
| VB.NET uses Property keyword to declare properties. C# does not have this keyword |
| Property definition consists of two parts Get and Set. The get part returns the property value and set par sets some private variable. |
| The value in Set routine is received via implicit variable called value in C#. VB.NET allows you to change this. |
| Creating methods |
| Methods represent actions performed by the object. In VB.NET functions and sub routines are collectively called as methods. In C# everything is function. |
| [VB.NET] |
| Public Sub CalculateSalary() |
| … |
| End Sub |
| [C#] |
| public void CalculateSalary() |
| { |
| … |
| } |
| Method overloading |
| Method overloading refers to methods with same name but different types or order of parameters. Following example make it clear: |
| [VB.NET] |
| Public Sub CalculateSalary() |
| … |
| End Sub |
| Public Sub CalculateSalary(month as Integer) |
| … |
| End Sub |
| [C#] |
| public void CalculateSalary() |
| { |
| … |
| } |
| public void CalculateSalary(int month) |
| { |
| … |
| } |
| In VB.NET you can also use optional parameters to achieve similar functionality. However, it is recommended to use overloading instead to make your code consistent across languages. |
| Inheritance |
| Inheritance is the ability to extend existing class further. Unlike languages like C++ that allow multiple inheritance .NET allows only single inheritance. This means that at a time you can inherit from a single class. |
| [VB.NET] |
| Public Class Manager |
| Inherits Employee |
| … |
| End Class |
| [C#] |
| public class Manager : Employee |
| { |
| … |
| } |
| In the above example, we create a class called Manager that inherits from Employee class. As you can guess Manager is specific implementation of generic Employee class. VB.NET uses Inherits keyword to indicate the parent class where as C# uses : operator to indicate that. |
| Method Overriding |
| In order to override method in child class they need to be marked as Overridable (VB.NET) or virtual (C#) in the parent class. |
| [VB.NET] |
| Public Overridable Function CalculateSalary() As Integer |
| … |
| End Function |
| [C#] |
| public virtual int CalculateSalary() |
| { |
| … |
| } |
| Then in the child class you can create a method with the same signature and specify that it overrides the base class method. |
| [VB.NET] |
| Public Overrides Function CalculateSalary() As Integer |
| … |
| End Function |
| [C#] |
| public override int CalculateSalary() |
| { |
| … |
| } |
| Note that if you do not provide the Overrides (VB.NET) or override (C#) keywords in the child class the compiler issues a warning that you are hiding a base class member. In this case you can either put the above keywords or use Shadows (VB.NET) or new (C#) keywords. Using these keywords ,however, will hide the base class members. |
| Creating Interfaces |
| Just like classes are templates for real life entities, interfaces can be thought of as templates for classes. They bring uniformity in your object model. |
| [VB.NET] |
| Public Interface IEmployee |
| Property EmployeeID() As Integer |
| Property Name() As String |
| Property Age() As Integer |
| Function CalculateSalary() As Integer |
| End Interface |
| [C#] |
| public interface IEmployee |
| { |
| int EmployeeID |
| { |
| get; |
| } |
| string Name |
| { |
| get; |
| set; |
| } |
| int Age |
| { |
| get; |
| set; |
| } |
| int CalculateSalary(); |
| } |
| As you can see VB.NET uses Interface keyword to define an interface. Similarly, C# uses interface keyword. Note, how they contain only property and method signatures and no code at all. |
| Implementing Interfaces |
| The main difference between inheritance based programming and interfaces based programming is that – interfaces just specify signatures of properties and methods for a class. Your class "implements" the interface by providing implementation for various properties and methods. Unlike inheritance there is no "code" inherited from interfaces. Your class can implement one or more interfaces. |
| [VB.NET] |
| Public Class Manager |
| Implements IEmployee |
| … |
| Public Function CalculateSalary() As Integer Implements IEmployee.CalculateSalary |
| … |
| End Function |
| End Class |
| [C#] |
| public class Manager : IEmployee |
| { |
| … |
| public int CalculateSalary() |
| { |
| … |
| } |
| } |
| Above example shows how VB.NET uses Implements keyword to implement an interface. Note how VB.NET also requires the use of Implements keyword for each property and method. You must have guessed from this that in VB.NET you can give different name to the implemented member than the interface. This feature is not available in C#. C# do not have a special keyword and uses the same : operator to implement the interface. |
| Polymorphism |
| Consider following lines of code: |
| [VB.NET] |
| Dim emp As Employee |
| emp = New Clerk() |
| Console.WriteLine ("Clerk Salary :{0}", emp.CalculateSalary()) |
| emp = New Manager() |
| Console.WriteLine ("Manager Salary :{0}", emp.CalculateSalary()) |
| [C#] |
| Employee emp; |
| emp=new Clerk(); |
| Console.WriteLine ("Clerk Salary :{0}",emp.CalculateSalary()); |
| emp=new Manager(); |
| Console.WriteLine ("Manager Salary :{0}",emp.CalculateSalary()); |
| Here, we have declared a variable of type Employee. A variable of parent class type can point to instance of any of its children. First, we point it to an instance of Clerk class. Then we point it to an instance of Manager class. Even though the variable is of type Employee, depending on which child type it is pointing to it calls the correct implementation of CalculateSalary() method. The underlying system does this via inheritance polymorphism. Similar thing can also be achieved in interface polymorphism. |
| [VB.NET] |
| Dim emp As IEmployee |
| emp = New Clerk() |
| Console.WriteLine ("Clerk Salary :{0}", emp.CalculateSalary()) |
| emp = New Manager() |
| Console.WriteLine ("Manager Salary :{0}", emp.CalculateSalary()) |
| [C#] |
| IEmployee emp; |
| emp=new Clerk(); |
| Console.WriteLine ("Clerk Salary :{0}",emp.CalculateSalary()); |
| emp=new Manager(); |
| Console.WriteLine ("Manager Salary :{0}",emp.CalculateSalary()); |
SELECT id, first_name, last_name, age, subject FROM student_details;
SELECT * FROM student_details; SELECT subject, count(subject)
FROM student_details
WHERE
subject != 'Science'
AND subject != 'Maths'
GROUP BY subject; SELECT subject, count(subject)
FROM student_details
GROUP BY
subject
HAVING subject!= 'Vancouver' AND subject!= 'Toronto'; SELECT name
FROM employee
WHERE (salary, age ) = (SELECT MAX
(salary), MAX (age)
FROM employee_details)
AND dept = 'Electronics';
SELECT name
FROM employee
WHERE salary = (SELECT MAX(salary)
FROM employee_details)
AND age = (SELECT MAX(age) FROM employee_details)
AND emp_dept = 'Electronics'; Select * from product p
where EXISTS (select * from order_items o
where o.product_id = p.product_id) Select * from product p
where product_id IN
(select product_id
from order_items SELECT d.dept_id, d.dept
FROM dept d
WHERE EXISTS ( SELECT 'X'
FROM employee e WHERE e.dept = d.dept); SELECT DISTINCT d.dept_id, d.dept
FROM dept d,employee e
WHERE
e.dept = e.dept; SELECT id, first_name
FROM student_details_class10
UNION ALL
SELECT id, first_name
FROM sports_team; SELECT id, first_name, subject
FROM student_details_class10
UNION
SELECT id, first_name
FROM sports_team; SELECT id, first_name, age FROM student_details WHERE age > 10;
SELECT id, first_name, age FROM student_details WHERE age != 10;
SELECT id, first_name, age
FROM student_details
WHERE
first_name LIKE 'Chan%';SELECT id, first_name, age
FROM student_details
WHERE
SUBSTR(first_name,1,3) = 'Cha';SELECT id, first_name, age
FROM student_details
WHERE
first_name LIKE NVL ( :name, '%');SELECT id, first_name, age
FROM student_details
WHERE
first_name = NVL ( :name, first_name);SELECT product_id, product_name
FROM product
WHERE unit_price
BETWEEN MAX(unit_price) and MIN(unit_price) SELECT product_id, product_name
FROM product
WHERE unit_price
>= MAX(unit_price)
and unit_price <= MIN(unit_price) SELECT id, name, salary
FROM employee
WHERE dept =
'Electronics'
AND location = 'Bangalore'; SELECT id, name, salary
FROM employee
WHERE dept || location=
'ElectronicsBangalore'; SELECT id, name, salary
FROM employee
WHERE salary < 25000;
SELECT id, name, salary
FROM employee
WHERE salary + 10000 <
35000; SELECT id, first_name, age
FROM student_details
WHERE age >
10; SELECT id, first_name, age
FROM student_details
WHERE age NOT =
10; SELECT id FROM employee
WHERE name LIKE 'Ramesh%'
and location
= 'Bangalore'; SELECT DECODE(location,'Bangalore',id,NULL) id FROM employee
WHERE
name LIKE 'Ramesh%'; |
Page Event |
|
|---|---|
|
PreInit
|
Raised after the start stage is complete and before the initialization stage begins. Use this event for the following:
|
|
Init
|
Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page. Use this event to read or initialize control properties. |
|
InitComplete
|
Raised at the end of the page's initialization stage. Only one operation takes place between the Init and InitComplete
events: tracking of view state changes is turned on. View state
tracking enables controls to persist any values that are
programmatically added to the ViewState
collection. Until view state tracking is turned on, any values added to
view state are lost across postbacks. Controls typically turn on view
state tracking immediately after they raise their Init event. Use this event to make changes to view state that you want to make sure are persisted after the next postback. |
|
PreLoad
|
Raised after the page loads view state for itself and all
controls, and after it processes postback data that is included with the
Request instance. |
|
Load
|
The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page. Use the OnLoad event method to set properties in controls and to establish database connections. |
|
Control events |
Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event. |
|
LoadComplete
|
Raised at the end of the event-handling stage. Use this event for tasks that require that all other controls on the page be loaded. |
|
PreRender
|
Raised after the Page
object has created all controls that are required in order to render
the page, including child controls of composite controls. (To do this,
the Page object calls EnsureChildControls for each control and for the page.) The Page object raises the PreRender event on the Page object, and then recursively does the same for each child control. The PreRender event of individual controls occurs after the PreRender event of the page. Use the event to make final changes to the contents of the page or its controls before the rendering stage begins. |
|
PreRenderComplete
|
Raised after each data bound control whose DataSourceID property is set calls its DataBind method. For more information, see Data Binding Events for Data-Bound Controls later in this topic. |
|
SaveStateComplete
|
Raised after view state and control state have been saved for
the page and for all controls. Any changes to the page or controls at
this point affect rendering, but the changes will not be retrieved on
the next postback. |
|
Render
|
This is not an event; instead, at this stage of processing, the Page object calls this method on each control. All ASP.NET Web server controls have a Render method that writes out the control's markup to send to the browser. If you create a custom control, you typically override this method to output the control's markup. However, if your custom control incorporates only standard ASP.NET Web server controls and no custom markup, you do not need to override the Render method. For more information, see Developing Custom ASP.NET Server Controls. A user control (an .ascx file) automatically incorporates rendering, so you do not need to explicitly render the control in code. |
|
Unload
|
Raised for each control and then for the page. In controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections. For the page itself, use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks.
During
the unload stage, the page and its controls have been rendered, so you
cannot make further changes to the response stream. If you attempt to
call a method such as the Response.Write method, the page will throw an exception.
|