C# Programming Questions and Answers
Correct Declaration of Values to variables ‘a’ and ‘b’?
a) Although,declaration of ‘b’ and ‘a’ are correct but initialization of value to ‘b’ should be ‘int’ datatype not float.
b) Missing declaration type of ‘b’.
c) correctly declared datatypes ‘a’ and ‘b’.
d) ‘b’ can’t be assigned values before declaration.
Select error in the given program :
1.
static Void Main(String[] args)
2.
{
3.
constint m =100;
4.
int n =10;
5.
constint k = n /5*100* n ;
6.
Console.WriteLine(m * k);
7.
Console.ReadLine();
8.
}
’k’ should be declared as const int k = 10/5 * 100*10 i.e only constant values should be assigned to a constant.
Output :Error 1 – The expression being assigned to ‘k’ must be constant.
Correct output for code is?
1.
staticvoid Main(string[] args)
2.
{
3.
float a = 10.553f;
4.
long b = 12L;
5.
int c;
6.
c = Convert.ToInt32(a + b);
7.
Console.WriteLine(c);
8.
}
The two datatype ‘float’ and ‘long’ after arithmetic operation completely converted to nearest whole number 23.
Output : 23.
Select the output for the relevant code set:
1.
staticvoid Main(string[] args)
2.
{
3.
int a =4, b =5, c =7, u =9;
4.
int h;
5.
h =(Convert.ToInt32(u < b))+(a + b--)+2;
6.
Console.WriteLine(h);
7.
Console.WriteLine(b);
8.
Console.WriteLine(u < b);
9.
}
Step 1: Convert.ToInt32(u < b)(Evaluate result as 9 < 5 which is false in nature.So, solution is converted from 'false' to '0').
Step 2: (a + b--) evaluated as 4 + 5 = 9 + 2 =11. Step 3: u < b evaluated as 'False' without being converted to '0'.
Output : 11 4 False
What is the output of the following code?
static void Main(string[] args)
{
int a = 5 ;
if(Convert.ToBoolean((.002f)-(0.1f)))
Console.WriteLine("Sachin Tendulkar");
elseif(a ==5)
Console.WriteLine("Rahul Dravid");
else
Console.WriteLine("Ms Dhoni");
Console.ReadLine();
}
(0.002 – 0.1f) not equivalent to zero hence it is true. So,only first if clause will execute and print:Sachin Tendulkar on console.As,first condition is always true so no else if statement will be executed.
Output: Sachin Tendulkar
Select the correct ‘if statement’ to be filled in the given set of code :
static void Main(string [] args)
{
int[]num ={50, 65, 56, 88, 43, 52};
int even =0, odd =0;
for(int i =0;i < num.Length;i++)
{
/*_________________*/
}
Console.WriteLine("Even Numbers:"+even);
Console.WriteLine("Odd Numbers:"+odd);
Console.ReadLine();
}
int []num = {50, 65, 56, 88, 43, 52};
int even = 0,odd = 0;
for (int i = 0 ;i < num.Length ;i++) { if (num[i] % 2 == 0) { even += 1; } else { odd += 1; } } Console.WriteLine("Even Numbers: " +even); Console.WriteLine("Odd Numbers: " +odd); Console.ReadLine()
Select a convenient declaration and initialization of a floating point number:
We cannot implicitly convert a “double” number directly to any other datatype.Here, its float we have to add the required datatype to number as :
float somevariable = (float)12.502D;
or
Double somevariable = (Double)12.502D;
Correct way to define a value 6.28 in a variable ‘a’ where value cannot be modified ?
Const is a reserve keyword whenever they are declared with any variables.The value stored in that variable always remain fixed.Any modification done to change value of that variable results in error.Hence, options a, b and d are rejected because value is not declared fixed.Now, for value ‘c’ and ‘d’ only option ‘c’ is correct because for value ‘d’ while declaring a constant variable we need to provide a constant value too as provided in ‘c’. So, option ‘c’ is correct way of declaration of constant variable.
Why does a float variable stop incrementing at number ‘16777216’ in given code in C#?
float = 0 ;
while(true)
{
a++;
if ( a > 16777216 )
break;
}
16777216 is exactly 224, and would be represented as 32-bit float like so:
sign = 0 (positive number)
exponent = 24 (stored as 24 + 127 = 151 = 10010111)
mantissa = . 0
As 32 bits floating-point representation: 0 10010111 00000000000000000000000
Therefore: Value = (+ 1) * 2 ^ 24 * (1. 0 + . 0) = 2 ^ 24 = 16777216
Now let’s look at the number 16777217, or exactly 224 + 1:
sign and exponent are the same.
Mantissa should have to be exactly 2-24 so that (+ 1) * 2 ^ 24 * (1. 0 + 2 ^-24) = 2 ^ 24 + 1 = 16777217 and here lies the actual problem . The mantissa cannot have the value 2-24 because it only has 23 bits, so the number 16777217 just cannot be represented with the accuracy of 32-bit floating points numbers.
Select the output for the following set of Code :
staticvoid Main(string[] args)
{
int a =8, b =6, c =10;
int d = a * c *2/ Convert.ToInt32(Math.Pow((c - b), 2));
if(d ==(c = Convert.ToInt32(Math.Sqrt(a * a + b * b)))&& c ==10)
{
Console.WriteLine("figure is hypotenuse");
}
else
{
Console.WriteLine("figure is square");
}
}
Solving the expression for ‘c’ we get c==10 in if first condition as (c == Convert.ToInt32(Math.Sqrt(a * a + b * b))). The logical condition when d == (c = 10) suits here . Similarly, going for second condition where c ==10 as ‘&&’ operator exists between both given condition and at last both are evaluated to true as c == 10. So, only first statement is executed.
Output :Figure is square