Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Monday, October 26, 2009

C# Object oriented programming concepts

Object oriented programming is a successful paradigm shift from procedural world. Representing a real world entity through an object and encapsulating data and behavior together in an object are some of basics of OOPS world. Object oriented has showered us with many benefits esp. code re usability through inheritance and abstraction.  At the same time, we are pushed into complex world filled with classes, contracts, objects and relationships between them. Polymorphism, encapsulation, messaging, abstraction are OOPS concepts.

There are three kinds of relationships between objects

Association:  Simple relationship which relates two objects. Customer object is related to sales object. 

Aggregation: It is containment or composition model. Example: page class encompasses controls on the page.

Inheritance:  Defining specialized derived classes from preexisting generalized classes. It shows more of a parent-child relationship like vehicle -> car


Abstract classes and Interfaces:


A class can implement more than one interface but can only inherit from one abstract class. Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritances. All elements in interfaces are public by default.

An abstract class can contain non-abstract methods. i.e methods with code in it.
An interface cannot contain non-abstract methods. Abstract classes SHOULD have atleast one method abstract and interfaces have all abstract methods.

Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

An abstract class defines the core identity of a class and there it is used for objects of the same type.

An abstract class can contain non-static member variables.
An interface cannot contain non-static member variables.

A derived class uses the keyword "Extends" for an abstract class.
A subclass uses the keyword "Implements" for an interface.

If the subclass extends an abstract class, it cannot extend any other class.
If the subclass implements an interface, it can implement any number of other interfaces.

If the various implementations only share method signatures then it is better to use interfaces ( Multiple behaviors for different conditions)

Abstract classes provide a simple and easy way to version our components. By updating the base class, all the inheriting classes are automatically updated with the change. In an interface, creation of additional functions will have an effect on its child classes due to the necessary implementations of interface methods in classes. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

Say there are two classes, bird and airplane, and both of them have methods called fly. It would be ridiculous for an airplane to inherit from the bird class just because it has fly() method. Rather, the fly() method should be defined as an interface and both bird and airplanes should implement that interface. If we want to provide common, implemented functionality among all implementations of component.

In VB.Net language, abstract classes are created using "MustInherit" keyword. In C#, we have "Abstract" keyword. Abstract class is designed to act as a base class. You cannot create a object of abstract class.

Shadowing:

When two elements in a program have same name, one of them can hide and shadow the other one. So in such cases the element which shadowed the main element is referenced.

Differences between overriding and shadowing:

  • Overriding redefines only the implementation while shadowing redefines the whole element.

Sealed classes:

The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class. A sealed class cannot also be an abstract class. In .Net Framework, string is a sealed class.

Virtual keyword: (Overridable)
They signify  that method and property can be overridden.

Static/Shared:

  • If you want an object to be shared between multiple instances you will use a static/shared class.
  • Static classes cannot be instantiated. By default, an object is created on the first method call to that object.
  • Static classes cannot be inherited. 
  • Static classes can have only static members.
  • Static classes can have only static constructor

Structures and classes:

  • Structures are value types and classes are reference types. So structures use stack and classes use heap. 
  • Structure members cannot be declared as protected, but class member can be.
  • You cannot define inheritance in structures.
  • Structures do not require constructors while classes require.
  • Objects created from classes are terminated using garbage collector while structures are not destroyed GC



Sunday, October 4, 2009

ASP.Net/SQL Server/.Net Framework Interview questions

Following are .Net Framework/ASP.Net/SQL Server questions which tests your mettle in these technologies. I quickly gathered these questions from different sources. Probably, i guess I should provide answers to these questions. I'll do that in my next post.


Where are the value types contained inside a reference type stored? Say for example if there is a class Employee and a member 'int roll no', where will the roll number be stored?
  1. In the stack
  2. In the heap
  3. In the harddisk
What is faster?
  1. Accessing the heap
  2. Accessing the stack
  3. Accessing the hard disk
If you declare String a = "Minecode" and 'a' points to an address 1000, executing a = "MINECODE01" will not change the address. Is this true?
  • True
  • False
Which of the following datatypes cannot be set to null?
  1. string
  2. custom classes
  3. DateTime

What are the interfaces related to USING block in C#?
  1. IComparer
  2. IDisposable
  3. ISerializable
Which of the following is true?
  1. A static method can access all private properties of a class.
  2. A static method can access only the public properties of a class.
  3. None of the above
Which of the following is true?

  1. A delegate uses an event
  2. An event uses a delegate
  3. None of the above

If we have a try block and three catch blocks, the first catch block catches Exception, the second one catches ArgumentException and the third one catches ArgumentNullException, Which block will catch an ArgumentNullException thrown from the try block?
  1. Catch Exception
  2. Catch ArgumentException
  3. Catch ArgumentNullException

LogIn is bad on?
  1. A write intensive table
  2. A read intensive table
  3. None
You can use an insert statement on a view.
  • True
  • False
You can insert into a table without providing values for all the non null columns.
  • True
  • False
Group by takes precedency over Order by.
  • True
  • False
Which of the following can not be rolled back?
  1. Truncate
  2. Delete
  3. Update
  4. None of the above
Referential integrity is related to.
  1. Views
  2. Foriegn keys
  3. LogIn
Which of the following is true.
  1. There can be only one unique column in a table
  2. There can be only one primary key in a table
  3. There can be only one foriegn key in a table

Which SQL statement is used to return only different values?
  1. select distinct
  2. select unique
  3. select different
If there are two tables Employees and Orders an inner join between employees and orders would fetch employee rows?
  1. Even if there is no order for the employee.
  2. only if there is an order for the employee

If there are two tables Employees and Orders an left join between employees and orders would fetch employee rows
  1. Even if there is no order for the employee.
  2. only if there is an order for the employee.
I have a table called products that has the list of all products. I have a table called stores which has all the stores in the market. If I want to track the sales for a store and product combination, what kind of join will I use?

  1. Inner Join
  2. Right Outer Join
  3. Cross Join
  4. Left Outer Join
Can you explicitly update an Identity column?
  • Yes
  • No

In Asp.Net, interceptions like authentication and session information are handled
using
  1. Pages
  2. handlers
  3. Modules

The page class implements
  1. IHttpModule
  2. IHttpHandler
  3. IHttpContext
Managing Session state in memory
  1. Decreases scalability
  2. Has no effect on scalability
  3. Increases scalability
Page remembers the state of the various controls across postbacks because of
  1. Application state
  2. Session state
  3. Cache
  4. View State

In which file do we handle the application start and end event?
  1. the aspx file
  2. Web.config
  3. application.asax
  4. global.asax
Which of the following is not a stage of page - Load Life cycle?
  1. Load()
  2. Init()
  3. PreRender()
  4. Render(),PostRender()
  5. Unload()

Interface does not come with an implementation. If there are multiple classes that want to implement the same interface and want to implement the same way, they are helpless. What is the correct way to work around this problem?

  1. Inheritance
  2. Composition
  3. Encapsulation
  4. Polymorphism

A contract that could possibly come with a default implementation

  1. Abstract Base class
  2. Interface
  3. Sealed class
  4. Partial class
What is the scope of connection pooling?
  1. Appdomain
  2. Process
  3. Machine
Which of the following prevent SQL injection?
  1. Use of Stored Procedures
  2. Use of dynamic queries by using SqlParameter objects.
  3. Use of dynamic Queries by concatinating strings.
I have a million rows in a database and I want to read the records and show them in the UI. What is the prefered way to read the data?

  1. DataReader
  2. DataSet
  3. RecordSet
When a web service receives an invalid input it should throw
  1. SoapFault with Fault code set to Client
  2. InvalidArgumentException
  3. SoapFault with Fault code set to Server
  4. Argument Exception
ASP.NET uses _________ to generate a WSDL out of your class
  1. Polymorphism and Serialization
  2. Reflection and serialization
  3. Encapsulation and serialization
  4. ISDASM and Serialization
WSDL is a
  1. contract in xml form
  2. contract in binary form
  3. Protocol to access a web service
  4. Directory to discover a web service

Where would you store the connection strings that are used in the ASP.NET application?
  1. Web.config
  2. Machine.config
  3. In your page class
  4. in global.asax

Which config file takes precedence?
  1. Web.Config
  2. Machine config

What is the extension used for ASP.NET HTTP Handlers?
  1. ASHX
  2. ASCX

Response.Redirect redirects to a new URL

  1. With the knowledge of the browser
  2. without the knowledge of the Browser