OOPS interview questions


Boxing and Un-Boxing

Boxing: means converting value-type to reference-type.

Eg:

int I = 20;

string s = I.ToSting();

UnBoxing: means converting reference-type to value-type.

Eg:

int I = 20;

string s = I.ToString(); //Box the int

int J = Convert.ToInt32(s); //UnBox it back to an int.

Note: Performance Overheads due to boxing and unboxing as the boxing makes a copy of value type from stack and place it inside an object of type System.Object in the heap.

Inheritance

The process of sub-classing a class to extend its functionality is called Inheritance.

It provides idea of reusability.

Order of Constructor execution in Inheritance

constructors are called in the order from the top to the bottom (parent to child class) in inheritance hierarchy.

Order of Destructor execution in Inheritance

The destructors are called in the reverse order, i.e., from the bottom to the top (child to parent class) in the inheritance hierarchy.

What are Sealed Classes in C#?
The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed class is specified as the base class of another class. (A sealed class cannot also be an abstract class)

Can you prevent your class from being inherited by another class?
Yes. The keyword “sealed” will prevent the class from being inherited.

Can you allow a class to be inherited, but prevent the method from being over-ridden?
Yes. Just leave the class public and make the method sealed.

Fast Facts of Inheritance

<!--[if !supportLists]--> Multiple inheritance of classes is not allowed in C#.<!--[endif]-->

<!--[if !supportLists]--> In C# you can implements more than one interface, thus multiple inheritance is achieved through interface.<!--[endif]-->

<!--[if !supportLists]--> The Object class defined in the System namespace is implicitly the ultimate base class of all the classes in C# (and the .NET framework) <!--[endif]-->

<!--[if !supportLists]--> Structures (struct) in C# does not support inheritance, it can only implements interfaces.<!--[endif]-->

Polymorphism

Polymorphism means same operation may behave differently on different classes.

Eg:

Method Overloading is an example of Compile Time Polymorphism.

Method Overriding is an example of Run Time Polymorphism

Does C#.net supports multiple inheritance?

No. A class can inherit from only one base class, however a class can implements many interface, which servers some of the same purpose without increasing complexity.

How many types of Access Modifiers.

<!--[if !supportLists]-->1) Public – Allows the members to be globally accessible.<!--[endif]-->

<!--[if !supportLists]-->2) Private – Limits the member’s access to only the containing type.<!--[endif]-->

<!--[if !supportLists]-->3) Protected – Limits the member’s access to the containing type and all classes derived from the containing type.<!--[endif]-->

<!--[if !supportLists]-->4) Internal – Limits the member’s access to within the current project.<!--[endif]-->

Method Overloading

<!--[if !supportLists]--> Method with same name but with different arguments is called method overloading.<!--[endif]-->

<!--[if !supportLists]--> Method Overloading forms compile-time polymorphism.<!--[endif]-->

<!--[if !supportLists]--> Eg:<!--[endif]-->

class A1

{

void hello()

{ Console.WriteLine(“Hello”); }

void hello(string s)

{ Console.WriteLine(“Hello {0}”,s); }

}

Method Overriding

<!--[if !supportLists]--> Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.<!--[endif]-->

<!--[if !supportLists]--> Method overriding forms Run-time polymorphism.<!--[endif]-->

<!--[if !supportLists]--> Note: By default functions are not virtual in C# and so you need to write “virtual” explicitly. While by default in Java each function are virtual.<!--[endif]-->

<!--[if !supportLists]--> Eg1:<!--[endif]-->

Class parent

{

virtual void hello()

{ Console.WriteLine(“Hello from Parent”); }

}

Class child : parent

{

override void hello()

{ Console.WriteLine(“Hello from Child”); }

}

static void main()

{

parent objParent = new child();

objParent.hello();

}

//Output

Hello from Child.

Virtual Method

By declaring base class function as virtual, we allow the function to be overridden in any of derived class.

Eg:

Class parent

{

virtual void hello()

{ Console.WriteLine(“Hello from Parent”); }

}

Class child : parent

{

override void hello()

{ Console.WriteLine(“Hello from Child”); }

}

static void main()

{

parent objParent = new child();

objParent.hello();

}

//Output

Hello from Child.




Posted by DotNetGuts at 8:24 PM 0 comments

Monday, May 7, 2007


What is Interface

·       An Interface is a group of constants and method declaration.

·       .Net supports multiple inheritance through Interface.

·       Interface states “what” to do, rather than “how” to do.

·       An interface defines only the members that will be made available by an implementing object. The definition of the interface states nothing about the implementation of the members, only the parameters they take and the types of values they will return. Implementation of an interface is left entirely to the implementing class. It is possible, therefore, for different objects to provide dramatically different implementations of the same members.

·       Example1, the Car object might implement the IDrivable interface (by convention, interfaces usually begin with I), which specifies the GoForward, GoBackward, and Halt methods. Other classes, such as Truck, Aircraft, Train or Boat might implement this interface and thus are able to interact with the Driver object. The Driver object is unaware of which interface implementation it is interacting with; it is only aware of the interface itself.

·       Example2, an interface named IShape, which defines a single method CalculateArea. A Circle class implementing this interface will calculate its area differently than a Square class implementing the same interface. However, an object that needs to interact with an IShape can call the CalculateArea method in either a Circle or a Square and obtain a valid result.

·       Practical Example

public interface IDrivable
{
void GoForward(int Speed);
}

public class Truck : IDrivable
{
public void GoForward(int Speed)
{
// Implementation omitted
}
}

public class Aircraft : IDrivable
{
public void GoForward(int Speed)
{
// Implementation omitted
}
}

public class Train : IDrivable
{
public void GoForward(int Speed)
{
// Implementation omitted
}
}


Extra

·       Each variable declared in interface must be assigned a constant value.

·       Every interface variable is implicitly public, static and final.

·       Every interface method is implicitly public and abstract.

·       Interfaces are allowed to extends other interfaces, but sub interface cannot define the methods declared in the super interface, as sub interface is still interface and not class.

·       If a class that implements an interface does not implements all the methods of the interface, then the class becomes an abstract class and cannot be instantiated.

·       Both classes and structures can implement interfaces, including multiple interfaces.


Let us understand SQL injection attack, with an example. I have an Employee Search Page as shown in the image below. This webform has a very simple functionality. You enter the ID of the employee, you want to search and click the Search Employee button. If a match is found in the database, we show the employee record in the GridView.

The HTML for the Employee Serach Page is shown below. As you can see from the HTML, the Employee Serach Page contains TextBox, Button and a GridView control.



Employee Search Page HTML



The Button1_Click event handler has the required ADO.NET code to get data from the database. This code is highly susceptible to sql injection attack and I will never ever have code like this in production environment. The second line in Button1_Click event handler, dynamically builds the sql query by concatenating the Employee ID that we typed into the TextBox.



So, for example, if we had typed 2 into the Employee ID textbox, we will have a SQL query as shown below.

Select * from Employees where Id=2



If a malicious user, types something like 2; Delete from Employees into the TextBox, then we will have a SQL query as shown below.

Select * from Employees where Id=2; Delete from Employees



When this query is executed, we loose all the data in the Employees table. This is SQL Injection Attack, as the user of the application is able to inject SQL and get it executed against the database. It is very easy to avoid SQL Injection attacks by using either parameterized queries or using stored procedures.



You may be thinking, how will the user of the application know the name of the table. Well, one way is to simply guess or inject a sql syntax error. The injected SQL syntax error causes the page to crash and can possibly reveal the name of the table as shown below. However, proper exception handling and custom error pages can be used to prevent the end user from seeing the yello screen of death. The screen shot below shows the table name Employees.



Page crash revealing Employees table name


 Explain inline, embedded and external style sheets .



Posted by: Chikul

There are three ways of inserting a style sheet:



1. External style sheet

2. Internal style sheet

3. Inline style



External Style Sheet :

An external style sheet is ideal when the style is applied to many pages.

With an external style sheet, you can change the look of an entire Web site by changing one file.

Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the head section:

<head>

<link rel="stylesheet" type="text/css" href="mystyle.css" />

</head>



Internal Style Sheet :

An internal style sheet should be used when a single document has a unique style. Internal styles sheet needs to put in the head section of an HTML page, by using the <style> tag, like this:

<head>

<style type="text/css">

hr {color:sienna}

p {margin-left:20px}

body {background-image:url("images/back40.gif")}

</style>

</head>



Inline Styles :

If only a small piece of code has to be styled then inline style sheets can be used.

An inline style loses many of the advantages of style sheets by mixing content with presentation.

To use inline styles you use the style attribute in the relevant tag.

The style attribute can contain any CSS property.

The example shows how to change the color and the left margin of a paragraph:

<p style="color:sienna;margin-left:20px">This is a paragraph.</p>



What are the values of "Position" attribute in CSS?



Posted by: Virendradugar

Possible values are

static, relative, absolute, fixed, inherit





What is the default value of "position" attribute in css?



Posted by: Virendradugar

Default value is "static".





display and visibility properties are used to hide and show elements in any page. Then how they are different from each other?



Posted by: Virendradugar

As said Both the properties are used to hide and show elements but they are different in the way they both work. visibility property, set to hidden will still occupy the space in the layout but display:none does not take up the space in the page.





Can you specify more than one css class for any HTML element?



Posted by: Virendradugar

Yes, you can. Just provide a space between both the class names.



like..





<div class="class1 class2">

</div>

No comments:

Post a Comment