C# Questions & Answers on IF Statements for Freshers

1. Select the output for the following set of code :
  1.  static void Main(string[] args)
  2.  {
  3.      int i = 30;
  4.      int j = 25 % 25;
  5.      if (Convert.ToBoolean(Convert.ToInt32(i = j)))
  6.      {
  7.          Console.WriteLine("In if");
  8.      }
  9.      else
  10.      {
  11.          Console.WriteLine("In else");
  12.      }
  13.      Console.WriteLine("In main");
  14.      Console.ReadLine();
  15.  }
a) In if
b) In else
c) In if
In main
d) In else
In main
Answer: d

Explanation: Usage of ‘=’ operator instead of ‘==’ operator .hence,the condition is not true.
Output: In else
In main
2. Select output for the following set of Code:
  1.  static void Main(string[] args)
  2.  {
  3.      int i;
  4.      int b = 8, a = 32;
  5.      for (i = 0; i <= 10; i++)
  6.      {
  7.          if ((a / b * 2)== 2)
  8.          {
  9.              Console.WriteLine( i + " ");
  10.              continue;
  11.          }
  12.          else if (i != 4)
  13.              Console.Write(i + " ");
  14.          else
  15.              break;
  16.     }
  17.     Console.ReadLine();
  18.  }
a) 1 2 3 4 5 6 7 8 9
b) 0 1 2 3 4 5 6 7 8
c) 0 1 2 3
d) 0 1 2 3 4
Answer: c

Explanation: The if condition will never be fulfilled as ((a / b) * 2 == 2) is never true.Hence,only else part of condition will be executed until i! = 4 i.e i = 0,1 ,2 ,3.
Output:0 1 2 3
3. Select the output for the following set of Code:
  1.  static void Main(string[] args)
  2.  {
  3.      Console.WriteLine("Enter a letter:");
  4.      char c = (char)Console.Read();
  5.      if (Char.IsDigit(c) == true)
  6.          Console.WriteLine("A number");
  7.      else if (char.IsLower(c) == true)
  8.          Console.WriteLine("A lower case letter");
  9.      else if (char.IsUpper(c) == true)
  10.          Console.WriteLine("An upper case letter");
  11.      Console.ReadLine();
  12.  }
  13.  1. Enter a letter :
  14.     a
  15.     An upper case letter
  16.  2. Enter a letter :
  17.     A
  18.     An upper case letter
  19.  3. Enter a letter :
  20.     2
  21.     A number
  22.  4. Enter a letter :
  23.     2
  24.     A lower case letter.
Which of the following conditions are true ?
a) a ,b ,c
b) b ,c ,d
c) a ,d ,b
d) b ,c
Answer: d

Output:Enter a letter :
A
An upper case letter
Enter a letter :
2
A number
4. Select the output for the following set of code :
  1.   static void Main(string[] args)
  2.   {
  3.       int i, j;
  4.       for (i = 2; i >= 0; i--)
  5.       {
  6.           for (j = 0; j <= 2; j++)
  7.           {
  8.               if (i == j)
  9.               {
  10.                   Console.WriteLine("1");
  11.               }
  12.               else
  13.               {
  14.                   Console.WriteLine("0");
  15.               }
  16.          }
  17.          Console.WriteLine("\n");
  18.          Console.ReadLine();
  19.       }
  20.   }
a) 1 0 0
0 1 0
0 0 1
b) 0 1 0
1 0 0
0 0 1
c) 0 0 1
0 1 0
1 0 0
d) 1 0 0
0 0 1
0 1 0
Answer: c

Explanation: In first row for i = 2 : j = 0 == 0 as if condition fails for (i == j)
i = 2 : j = 1 == 0 as again if condition fails for ( i == j)
i = 2 : j = 2 == 1 as (i == j).
In Second row for i = 1 : j = 0 == 0 as if condition fails for (i == j)
i = 1 : j = 1 == 1 (as i==j)
i = 1 : j = 2 == 0 as (i==j) not true
In Third row for i = 0 : j = 0 == 1 as (i==j) true
i = 0 : j = 1 == 0 as (i==j) not true.
i = 0 : j = 2 == 0 .
So, 0 0 1
0 1 0
1 0 0
Output: 0 0 1
0 1 0
1 0 0
5. Select the correct ‘if statement’ to be filled in the given set of code :
  1.  static void Main(string[] args)
  2.  {
  3.      int []num = {50, 65, 56, 88, 43, 52};
  4.      int even = 0, odd = 0;
  5.      for (int i = 0 ;i < num.Length ;i++)
  6.      {
  7.           /*___________________________*/
  8.      }   
  9.      Console.WriteLine("Even Numbers:" +even);
  10.      Console.WriteLine("Odd Numbers:" +odd);
  11.      Console.ReadLine();
  12.  }
a)
  1. if ((num % 2) == 0)
  2.    {
  3.        even += 1;
  4.    }
  5.    else
  6.    {
  7.        odd += 1;
  8.    }
b)
  1.  if((num * i) == 0)
  2.    {
  3.        even += 1;
  4.    }
  5.    else
  6.    {
  7.        odd += 1;
  8.    }
c)
  1. if(num[i] % 2 == 0)
  2.     {
  3.         even += 1;
  4.     }
  5.     else
  6.     {
  7.         odd += 1;
  8.     }
d)
  1.  if(num[i] % 2 = 0)
  2.     {
  3.         even += 1;
  4.     }
  5.     else
  6.     {
  7.         odd += 1;
  8.     }

Answer: c

Explanation: 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();
6. What is the output for the following code ?
  1.  static void Main(string[] args)
  2.  {
  3.      int a = 15, b = 10, c = 1;
  4.      if (Convert.ToBoolean(a) && (b > c))
  5.      {
  6.          Console.WriteLine("cquestionbank");
  7.      }
  8.      else
  9.      {
  10.          break;
  11.      }
  12.  }
a) cquestionbank
b) It will print nothing
c) Compile time error
d) Run time error
Answer: c

Explanation: Keyword “break” is not part of if-else statement.This keyword is used in case of loop or switch case statement.
7. What is the output for the following code ?
  1.   static void Main(string[] args)
  2.   {  
  3.       int a = 5;
  4.       if (Convert.ToBoolean((.002f) -(0.1f)))
  5.       Console.WriteLine("Sachin Tendulkar");
  6.       else if (a == 5)
  7.       Console.WriteLine("Rahul Dravid");
  8.       else
  9.       Console.WriteLine("Ms Dhoni");
  10.       Console.ReadLine();
  11.   }
a) Rahul Dravid
b) Sachin Tendulkar
c) Ms Dhoni
d) Warning : Unreachable Code
Answer: b

Explanation: (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
8. Select the output for the following set of Code :
  1.   static void Main(string[] args)
  2.   {   
  3.       int a = -1;
  4.       int b = -1;
  5.       if (Convert.ToBoolean (++a = ++b))
  6.       Console.WriteLine("a");
  7.       else
  8.       Console.WriteLine("b");
  9.       Console.ReadLine();
  10.   }
a) a
b) b
c) Compile time error
d) Code execute successfully with no output
Answer: c

Explanation: Both a and b are constants.Illegal to assign a value to constant on left hand of ‘=’operator .Hence,it must be some variable.
Output: static void Main(string[] args)
{
int a = -1;
int b = -1;
if (Convert.ToBoolean(++a == ++b))
Console.WriteLine(“a”);
else
Console.WriteLine(“b”);
Console.ReadLine();
}
9. Select the output for the following set of Code :
  1.   static void Main(string[] args)
  2.   {
  3.       int a = 5, b = 10;
  4.       if (Convert.ToBoolean(Convert.ToInt32(0xB)))
  5.       if (Convert.ToBoolean(Convert.ToInt32(022)))
  6.       if (Convert.ToBoolean(Convert.ToInt32('\xeb')))
  7.       Console.WriteLine("java");
  8.       else ;
  9.       else ;
  10.       else ;
  11.   }
a) Compile time error: Misplaced else
b) Compile time error: Undefined symbol
c) java
d) Warning: Condition is always true
Answer: c

Explanation: 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.
10. Select the output for the following set of Code :
  1.  static void Main(string[] args)
  2.  {
  3.      int a = 5, b = 10;
  4.      if (Convert.ToBoolean(Convert.ToInt32(++a)) || Convert.ToBoolean(Convert.ToInt32(++b)))
  5.      {
  6.          Console.WriteLine(a + "\n" + b);
  7.      }
  8.      else
  9.      Console.WriteLine(" C# ");
  10.  }
a) 6 11
b) 6 16
c) 6 12
d) 6 10
Answer: d

Explanation: 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.

Related

Multiple Choice Questions and Answers on SaaS of Cloud Computing

1. Which of the following is most complete cloud computing service model ? a) PaaSb) IaaSc) CaaSd) SaaS Answer: d Explanation: The most complete cloud computing service model is one in which the ...

Multiple choice Questions and Answers on PaaS of Cloud Computing

1. ______ offering provides the tools and development environment to deploy applications on another vendor’s application. a) PaaSb) IaaSc) CaaSd) All of the mentioned Answer: b Explanation: PaaS ...

Multiple Choice Questions and Answers on IaaS of Cloud Computing for Freshers

1. Which of the following is a virtual machine technology now owned by Oracle that can run various operating systems ? a) Vmachinesb) VirtualBoxc) ThoughtPoliced) None of the mentioned Answer: b ...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item