Thursday 31 May 2012

practical interview questions of c#



Q. Write a program in C# that take string input, and print its number of characters.

string name = Console.ReadLine();
Console.WriteLine(name.Length);
Console.ReadLine();

Q. Write a program in C Sharp that take a sentense as input, and show the number of "a" used in the sentense.

string name = Console.ReadLine();
int counter = 0;
for (int i = 0; i < name.Length; i++)
{
if (name[i] == 'a')
{
counter++;
}


}
Console.WriteLine(counter);
Console.ReadLine();

Q. Write a program in C# taht take name and password. If the name and password are correct, the program show "you are logged in", otherwise, "incorrect name or password".

Console.WriteLine("Enter your Name");
Console.WriteLine("Enter your Pswrd");
string name = Console.ReadLine();
string pswrd = Console.ReadLine();
string myname = "bilal";
string mypswrd = "123456";
if (name == myname && pswrd == mypswrd)
{
Console.WriteLine("You are logged in");
}
else
{
Console.WriteLine("Incorrect name or pswrd");
}
Console.ReadLine();
Sorting Arrays in C Sharp
Q. Write a string array of length 3, and sort them.

string[] name = new string[] { "We", "He", "Us"};
Array.Sort(name);
foreach (string i in name)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write a string array in C# that take 5 inputs, and sort them.

string[] name = new string[5];
for (int i = 0; i < 5; i++)
{
name[i] = Console.ReadLine();
}
Array.Sort(name);
foreach (string i in name)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write an array in C Sharp of length 3, and sort it.

int[] numbers = new int [] { 4, 3, 8, 0, 5 };
Array.Sort(numbers);
foreach (int i in numbers)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write a program in C# that take 5 integers, and sort them.

int[] numbers = new int[5];
for (int i = 0; i < 5; i++)
{
numbers[i] = Convert.ToInt16(Console.ReadLine());
}
Array.Sort(numbers);
foreach (int i in numbers)
{
Console.WriteLine(i);
}
Console.ReadLine();
Print Pattern in C-Sharp
Q. Print * 10 times vertically usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.WriteLine("*");
}
Console.ReadLine();

Q. Print * 10 times Horizontally usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.Write("*");
}
Console.ReadLine();

Q. Print * 10 times Horizontally with spaces between them usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.Write("* ");
}
Console.ReadLine();

Q. Write a program in C# that take string input and print the result vertically.

string name = Console.ReadLine();
for (int i = 0; i < name.Length; i++)
{
Console.WriteLine(name[i]);
}
Console.ReadLine();

Q. Print the following pattern using C# Console Application.
*
**
***
****
*****


for (int i = 1; i < 6; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();

Q. Print the following pattern using C-Sharp Console Application.
*****
****
***
**
*


for (int i = 5; i > 0; i--)
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();

Q. Print pyramid using C# Console Application, like this:
    *
   * *
  * * *
 * * * *
* * * * *
for (int i = 1; i < 6; i++)
{
for (int j = 4; j >= i; j--)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write("* ");
}
Console.WriteLine("");
}
Console.ReadLine();

No comments:

Post a Comment