C# Programming Questions and Answers Part-6

1. What will be the output of the following C# code?
static void Main(string[] args)
{
byte varA = 10;
byte varB = 20;
long result = varA & varB;
Console.WriteLine("{0} AND {1} Result :{2}", varA, varB, result);
varA = 10;
varB = 10;
result = varA & varB;
Console.WriteLine("{0} AND {1} Result : {2}", varA, varB, result);
Console.ReadLine();
}
a) 0, 20
b) 10, 10
c) 0, 10
d) 0, 0

Answer: c
Explanation: When ‘OR’ operations is done on the binary values following are the results of OR.
‘OR’ means addition(+) operation.
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.

2. What will be the output of the following C# code?
public static void Main()
{
byte varA = 10;
byte varB = 20;
long result = varA | varB;
Console.WriteLine("{0} OR {1} Result :{2}", varA, varB, result);
varA = 10;
varB = 10;
result = varA | varB;
Console.WriteLine("{0} OR {1} Result : {2}", varA, varB, result);
}
a) 20, 10
b) 30, 10
c) 10, 20
d) 10, 10

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 :
10 OR 20 Result :30.
10 OR 10 Result :10.

3. What will be the output of the following C# code?
static void Main(string[] args)
{
byte b1 = 0 * AB;
byte b2 = 0 * 99;
byte temp;
temp = (byte) ~b2;
Console.Write( temp + " ");
temp = (byte) (b1 << b2);
Console.Write(temp + " ");
temp = (byte)(b2 >> 2);
Console.WriteLine(temp);
Console.ReadLine();
}
a) 101 0 34
b) 103 2 38
c) 102 0 38
d) 101 1 35

Answer: c
Explanation: Output: 102 0 38.

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

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

5. What will be the output of the following C# code?
bool a = true;
bool b = false;
a |= b;
Console.WriteLine(a);
Console.ReadLine();
a) 0
b) 1
c) True
d) False

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

6. Select the relevant C# code set to fill up the blank for the following C# program?
static void Main(string[] args)
{
int x = 10, y = 20;
int res;
/*_______________*/
Console.WriteLine(res);
}
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

Answer: b
Explanation: 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?
static void Main(string[] args)
{
int y = 5;
int x;
int k = (!(Convert.ToInt32(y) > 10))? x = y + 3 : x = y + 10;
Console.WriteLine(x);
Console.WriteLine(y);
Console.ReadLine();
}
a) 5, 8
b) 10, 4
c) 8, 5
d) 11, 8

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.

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

Answer: c
Explanation: By definition

9. What will be the output of the following C# code?
public static void Main(string[] args)
{
int a = 4;
int c = 2;
bool b = (a % c == 0 ? true : false);
Console.WriteLine(b.ToString());
if (a/c == 2)
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
Console.ReadLine();
}
a) True
False
b) False
True
c) True
True
d) False
False

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.

10. Arrange the operators in the increasing order as defined in C#.
!=, ?:, &, ++, &&
a) ?: < && < != < & < ++
b) ?: < && < != < ++ < &
c) ?: < && < & <!= < ++
d) ?: < && < != < & < ++

Answer: c
Explanation: ?: < && < & <!= < ++