C# Questions & Answers – Relational and Logical Operators

«
»

This section of our 1000+ C# multiple choice questions and answers focuses on relational and logical operators in C# Programming Language.

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

1. ` static void Main(string[] args)`
2. ` {`
3. `     int a = 4;`
4. `     int b = 5;`
5. `     int c = 6;`
6. `     int d = 8;`
7. `     if (((a * b / c) + d) >= ((b * c + d ) / a))`
8. `     {`
9. `         Console.WriteLine("Line 1 - a is greater to b");`
10. `         Console.WriteLine((a * b / c) + d);`
11. `     }`
12. `     else`
13. `     {`
14. `         Console.WriteLine("Line 1 - a is not greater to b");`
15. `         Console.WriteLine((b * c + d )/ a);`
16. `     }`
17. ` }`

a)

```"Line 1 - a is  greater to b"
11```

b)

Note: Join free Sanfoundry classes at Telegram or Youtube
```"Line 1 - a is not greater to b"
9```

c) Both are equal
d) None of the mentioned

Explanation: Now, here in ‘if’ condition both conditions of parenthesis and hence evaluating operators based on parenthesis are tested.
for expression :

``` ((a * b / c) + d)
Step 1: (a*b/c) (Evaluating as 4*5/6 = 3)
Step 2: ( (a*b/c) + d ) (Evaluating (3 + 8 = 11))
Result: 11
for expression : (b * c + d )/ a
Step 1: (b*c + d) (Evaluating as 5*6 +8 = 38)
Step 2:   (b*c + d) / a (Evaluating as 38 / 4 = 9)
Result: 9```

The relational operator “>=” between both expressions check for largest figure and hence consecutively executes the if condition.
Output:

Take C# Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
```         Line 1 - a is greater to b.
11```

2. Check the following C# code whether the given relation operator works according to the if condition or not.

1. ` static void Main(string[] args)`
2. ` {`
3. `     int a = 10;`
4. `     int b = 5;`
5. `     int c = 12; `
6. `     int e = 8;`
7. `     int d;`
8. `     d = Convert.ToInt32((a * (c - b) / e + (b + c)) <= (e * (c + a) / (b + c) + a));`
9. `     Console.WriteLine(d);`
10. `     if (d == 1)`
11. `     {`
12. `         Console.WriteLine("C# is great language!");`
13. `         Console.WriteLine((a * (c - b) / e + (b + c)));`
14. `     }`
15. `     else`
16. `     {`
17. `         Console.WriteLine("harsh is not great language!");`
18. `         Console.WriteLine((e * (c + a) / (b + c) + a));`
19. `     }`
20. ` }`

a)

```0
C# is great!
20```

b)

```0
C# is not great!
25```

c)

```0
C# is great!
25```

d)

```0
C# is not great!
20```
Explanation: The expression (a * (c – b) / e + (b + c)) on evaluation parenthesis by parenthesis gives result mathematically as 25. Similarly, (e * (c + a) / (b + c) + a) on evaluation parenthesis by parenthesis gives mathematically result as 20. Relational operator now checks for condition as in if condition as (25 < 20) which is false. So, a false bit in form of ‘0’ is assigned to d. Now, in if condition (d != 1) as d = 0. So, condition after else is evaluated.
Output :

```        0.
C# is not great!.
20.```

3. Which of the following is/are not Relational operators in C#.NET?
a) >=
b) <>=
c) Not
d) <=

Explanation: By definition.

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

1. `    int n = 2;`
2. `    int p = 4;`
3. `    int q = 5;`
4. `    int w = 3;`
5. `    if ( !((p * q) /n <= (q * w) + n/p ))`
6. `    {`
7. `        Console.WriteLine( ++p + w++ + " " + ++n);`
8. `        Console.WriteLine("b");`
9. `    }`
10. `    else`
11. `    {`
12. `        Console.WriteLine(--p + q-- + " " + --n);`
13. `        Console.WriteLine("a");`
14. `    }`

a)

```6 2
b```

b)

```8 1
a```

c)

```6 1
a```

d)

```8 1
b```
Explanation: After evaluation of the test expression (!((p*q)/n <= (q*w)+n/p)). The use of logical operator(!) turns false(0) result to bit ‘1’ and hence the condition evaluated by ‘if’ loop is after else as :
–p = 3
q–= 5
–p + q– = 8 where now value of ‘q’is 4.
–n = 2 – 1 =1.
So, values after evaluations are 8 1.
a.
Output :

```         8 1
a```

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

```    m = 5;
int y;
1. y = m++;
2. y = ++m;```

a) y = 5, m = 6 ; y = 5, m = 5
b) y = 6, m = 6; y = 7, m = 6
c) y = 5, m = 6; y = 7, m = 7
d) y = 5, m = 6; y = 7, m = 8

Explanation:

```              step 1 :  m = 5, y = m++ i.e y =5, m =6.
step 2 :  y = ++m, Since m = 6. So, m = 7 on ++m and hence y = 7.
Output : y = 5, m = 6; y =7, m = 7.```

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

1. `  static void Main(string[] args)`
2. `  {`
3. `      int a = 3, b = 5, c = 1;`
4. `      int z = ++b;`
5. `      int y = ++c;`
6. `      b = Convert.ToInt32((Convert.ToBoolean(z)) && (Convert.ToBoolean(y)) `
7. `       || Convert.ToBoolean(Convert.ToInt32(!(++a == b))));`
8. `      a = Convert.ToInt32(Convert.ToBoolean(c) || Convert.ToBoolean(a--));`
9. `      Console.WriteLine(++a);`
10. `      Console.WriteLine(++b);`
11. `      Console.WriteLine(c);`
12. `  }`

a) 2, 2, 1
b) 2, 3, 2
c) 2, 2, 2
d) 2, 0, 9

Explanation: z = 6 as ++b.
y = 2 as ++c.
6 && 2 = 1
(++a == b) which is false as 4!=6. Now, !(false) = true i.e 1.
So, 1 || 1 = 1. So, b = 1.
Similarly, c = 2 and a = 4. Now, 2 || 4 = 1.
So, a = 1.
Hence ++a = 2, ++b = 2, c = 2.
Output :

`2, 2, 2`

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

1. ` static void Main(string[] args)`
2. ` {`
3. `     int a = 4, b = 5, c = 7, u = 9;`
4. `     int h;`
5. `     h = (Convert.ToInt32(u < b)) + (a + b--) + 2;`
6. `     Console.WriteLine(h);`
7. `     Console.WriteLine(b);`
8. `     Console.WriteLine(u < b);`
9. ` }`

a) 12, 5, 0
b) 11, 4, False
c) 11, 5, 0
d) 12, 4, False

Explanation: Step 1: Convert.ToInt32(u < b)(Evaluate result as 9 < 5 which is false in nature. So, solution is converted from ‘false’ to ‘0’).

```             Step 2: (a + b--) evaluated as 4 + 5 = 9 + 2 = 11.
Step 3: u &lt; b evaluated as 'False' without being converted to '0'.
Output: 11
4
False.```

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

1. ` static void Main(string[] args)`
2. ` {`
3. `     int m = 10, n = 5, p = 20;`
4. `     bool b1 =  m * p / n <= p * n / m ;`
5. `     int l = p - 2 * m;`
6. `     bool b2 = l == 0;`
7. `     int z = Convert.ToInt32(b2);`
8. `     int k = Convert.ToInt32(b1);`
9. `     Console.WriteLine(k);`
10. `     Console.WriteLine(z);`
11. ` }`

a) 0 0
b) 1 0
c) 0 1
d) 1 1

Explanation: Solving the expression for b1 tests the condition for either true or false result in ‘0’. Similarly, for ‘b2’ ‘l’ on solving gives ‘0’. So, condition is true for bool b2 as 0 == 0. Hence, k = 0 and z = 1.
Output :

`0 1.`

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

1. ` class method1`
2. ` {`
3. `     public int fun(int m)`
4. `     {`
5. `         return( m++ % 10);`
6. `     }`
7. ` }`
8. ` class Program`
9. ` {`
10. `     static void Main(string[] args)`
11. `     {`
12. `         int a = 23, b = 0, c;`
13. `         method1 z = new method1();`
14. `         c = z.fun (++b * --a % 2);`
15. `         int d = (z.fun (c-- + --a));`
16. `         Console.WriteLine(c);`
17. `         Console.WriteLine(a++);`
18. `         Console.WriteLine(d);`
19. `         Console.ReadLine();`
20. `     }`
21. ` }`

a) -1, 22, 0
b) -1, 21, 1
c) 0, 22, 1
d) 0, 22, 0

Explanation: Here, for first value of c, ++b = 1 and 1 * (22%2) = 0. c = 0. Now c — = 0 and — a = 22 – 1 =21. Now, c — is the first condition executed and then decremented So, c = -1. Similarly, a++ = 21. Now, as we can see from options we are confirmed over value of c = -1, a = 21. So, we can easily know that d = 1.
Output:

`-1 21 1`

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

1. `  static void Main(string[] args)`
2. `  {`
3. `      int a = 8, b = 6, c = 10;`
4. `      int d = a * c * 2 / Convert.ToInt32(Math.Pow ((c - b), 2));`
5. `      if (d == (c = Convert.ToInt32(Math.Sqrt (a * a + b * b))) && c == 10)`
6. `      {`
7. `          Console.WriteLine("figure is hypotenuse");`
8. `      }`
9. `      else`
10. `      {`
11. `         Console.WriteLine("figure is square");`
12. `      }`
13. `  }`

a) Figure is square
b) Figure is hypotenuse
c) False
d) None of the mentioned

Explanation: Solving the expression for ‘c’ we get c==10 in if first condition as (c == Convert.ToInt32(Math.Sqrt(a * a + b * b))). The logical condition when d == (c = 10) suits here. Similarly, going for second condition where c ==10 as ‘&&’ operator exists between both given condition and at last both are evaluated to true as c == 10. So, only first statement is executed.
Output :

`Figure is square`

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.