C# Interview Questions and Answers (958) - Page 20

Difference between catch(Exception error) and catch

The code below explains the difference between catch(Exception error) and catch

try
{
//Code that might generate error
}

catch(Exception error)
{
//Catches all Cls-complaint exceptions
}

catch
{
//Catches all other exception including the Non-Cls complaint exceptions
}

What is Multi-Threading?

Threading:

A thread is nothing more than a process.
The process sets up sequential steps, each step executing a line of code.

MultiThreading:

A multithreaded application allows you to run several threads, each thread runs in its own process.
So that you can run step1 in one thread and step 2 in another thread at the same time.
What are Destructors?

Destructors are used to destruct instances of classes.
Destructors cannot be defined in structures, they are only used with classes.
A class can only have one destructor.
Destructors cannot be inherited or overloaded.
Destructors cannot be called, they are invoked automatically.
A destructor does not take modifiers or have parameters.

Example:

class Car

{
~ Car() // destructor
{
// cleanup statements...
}
}

What is a Constructor?

A constructor is a special method for initializing a new instance of a class.
The constructor method for a class will have the same name as that of a class.
A class may have multiple constructors in which, each constructor will have the same name, but will have different arguments.
There is no need of calling a constructor, It will evoke automatically when an Object of the class is created.
What are the types of Constructors ?

There are three types of constructors.

1)Default Constructor
2)Parametric Constructor
3)Copy Constructor

Default Constructor :

A default constructor is a constructor which has no parameters or where it has parameters they are all defaulted.

Parametrized Constructor :

Parametrized constructors allows you to create a new instance of a class while simultaneously passing arguments to the new instance.

Copy Constructor :

A copy constructor is a special constructor which creates a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed (const or non-const), which might be followed by parameters of any type (all having default values).
What is Parse Error ?

It is just a syntactical error in our ASPX page. It generally happens when our page is unreadable for the part of ASP.NET which transforms our code into an executable code.

Example

Dim xmlDoc As New Msxml2.DOMDocument30
Dim myErr As IXMLDOMParseError
xmlDoc.async = False
xmlDoc.Load App.Path & "\lyrics.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
Dim myErr
Set myErr = xmlDoc.parseError
MsgBox("Error Occured" & myErr.reason)
Else
Set myErr = xmlDoc.parseError
If (myErr.errorCode <> 0) Then
MsgBox ("Error Occured " & myErr.reason)
End If
End If

When we use HTTP handlers can we access session state ?

No, When we use HTTP handlers we cannot access Session state but we can access by using a "marker" interface IRequiresSessionState or IReadOnlySessionState in order to use session state.
Can we use session variables in a class ?

Yes we can use session variables in class. This can be done using this way

First of all we need to import the name space

Use HttpContext.Current.Session

In C#.Net

HttpContext.Current.Session["Value1"] = "1";


In VB.Net

HttpContext.Current.Session("Value1") = "1"  

How can we check files exists in a physical directory ?

We can check whether a particular file is existing in a physical directory or not by using this code

System.IO.File.Exists("path")". path


This is the physical file path and it will return you a Boolean value so that we can check whether a file is existing or not
How can we save a file at Client Machine?

Generally browser will not allow us to save a file directly to a client machine however we can use this

Response.Redirect("http://server/filename"); 


Which will send the file back to the browser and the user will be prompted to Open/Save a file at this local system.
How can we make a browser window opening with a maximum size on a button click?

We can make that by setting an attribute FullScreen = 'Yes' by this way

Button1.Attributes.Add("onclick", "window.open(’page2.aspx’,’’,’fullscreen=yes’)");  

Can we delete a file from the Server?

Yes we can delete a file located at the server side. That can be done using this simple step :

System.IO.File.Delete(Server.MapPath("MyFile.txt"));

How can we specify a line break in Labels text ?

This is very simple question where we can specify line break using <br> tag. Sample follows

   Label1.Text = "<Anothertag>" + "<br>" + "<AnotherTag>";  

What is the basic difference between a "const" and "readonly" in C#?

const = const variables are implicitly static and they must be defined when declared. Example :-

private const  int testVariable = 500;


In the above code, a constant variable is declared and defined in the same time. If you don't initialize a const variable then it will results in compilation error.

readonly = The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

public readonly  int i = 5;


Example :-

class MyTestClass 

{
public readonly int i;

public MyTestClass()
{
i = 100; // Initialize a readonly instance field
}
}


In readonly , either you initialize in the declaration statement or you initialize it via a constructor.


I hope the above code samples are clear.

Thanks and Regards
Akiii
What’s the advantage of using System.Text.StringBuilder over System.String?

String Builder 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.
Difference between String.Equals(string1,string2) and string1.Equals(string2)

If any of strings value is null, string1.Equals(string2) will throw runtime error but
String.Equals(string1,string2) will not throw any error.

Example:

string str1=null;
string str2="abc";
str2.Equals(str1); // will give runtime error
String.Equals(str1,str2); //will not give any error
Difference between catch(Exception objex) and catch() block

catch(Exception objEx) will catch only .net compliance exceptions and catch() will catch all types of exception non-compliance and .net compliance.
Can you allow class to be inherited, but prevent the method from being over-ridden?

Yes, we can allow the class to be inherited without over-riding the method.
Just leave the class as public and make the method sealed.
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.
Which is the correct declaration for a nullable integer?

NOTE: This is objective type question, Please click question title for correct answer.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories