C# Programming - Operators

Exercise : Operators - General Questions
  • Operators - General Questions
6.
Which of the following statements is correct about the C#.NET code snippet given below?
int d; 
d = Convert.ToInt32( !(30 < 20) );
A value 0 will be assigned to d.
A value 1 will be assigned to d.
A value -1 will be assigned to d.
The code reports an error.
The code snippet will work correctly if ! is replaced by Not.
Answer: Option
Explanation:

Sample Program:

bool falseFlag = false;
bool trueFlag = true;

Console.WriteLine("{0} converts to {1}.", falseFlag,
                  Convert.ToInt32(falseFlag));
Console.WriteLine("{0} converts to {1}.", trueFlag,
                  Convert.ToInt32(trueFlag));
The example displays the following output:

       False converts to 0.
       True converts to 1.

7.
Which of the following is the correct output for the C#.NET code snippet given below?
Console.WriteLine(13 / 2 + " " + 13 % 2); 
6.5 1
6.5 0
6 0
6 1
6.5 6.5
Answer: Option
Explanation:
No answer description is available. Let's discuss.

8.
Which of the following statements are correct about the Bitwise & operator used in C#.NET?
  1. The & operator can be used to Invert a bit.
  2. The & operator can be used to put ON a bit.
  3. The & operator can be used to put OFF a bit.
  4. The & operator can be used to check whether a bit is ON.
  5. The & operator can be used to check whether a bit is OFF.
1, 2, 4
2, 3, 5
3, 4
3, 4, 5
None of these
Answer: Option
Explanation:
No answer description is available. Let's discuss.

9.
Which of the following are Logical operators in C#.NET?
  1. &&
  2. ||
  3. !
  4. Xor
  5. %
1, 2, 3
1, 3, 4
2, 4, 5
3, 4, 5
None of these
Answer: Option
Explanation:
No answer description is available. Let's discuss.

10.
Suppose n is a variable of the type Byte and we wish, to check whether its fourth bit (from right) is ON or OFF. Which of the following statements will do this correctly?
if ((n&16) == 16)
Console.WriteLine("Fourth bit is ON");
if ((n&8) == 8)
Console.WriteLine("Fourth bit is ON");
if ((n ! 8) == 8)
Console.WriteLine("Fourth bit is ON");
if ((n ^ 8) == 8)
Console.WriteLine("Fourth bit is ON");
if ((n ~ 8) == 8)
Console. WriteLine("Fourth bit is ON");
Answer: Option
Explanation:
byte myByte = 153; // In Binary = 10011001

byte n = 8; // In Binary = 00001000 
(Here 1 is the 4th bit from right)

Now perform logical AND operation (n & myByte)

 10011001
 00001000
---------
 00001000  Here result is other than 0, so evaluated to True.
---------

If the result is true, then we can understand that 4th bit is ON of the given data myByte.