C# Programming Questions and Answers
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
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
Select the relevant output for the following set of code:
staticvoid Main(string[] args)
{
int a =4;
int b =5;
int c =6;
int d =8;
if(((a * b / c)+ d)>=((b * c + d )/ a))
{
Console.WriteLine("Line 1 - a is greater to b");
Console.WriteLine((a * b / c)+ d);
}
else
{
Console.WriteLine("Line 1 - a is not greater to b");
Console.WriteLine((b * c + d )/ a);
}
}
Now, here in ‘if’ condition both conditions of parenthesis and hence evaluating operators based on parenthesis are tested.
for expression : ((a * b / c) + d)
Step 1 : (a*b/c) (Evaluating as 4*5/6 = 3)
Step 2 : ( (a*b/c) + d ) (Evaluating (3 + 8 = 11))
Result : 11
for expression : (b * c + d )/ a
Step 1 : (b*c + d) (Evaluating as 5*6 +8 = 38)
Step 2: (b*c + d) / a (Evaluating as 38 / 4 = 9)
Result : 9
The relational operator “>=” between both expressions check for largest figure and hence consecutively executes the if condition.
Output : Line 1 – a is greater to b.
11