C# Programming Questions and Answers
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 the output for the following set of code :
staticvoid Main(string[] args)
{
int i =30;
int j =25%25;
if(Convert.ToBoolean(Convert.ToInt32(i = j)))
{
Console.WriteLine("In if");
}
else
{
Console.WriteLine("In else");
}
Console.WriteLine("In main");
Console.ReadLine();
}
Usage of ‘=’ operator instead of ‘==’ operator .hence,the condition is not true.
Output: In else
In main
Select the output for the following set of Code :
staticvoid Main(string[] args)
{
int a =5, b =10;
if(Convert.ToBoolean(Convert.ToInt32(0xB)))
if(Convert.ToBoolean(Convert.ToInt32(022)))
if(Convert.ToBoolean(Convert.ToInt32('\xeb')))
Console.WriteLine("java");
else;
else;
else;
}
oxB: hexadecimal integer constant.
022: It octal integer constant.
‘\xeb’: It is hexadecimal character constant.
As,zero is false and any non-zero number is true. All,constants return a non-zero value. So, all if conditions in the above program are true.
Output: java.
Select the output for the following set of Code :
staticvoid Main(string[] args)
{
int a =5, b =10;
if(Convert.ToBoolean(Convert.ToInt32(++a))||Convert.ToBoolean(Convert.ToInt32(++b)))
{
Console.WriteLine(a +"\n"+ b);
}
else
Console.WriteLine(" C# ");
}
Consider the following expression:( ++a || ++b). In this expression || is ‘Logical OR operator’. Two important properties of this operator are:
Property 1:
(Expression1) || (Expression2)
|| operator returns 0 if and only if both expressions return a zero otherwise || operator returns 1.
initial value of a is 5. So ++a will be 6. Since ++a is returning a non-zero so ++b will not execute.
Output : 6 10.