C# Operators MCQ

1. What is the output of the following expression in C#?

5 + 3 * 2
a) 16
b) 11
c) 8
d) 13

Answer:

b) 11

Explanation:

According to the order of operations in C#, multiplication is performed before addition. So, 3 * 2 is calculated first, making the expression 5 + 6, which equals 11.

2. In C#, which operator is used for concatenating two strings?

a) +
b) &
c) .
d) *

Answer:

a) +

Explanation:

In C#, the '+' operator is used to concatenate two strings.

3. What is the purpose of the '??' operator in C#?

a) It checks if the value is null and returns an alternative if true.
b) It adds two nullable types.
c) It compares two values for equality.
d) It performs a logical OR operation.

Answer:

a) It checks if the value is null and returns an alternative if true.

Explanation:

The '??' operator in C# is the null-coalescing operator, used to return the left-hand operand if it is not null, or the right-hand operand otherwise.

4. Which operator in C# is used to check type compatibility?

a) is
b) typeof
c) as
d) ==

Answer:

a) is

Explanation:

The 'is' operator in C# is used to check if an object is compatible with a given type.

5. What does the following C# code return?

int x = 10;
int result = x++;
a) 10
b) 11
c) 9
d) 12

Answer:

a) 10

Explanation:

The post-increment operator (x++) increases the value of x after its current value has been assigned to result. So, result gets the value 10.

6. Which of the following is the correct use of the conditional (ternary) operator in C#?

a) int result = (x > 10) ? x : 10;
b) int result = x > 10 ? x : 10;
c) int result = if(x > 10) x; else 10;
d) int result = x > 10 : x ? 10;

Answer:

b) int result = x > 10 ? x : 10;

Explanation:

The correct syntax for the ternary operator in C# is 'condition ? true_expression : false_expression'.

7. What is the purpose of the '%' operator in C#?

a) Exponentiation
b) Division
c) Modulus
d) Multiplication

Answer:

c) Modulus

Explanation:

The '%' operator in C# is used to find the remainder after division of one number by another.

8. In C#, which operator is used for bitwise AND operation?

a) &&
b) &
c) ||
d) |

Answer:

b) &

Explanation:

The '&' operator in C# is used for bitwise AND operations.

9. What will be the result of the expression (true || false) in C#?

a) true
b) false
c) null
d) 1

Answer:

a) true

Explanation:

The logical OR operator '||' returns true if either of its operands is true. Here, since one operand is true, the result is true.

10. Which operator in C# is used to explicitly convert a type?

a) as
b) is
c) ()
d) typeof

Answer:

c) ()

Explanation:

In C#, parentheses are used for explicit type conversion, also known as casting.

11. How does the 'as' operator differ from casting in C#?

a) 'as' operator performs a deep copy of the object.
b) 'as' operator returns null if the conversion fails, whereas casting throws an exception.
c) There is no difference.
d) 'as' operator is used for value types only.

Answer:

b) 'as' operator returns null if the conversion fails, whereas casting throws an exception.

Explanation:

The 'as' operator in C# is used for safe type conversion. It returns null if the conversion cannot be performed, unlike explicit casting which throws an exception.

12. What is the precedence of the logical AND ('&&') operator compared to the logical OR ('||') operator in C#?

a) '&&' has higher precedence than '||'.
b) '||' has higher precedence than '&&'.
c) Both have the same precedence.
d) The precedence is determined at runtime.

Answer:

a) '&&' has higher precedence than '||'.

Explanation:

In C#, the logical AND operator ('&&') has higher precedence than the logical OR operator ('||').

13. Which of the following operators can be overloaded in C#?

a) +
b) ?:
c) &&
d) ??

Answer:

a) +

Explanation:

In C#, operators like '+' can be overloaded, but conditional (?:), logical AND (&&), and null-coalescing (??) operators cannot be.

14. What will be the result of the expression (5 & 3) in C#?

a) 6
b) 1
c) 7
d) 0

Answer:

b) 1

Explanation:

The bitwise AND of 5 (0101 in binary) and 3 (0011 in binary) is 0001 in binary, which is 1 in decimal.

15. What is the output of the following C# code?

int a = 10;
int b = 3;
int result = a / b;
a) 3.3333
b) 3
c) 3.3
d) 4

Answer:

b) 3

Explanation:

In C#, the division of two integers results in an integer. The fractional part is discarded, so 10 / 3 gives 3.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top