Feb 26, 2010

Asp.net C# and OOPs Question & Answer

What’s the difference between const and readonly?

You can initiaLize readonLy variabLes to some runtime vaLues. Let‘s say your program uses current

date and time as one of the vaLues that won‘t change. This way you decLare pubLic readonLy string

DateT 3 new DateTime().ToString().

What does \a character do?

On most systems, produces a rather annoying beep.

Can you create enumerated data types in C#?

Yes.

What’s different about switch statements in C#?

No faLL-throughs aLLowed.

What happens when you encounter a continue statement inside the for loop?

The code for the rest of the Loop is ignored, the controL is transferred back to the beginning of

the Loop.

What’s the advantage of using System.Text.StringBuilder over System.String? StringBuiLder is more

efficient in the cases, where a Lot of manipuLation is done to the text. Strings are immutabLe, so

each time it‘s being operated on, a new instance is created.

Can you store multiple data types in System.Array?

No.

What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?

The first one performs a deep copy of the array, the second one is shaLLow.

How can you sort the elements of the array in descending order?

By caLLing Sort() and then Reverse() methods.

What’s the .NET datatype that allows the retrieval of data by a unique key?

HashTabLe.

What’s class SortedList underneath?

A sorted HashTabLe.

Will finally block get executed if the exception had not occurred?

Yes.

What’s the C# equivalent of C++ catch (A), which was a catch-all statement for any possible

exception?

A catch bLock that catches the exception of type System.Exception. You can aLso omit the parameter

data type in this case and just write catch WX.

Can multiple catch blocks be executed?

No, once the proper catch code fires off, the controL is transferred to the finaLLy bLock (if there

are any), and then whatever foLLows the finaLLy bLock.

Why is it a bad idea to throw your own exceptions?

WeLL, if at that point you know that an error has occurred, then why not write the proper code to

handLe that error instead of passing a new Exception object to the catch bLock? Throwing your own

exceptions signifies some design fLaws in the project.

How’s the DLL Hell problem solved in .NET?

AssembLy versioning aLLows the appLication to specify not onLy the Library it needs to run (which

was avaiLabLe under Win32), but aLso the version of the assembLy.

What are the ways to deploy an assembly?

An MSI instaLLer, a CAB archive, and XCOPY command.

What’s a sateLLite assembLyi

When you write a multilingual or multi-cultural application in .NET, and want to distribute the

core application separately from the localized modules, the localized assemblies that modify the

core application are called satellite assemblies.

What namespaces are necessary to create a LocaLized appLicationi

System.Globalization, System.Resources.

What’s the difference between II comments, I* *I comments and III commentsi

Single-line, multi-line and XML documentation comments.

How do you generate documentation from the C# fiLe commented properLy with a command-Line compiLeri

Compile it with a /doc switch.

What’s the difference between the Debug cLass and Trace cLassi

Documentation looks the same. Use Debug class for debug builds, use Trace class for both debug and

release builds.

Why are there five tracing LeveLs in System.Diagnostics.TraceSwitcheri

The tracing dumps can be quite verbose and for some applications that are constantly running you

run the risk of overloading the machine and the hard drive there. Five levels range from None to

Verbose, allowing to fine-tune the tracing activities.

Where is the output of TextWriterTraceListener redirectedi

To the Console or a text file depending on the parameter passed to the constructor.

How do you debug an ASP.NET Web appLicationi

Attach the aspnet_wp.exe process to the DbgClr debugger.

What are three test cases you shouLd go through in unit testingi

Positive test cases (correct data, correct output), negative test cases (broken or missing data,

proper handling), exception test

cases (exceptions are thrown and caught properly).

Can you change the vaLue of a variabLe whiLe debugging a C# appLicationi

Yes, if you are debugging via Visual Studio.NET, just go to Immediate window.

ExpLain Inheritance, EncapsuLation, PoLymorphismi

CLass: A class is a template or blueprint that defines an object’s attributes and operations and

that is created at design time.

Ob3ect: An object is a running instance of a class that consumes memory and has a finite lifespan.

Inheritance: Inheritance is the concept of reusing common attributes and operations from a

base class in a derived class.

EncapsuLation: Encapsulation is the process of hiding the details from the client. PoLymorphism:

The meaning of the word polymorphism is something like one name, many forms. Polymorphism is the

ability to call the same method on multiple objects that have been instantiated from different

subclasses and generate differing behaviour.

Abstraction: Abstraction is the practice of focusing only on the essential aspects of an object.

It allows you to selectively ignore aspects that you deem unimprtant to the functionality provided

by the object. A good abstraction only provides as many operations and attributes aas are required

to get the job done.

Interface: It‘s an abstract class with public abstract methods all of which must be implemented inthe inherited classes.

Abstract cLass: A class that cannot be instantiated. An abstract class is a class that must be

inherited and have the methods overridden. An abstract class is essentially a blueprint for a class

without any implementation.

When do you absoLuteLy have to decLare a cLass as abstracti

1. When at least one of the methods in the class is abstract.

2. When the class itself is inherited from an abstract class, but not all base abstract

methods have been overridden.

Why can’t you specify the accessibiLity modifier for methods inside the interfacei

They all must be public. Therefore, to prevent you from getting the false impression that you have

any freedom of choice,

you are not allowed to specify any accessibility, it‘s public by default.

How is method overriding different from method overLoadingi

When overriding a method, you change the behavior of the method for the derived class. Overloading

a method simply involves having another method with the same name within the class.

Can you decLare an override method to be static if the originaL method is non-statici No. The

signature of the virtual method must remain the same, only the keyword virtual is changed to

keyword override.

Can you override private virtuaL methodsi

No. Private methods are not accessible outside the class.

What’s the impLicit name of the parameter that gets passed into the cLass’ set methodi

Value, and it‘s datatype depends on whatever variable we‘re changing.

How do you inherit from a cLass in C#i

Place a colon and then the name of the base class. Notice that it‘s double colon in C++.

Does C# support muLtipLe inheritancei

No, use interfaces instead.

When you inherit a protected cLass-LeveL variabLe, who is it avaiLabLe toi

Classes in the same namespace.

What’s the top .NET cLass that everything is derived fromi

System.Object.

What does the keyword virtuaL mean in the method definitioni

The method can be over-ridden.

Can you decLare the override method static whiLe the originaL method is non-statici

No, you can‘t, the signature of the virtual method must remain the same, only the keyword virtual

is changed to keyword override.Can you override private virtuaL methodsi

No, moreover, you cannot access private methods in inherited classes, have to be protected in the

base class to allow any sort of access.

Can you prevent your cLass from being inherited and becoming a base cLass for some other cLassesi

Yes, that‘s what keyword sealed in the class definition is for. The developer trying to derive from

your class will get a message: cannot inherit from Sealed class WhateverBaseClassName. It‘s the

same concept as final class in Java.

Can you aLLow cLass to be inherited, but prevent the method from being over-riddeni

Yes, just leave the class public and make the method sealed.

Why can’t you specify the accessibiLity modifier for methods inside the interfacei

They all must be public. Therefore, to prevent you from getting the false impression that you have

any freedom of choice, you are not allowed to specify any accessibility, it‘s public by default.

Types of PoLymorphismi

1) Compiletime Polymorphism, Early Binding (Method overloading)

2) Runtime Polymorphism, Late Binding(Method overriding through inheritance, Method overriding

through the C# interface)

With runtime polymorphism based on method overriding, the decision as to which version of a method

will be executed is based on the actual type of object whose reference is stored in the reference

variable, and not on the type of the reference variable on which the method is invoked.

Can you inherit muLtipLe interfacesi

Yes, why not.

And if they have confLicting method namesi

It‘s up to you to implement the method inside your own class, so implementation is left entirely up

to you. This might cause a problem on a higher-level scale if similarly named methods from

different interfaces expect different data, but as far as compiler cares you‘re

okay.

You can implement method like Interface1.GetName(), Interface2.GetName() with full qualified method

name.

What’s the difference between an interface and abstract cLassi

In the interface all methods must be abstract, in the abstract class some methods can be concrete.

In the interface no accessibility modifiers are allowed, which is ok in abstract classes.

-Interfaces are closely related to abstract classes that have all members abstract.

- For an abstract class, at least one method of the class must be an abstract method that means it

may have concrete methods.

- For an interface, all the methods must be abstract

- Class that implements an interface much provide concrete implementation of all the methods

definition in an interface or else must be declare an abstract class.

- In C#, multiple inheritance is possible only through implementation of multiple interfaces.

Abstract class can only be derived once.

- An interface defines a contract and can only contains four entities viz methods, properties,

events and indexes. An interface thus cannot contain constants, fields, operators, constructors,

destructors, static constructors, or types.

- Also an interface cannot contain static members of any kind. The modifiers abstract, public,

protected, internal, private, virtual, override is disallowed, as they make no sense in this

context.

- Class members that implement the interface members must be publicly accessible.

How can you overLoad a methodi

Different parameter data types, different number of parameters, different order of parameters.

If a base cLass has a bunch of overLoaded constructors, and an inherited cLass has another bunch of

overLoaded constructors, can you enforce a caLL from an inherited constructor to an arbitrary base

constructori

Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate

constructor) in the overloaded constructor definition inside the inherited class.

What’s the difference between System.String and System.StringBuiLder cLassesi System.String is

immutable, System.StringBuilder was designed with the purpose of having a mutable string where a

variety of operations can be performed.

Does C# support muLtipLe-inheritancei

No, use interfaces instead.

When you inherit a protected cLass-LeveL variabLe, who is it avaiLabLe toi

Classes in the same namespace.

Are private cLass-LeveL variabLes inheritedi

Yes, but they are not accessible. Although they are not visible or accessible via the class

interface, they are inherited.

Describe the accessibiLity modifier “protected internaL“.

It is available to derived classes and classes within the same Assembly (and naturally from the

base class it‘s declared in).

What’s the top .NET cLass that everything is derived fromi

System.Object.

What’s the advantage of using System.Text.StringBuiLder over System.Stringi

StringBuilder is more efficient in cases where there is a large amount of string manipulation.

Strings are immutable, so each time it‘s being operated on, a new instance is created.

Can you store muLtipLe data types in System.Arrayi

No.

What’s a deLegatei

A delegate object encapsulates a reference to a method.

What’s a muLticast deLegatei

It‘s a delegate that points to and eventually fires off several methods

Windows Forms

Can you write a cLass without specifying namespacei Which namespace does it beLong to by defauLtii

Yes, you can, then the class belongs to global namespace which has no name. For commercial

products, naturally, you wouldn‘t want global namespace.



You are designing a GUI appLication with a windows and severaL widgets on it. The user then

resizes the app window and sees a Lot of grey space, whiLe the widgets stay in pLace. What’s the

probLemi

One shouLd use anchoring for correct resizing. Otherwise the defauLt property of a widget on a form

is top-Left, so it stays at the same Location when resized.

How can you save the desired properties of Windows Forms appLicationi

.config fiLes in .NET are supported through the API to aLLow storing and retrieving information.

They are nothing more than simpLe XML fiLes, sort of Like what .ini fiLes were before for Win32apps.

So how do you retrieve the customized properties of a .NET appLication from XML .config fiLei

InitiaLize an instance of AppSettingsReader cLass. CaLL the GetVaLue method of AppSettingsReader

cLass, passing in the name of the property and the type expected. Assign the resuLt to the

appropriate variabLe.

Can you automate this processi

In VisuaL Studio yes, use Dynamic Properties for automatic .config creation, storage and retrievaL.

My progress bar freezes up and diaLog window shows bLank, when an intensive background process

takes over.

Yes, you shouLdIve muLti-threaded your GUI, with taskbar and main form being one thread, and the

background process being the other.

No comments:

Post a Comment