C# Programming Questions and Answers
A preprocessor is a program
What is the output of the following set of code ?
staticvoid Main(string[] args)
{
int i, j;
int[, ] arr =newint[3, 3];
for(i =0; i <3;++i)
{
for(j =0; j <3;++j)
{
arr[i, j]= i *2+ i *2;
Console.WriteLine(arr[i, j]);
}
Console.ReadLine();
}
}
Since,with each value of of ‘i’ the value of ‘j’ is executed three times i.e
for i = 0, j = 0, 0, 0, i = 1, j = 2, 2, 2.
Output: 0, 0, 0 4, 4, 4 8, 8, 8.
What is the output for the following set of code ?
staticvoid Main(string[] args)
{
double a =345.09;
byte c =(byte) a;
Console.WriteLine(c);
Console.ReadLine();
}
Type casting a larger variable into a smaller variable results in modules of larger variable by range of smaller variable. a is ‘345.09’ which is larger than byte’s range i:e -128 to 127.
Output : 89.
Which is the correct way of defining and initializing an array of 3 integers?
Answer: Option (A) and Option (D)
What is the output for the following set of code?
staticvoid Main(string[] args)
{
char A ='K';
char B = Convert.ToChar(76);
A++;
B++;
Console.WriteLine(A+" "+B);
Console.ReadLine();
}
“++” increments the value of character by 1. A and B are given values K and 76, when we use increment operator their values increments by 1, A and B becomes L and M.
Output: L, M.
Choose selective differences between an array in c# and array in other programming languages.
1. When declaring an array in C#, the square brackets ([]) come after the type, not the identifier.Brackets after the identifier is
not legal syntax in C#.
example : int[] IntegerArray;
2. The size of the array is not part of its type as it is in the C language. This allows to declare an array and assign any array
of int objects to it, regardless of the array’s length.
int[] IntegerArray;
IntegerArray = new int[10];
IntegerArray = new int[50];
Difference between keywords 'var' and 'dynamic' is:
Answer: Option (A), (B) and (C)
Select output of the given set of Code :
staticvoid Main(string[] args)
{
String name ="Dr.Gupta";
Console.WriteLine("Good Morning"+ name);
}
How to intialize a string variable and concatenate string using ‘+’ operator.
Output:Good Morning Dr.Gupta
Select output for the following set of code.
staticvoid Main(string[] args)
{
int a =5;
int b =10;
int c;
Console.WriteLine(c = a---++b);
Console.WriteLine(b);
Console.ReadLine();
}
Answer: Option (C)
Which of these keywords is not a part of exception handling?
Exception handling is managed via 5 keywords – try, catch, throws, throw and finally.