Breaking

Sunday, October 28, 2018

C# Coding Interview Questions (Part – 1)



1. What happens when you compile and run the below program?


public class CsharpArray
    {
public static void Main(string[] args)
        {
int[] i = new int[0];
Console.WriteLine(i[0]);
        }
    }

Output:

System.IndexOutOfRangeException: Index was outside the bounds of the array

2. What will be the result for below program?


public class InterviewProgram
    {
public static void Main(string[] args)
        {
        #if (!pi)
Console.WriteLine("i");
        #else
Console.WriteLine("pi not define");
        #endif
Console.WriteLine("ok");
Console.ReadLine();
        }
    }

Output:

i
ok

3.What will be the result for below program?


public class CsharpArray
    {

public static void Main(string[] args)
        {
int[] a = new int[3];

a[1] = 10;

        Object o = a;

int[] b = (int[])o;

b[1] = 100;

Console.WriteLine(a[1]);

        ((int[])o)[1] = 1000;

Console.WriteLine(a[1]);
        }
    }

Output:

100
1000
Press any key to continue…

4.Predict the output for below program.


namespaceInterviewNamespace
{
public class Generic<T>
    {
public T Field;
    }
public class InterviewProgram
    {
public static void Main(string[] args)
        {
            Generic<int> g2 = new Generic<int>();
            Generic<int> g3 = new Generic<int>();
            g2.Field = 8;
            g3.Field = 4;
if (g2.Field % g3.Field == 0)
            {
Console.WriteLine("Welcome to CsharpStar !!");
            }
else
Console.WriteLine("Prints nothing:");
Console.ReadLine();
        }
    }
}

Output:

Welcome to CsharpStar !!

5. What will be the ouput for below program?


public class CsharpArray
    {
public static void Main(string[] args)
        {
int[] a = { 10, 20, 30, 40, 50, 80 };
Console.WriteLine(a[-1]);
        }
    }

Output:

System.IndexOutOfRangeException: Index was outside the bounds of the array because arry indices always starts with 0

6.Predict the ouput for below program?


public class InterviewProgram
    {
public static void Main(string[] args)
        {
            String a = "CsharpStar";
            String b = "CSHARPSTAR";
int c;
            c = a.CompareTo(b);
Console.WriteLine(c);
        }

    }

Output:

-1

7. What will be the ouput for below program?


public class CsharpArray
    {
static void methodOne(int[] a)
    {
int[] b = new int[5];

        a = b;

Console.WriteLine(a.Length);

Console.WriteLine(b.Length);
    }
public static void Main(string[] args)
        {
int[] a = new int[10];

methodOne(a);

Console.WriteLine(a.Length);
        }
    }

Output:

5
5
10
Press any key to continue…

8. Predict the output for below program


public class InterviewProgram
    {
public static void Main(string[] args)
        {
InterviewProgram p = new InterviewProgram();
p.display(2, 3, 8);
int[] a = { 2, 56, 78, 66 };
Console.WriteLine("Example of array");
Console.WriteLine("Array elements added are");
p.display(a);
Console.ReadLine();
        }
public void display(paramsint[] b)
        {
foreach (int i in b)
            {
Console.WriteLine("ARRAY IS HAVING:{0}", i);
            }
        }
    }

Output:

ARRAY IS HAVING: 2
ARRAY IS HAVING: 3
ARRAY IS HAVING: 8
Example of array
Array elements added are
ARRAY IS HAVING: 2
ARRAY IS HAVING: 56
ARRAY IS HAVING: 78
ARRAY IS HAVING: 66

9. What happens when you compile and run the below program?


public class InterviewProgram
    {
public static void Main(string[] args)
        {
char A = 'P';
char B = Convert.ToChar(76);
            A++;
            B++;
Console.WriteLine(A + "  " + B);
Console.ReadLine();
        }

    }

Output:

Q M
 

10. What happens when you compile and run the below program?


namespaceInterviewNamespace
{
delegate string del(string str);
class sample
    {
public static string DelegateSample(string a)
        {
returna.Replace(',', '*');
        }
    }
public class InterviewProgram
    {
public static void Main(string[] args)
        {
del str1 = new del(sample.DelegateSample);
stringstr = str1("Welcome to CsharpStar !!");
Console.WriteLine(str);
        }
    }
}

Output:

Welcome to CsharpStar !!


No comments:

Post a Comment