Showing posts with label c# interview questions. Show all posts
Showing posts with label c# interview questions. Show all posts

Tuesday, December 10, 2013

Code that uses goto on loop [C#]

static int GotoMethod()
{
    int dummy = 0;
    for (int a = 0; a < 10; a++)
    {
 for (int y = 0; y < 10; y++) // Run until condition.
 {
     for (int x = 0; x < 10; x++) // Run until condition.
     {
  if (x == 5 &&
      y == 5)
  {
      goto Outer;
  }
     }
     dummy++;
 }
    Outer:
 continue;
    }
    return dummy;
}

http://www.dotnetperls.com/goto
Multidimensional Arrays (C# Programming Guide)

Friday, March 29, 2013

The shift operators


The shift operators allow programmers to adjust an integer by shifting all of its bits to the left or the right.

The following diagram shows the affect of shifting a value to the left by one digit
  00001111  =  15
SHIFT LEFT
  00011110  =  30


Compound Assignment Operators
The shift operators have equivalents for compound assignment
These are used by adding an equals sign to the operator and using the number of bits to shift by as the operand.


int value = 240;             // 11110000

value >>= 2;      // Result = 60
value <<= 1;      // Result = 120


http://www.blackwasp.co.uk/CSharpShiftOperators.aspx

Wednesday, February 6, 2013

code sample question 1

int x = 10;  x += x--;  value of x?



x += x--;

This is equivalent to:
x = x + x--;

Which is equivalent to:

int a1 = x; // a1 = 10, x = 10
int a2 = x--; // a2 = 10, x = 9
x = a1 + a2; // x = 20

So x is 20 afterwards - and that's guaranteed by the spec.



  • 1) subexpressions are always evaluated left to right. Period. Evaluation of a subexpression may induce a side effect.


2) execution of operators is always done in the order indicated by parentheses, precedence and associativity. Execution of operators may induce a side effect.

The "x" to the left of the += is the leftmost subexpression, and therefore rule (1) applies. Its value is computed first -- 10.

The x-- to the right of the += is the next one in left-to-right order, so it is evaluated next. The value of x-- is 10, and the side effect is that x becomes 9. This is as it should be, because -- is of higher precedence than +=, so its side effect runs first.

Finally, the side effect of += runs last. The two operands were 10 and 10, so the result is to assign 20 to x.

I get questions about this all the time. Remember, the rules are very straightforward: subexpressions left-to-right, operators in precedence order, period.



  • a difference between --x and x-- here. 

If you had written x += --x;
this would be equivalent to x = x + --x;
then you would get 19.
This is because the value of x is decremented and the resulting value is used in the expression
(unlike x-- where the original value of x is used in the expression).

This expression x = x + --x + x will give 28 because the third timefourth time (see comments) x is evaluated it is 9.



http://stackoverflow.com/questions/2299437/int-x-10-x-x-in-net-why

Two-dimensional array



int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

// Accessing array elements.
System.Console.WriteLine(array2D[0, 0]);
System.Console.WriteLine(array2D[0, 1]);
System.Console.WriteLine(array2D[1, 0]);
System.Console.WriteLine(array2D[1, 1]);
System.Console.WriteLine(array2D[3, 0]);
System.Console.WriteLine(array2Db[1, 0]);
System.Console.WriteLine(array3Da[1, 0, 1]);

http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

Static Classes


A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
http://msdn.microsoft.com/en-us/library/79b3xss3%28v=vs.80%29.aspx

sealed class


A sealed class cannot be inherited. It is an error to use a sealed class as a base class. Use the sealed modifier in a class declaration to prevent inheritance of the class.

Abstract Class in C#

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.
http://msdn.microsoft.com/en-us/library/ms173150%28v=vs.80%29.aspx