C# Questions & Answers – Bitwise and Conditional Operators

This set of C# Multiple Choice Questions & Answers focuses on “Bitwise and Conditional Operators”.

1. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      byte varA = 10;
  4.      byte varB = 20;
  5.      long result = varA & varB;
  6.      Console.WriteLine("{0}  AND  {1} Result :{2}", varA, varB, result);
  7.      varA = 10;
  8.      varB = 10;
  9.      result = varA & varB;
  10.      Console.WriteLine("{0}  AND  {1} Result : {2}", varA, varB, result);
  11.      Console.ReadLine();
  12.  }

a) 0, 20
b) 10, 10
c) 0, 10
d) 0, 0
View Answer

Answer: c
Explanation: When ‘OR’ operations is done on the binary values following are the results of OR.
‘OR’ means addition(+) operation.

advertisement
advertisement
                 0 (false) +   0(false)  =   0 (false)
	         1 (True)  +   0(false)  =   1 (True)
	         0(false)  +   1(True)   =   1 (True)
	         1(True)   +   1(True)   =   1 (True)

When using OR operation it gives FALSE only when both the values are FALSE. In all other cases ‘OR’ operation gives ‘true’.
Output :

          10  AND  20 Result :0.
          10  AND  10 Result :10.

advertisement

2. What will be the output of the following C# code?

  1.  public static void Main() 
  2.  {
  3.      byte varA = 10;
  4.      byte varB = 20;
  5.      long result = varA | varB; 
  6.      Console.WriteLine("{0}  OR  {1} Result :{2}", varA, varB, result);
  7.      varA = 10;
  8.      varB = 10;
  9.      result = varA | varB;  
  10.      Console.WriteLine("{0}  OR  {1} Result : {2}", varA, varB, result);
  11.  }

a) 20, 10
b) 30, 10
c) 10, 20
d) 10, 10
View Answer

Answer: b
Explanation: There are two kinds of Shift operations “Right Shift” and “Left Shift”. Right Shift operation is used for shifting the bit positions towards right side. Left Shift operation is used for shifting the bit positions towards left side. When Right Shift operations are done on a binary value the bits are shifted one position towards the right.
Output :

advertisement
        10  OR  20 Result :30.
        10  OR  10 Result :10.

3. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      byte b1 = 0 * AB;
  4.      byte b2 = 0 * 99;
  5.      byte temp;
  6.      temp = (byte) ~b2;
  7.      Console.Write( temp + " ");
  8.      temp = (byte) (b1 << b2);
  9.      Console.Write(temp + " ");
  10.      temp = (byte)(b2  >> 2);
  11.      Console.WriteLine(temp);
  12.      Console.ReadLine();
  13.  }

a) 101 0 34
b) 103 2 38
c) 102 0 38
d) 101 1 35
View Answer

Answer: c
Explanation: None.
Output:

102 0 38.

4. Which of the following options is not a Bitwise Operator in C#?
a) &, |
b) ^, ~
c) <<, >>
d) +=, -=
View Answer

Answer: d
Explanation: +=, -= are Assignment Operators in C#.

5. What will be the output of the following C# code?

  1.   bool a = true;
  2.   bool b = false;
  3.   a |= b;
  4.   Console.WriteLine(a);
  5.   Console.ReadLine();

a) 0
b) 1
c) True
d) False
View Answer

Answer: c
Explanation: ‘bools’ are single bits, and so a bit-wise OR is the same as a logical OR.
Output :

True.

6. Select the relevant C# code set to fill up the blank for the following C# program?

  1.   static void Main(string[] args)
  2.   {
  3.       int x = 10, y = 20;
  4.       int res;
  5.       /*_______________*/ 
  6.       Console.WriteLine(res);
  7.   }

a) x % y == 0 ? (x == y ? (x += 2):(y = x + y)):y = y*10;
b) x % y == 0 ? y += 10:(x += 10);
c) x % y == 0 ? return(x) : return (y);
d) All of the mentioned
View Answer

Answer: b
Explanation: None.
Output :

         {
              int x = 10, y = 20;
              int res;
              x % y == 0 ? y += 10:(x += 10);
              Console.WriteLine(res);
          }

7. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      int y = 5;
  4.      int x;
  5.      int k = (!(Convert.ToInt32(y) > 10))?  x = y + 3 : x = y + 10;
  6.      Console.WriteLine(x);
  7.      Console.WriteLine(y);
  8.      Console.ReadLine();
  9.  }

a) 5, 8
b) 10, 4
c) 8, 5
d) 11, 8
View Answer

Answer: c
Explanation: Since condition y > 10 is false and !(false) = true. So, first statement x = y + 3 is executed which is x = 8 with y = 5.
Output:

8, 5.

8. Which among the following is a conditional operator?
a) ‘:?’
b) ?;
c) ?:
d) ??
View Answer

Answer: c
Explanation: By definition.

9. What will be the output of the following C# code?

  1. public static void Main(string[] args)
  2. {
  3.     int a = 4;
  4.     int c = 2;
  5.     bool b = (a % c == 0 ? true : false);
  6.     Console.WriteLine(b.ToString());
  7.     if (a/c == 2)
  8.     {
  9.         Console.WriteLine("true");
  10.     }
  11.     else
  12.     {
  13.         Console.WriteLine("false");
  14.     }
  15.     Console.ReadLine();
  16. }

a)

True
False

b)

False
True

c)

True
True

d)

False
False
View Answer
Answer: c
Explanation: a % c == 0 condition is true as (4 % 2 == 0). So, b is evaluated as true. Now (a/c == 2) which means if condition is also true hence it is evaluated as true.
Output:

        True
        True
 
 

10. Arrange the operators in the increasing order as defined in C#.

    !=,   ?:,   &,   ++,  &&

a) ?: < && < != < & < ++
b) ?: < && < != < ++ < &
c) ?: < && < & <!= < ++
d) ?: < && < != < & < ++
View Answer

Answer: c
Explanation: By definition.

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.