Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Microsoft .NET Interview Questions and Answers

Test your skills through the online practice test: Microsoft .NET Quiz Online Practice Test

Ques 1. Write code for a case statement.

switch (n)
{
case 1:
x=1;
break;
case 2:
x=2;
break;
default:
goto case 1;
}

Is it helpful? Add Comment View Comments
 

Ques 2. Is a struct stored on the heap or stack?

Stack

Is it helpful? Add Comment View Comments
 

Ques 3. Can a struct have methods?

Yes

Is it helpful? Add Comment View Comments
 

Ques 4. What is checked { } and unchecked { }?

By default C# does not check for overflow (unless using constants), we can use
checked to raise an exception. E.g.:
static short x = 32767; // Max short value
static short y = 32767;
// Using a checked expression
public static int myMethodCh()
{
int z = 0;
try
{
z = checked((short)(x + y));
//z = (short)(x + y);
}
catch (System.OverflowException e)
{
System.Console.WriteLine(e.ToString());
}
return z; // Throws the exception OverflowException
}
This code will raise an exception, if we remove unchecked as in:
//z = checked((short)(x + y));
z = (short)(x + y);
Then the cast will raise no overflow exception and z will be assigned –2. unchecked can
be used in the opposite way, to say avoid compile time errors with constanst overflow.
E.g. the following will cause a compiler error:
const short x = 32767; // Max short value
const short y = 32767;
public static int myMethodUnch()
{
int z = (short)(x + y);
return z; // Returns -2
}
The following will not:
const short x = 32767; // Max short value
const short y = 32767;
public static int myMethodUnch()
{
int z = unchecked((short)(x + y));
return z; // Returns -2
}

Is it helpful? Add Comment View Comments
 

Ques 5. Can C# have global overflow checking?

Yes

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook