C# Questions & Answers – Arithmetic Operators

«
»

This section of our 1000+ C# multiple choice questions focuses on arithmetic operators in C# Programming Language.

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

  1.  static void Main(string[] args)
  2.  {
  3.      float a = 16.4f;
  4.      int b = 12;
  5.      float c;
  6.      c =  a * ( b + a) / (a - b) ;
  7.      Console.WriteLine("result is :" +c);
  8.      Console.ReadLine();
  9.  }

a) 106
b) 104.789
c) 105.8546
d) 103.45
View Answer

Answer: c
Explanation: The first expression evaluated is ‘b+a’ as both are combined. Next the expression is multiplied by operand ‘a’ i.e a (b+a) the whole result of numerator is combined and divided by denominator expression (a – b).
Output:

advertisement
 result is : 105.8546.

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

advertisement
advertisement
  1.   static void Main(string[] args)
  2.   {
  3.       int a, b, c, x;
  4.       a = 90;
  5.       b = 15;
  6.       c = 3;
  7.       x = a - b / 3 + c * 2 - 1;
  8.       Console.WriteLine(x);
  9.       Console.ReadLine();
  10.   }

a) 92
b) 89
c) 90
d) 88
View Answer

Answer: c
Explanation: The basic evaluation process includes two left to right passes through the expression. During first pass, the high priority operators are applied and during second pass, the low priority operators are applied as they are encountered.

advertisement
First pass :
       step 1 : x = 90 - 15 / 3 + 3 * 2 - 1 (15 / 3 evaluated)
       step 2 : x = 90 - 5 + 3 * 2 - 1
       step 3 : x = 90 - 5 + 3 * 2 -1 (3 * 2 is evaluated)
       step 4 : x = 90 - 5 + 6 - 1
 Second pass :
       step 5 : x = 85 + 6 - 1 (90 - 5 is evaluated)
       step 6 : x = 91 - 1(85 + 6 is evaluated)
       step 7 : x = 90(91 - 1 is evaluated)
Output : 90.

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

advertisement
  1.   static void Main(string[] args)
  2.   {
  3.       int a, b, c, x;
  4.       a = 80;
  5.       b = 15;
  6.       c = 2;
  7.       x = a - b / (3 * c) * ( a + c);
  8.       Console.WriteLine(x);
  9.       Console.ReadLine();
  10.   }

a) 78
b) -84
c) 80
d) 98
View Answer

Answer: b
Explanation: Whenever the parentheses are used, the expressions within parentheses assumes higher priority. If Two or more sets of parentheses appear one after another as shown above, the expression contained on the left side is evaluated first and right hand side last.

advertisement
  First pass:
      Step 1: 80 - 15/(3*2)*(80 + 2)
      Step 2: 80 - 15/6*82 ((3 * 2) evaluated first and (80 + 2) evaluated later)
  Second pass:
      Step 3: 80 - 2*82
      Step 4: 80 - 164.
  Third pass:
     Step 5 : -84. (80 - 164 is evaluated)
Output : -84 .

4. Correct order of priorities are:
a) ‘/’ > ‘%’ > ‘*’ > ‘+’
b) ‘/’ > ‘*’ > ‘%’ > ‘+’
c) ‘*’ > ‘/’ > ‘%’ > ‘+’
d) ‘%’ > ‘*’ > ‘/’ > ‘+’
View Answer

Answer: c
Explanation: By definition.

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

  1.     int i, j = 1, k;
  2.     for (i = 0; i < 3; i++)
  3.     {
  4.         k = j++ - ++j;
  5.         Console.Write(k + " ");
  6.     }

a) -4 -3 -2
b) -6 -4 -1
c) -2 -2 -2
d) -4 -4 -4
View Answer

Answer: c
Explanation:

 Here i = 0, j = 1.
                     k = 1 - 3 ( j++ = 2 and ++j = 3)
                     k = -2.
                     i = 1 , j = 3.
                     k = 3 - 5 ( j++ = 4 and ++j = 5)
                     k = -2.
                     i = 2 , j = 5.
                     k = 5 - 7 (j++ = 6  and ++j = 7)
                     k = -2.
Output : -2 , -2 , -2.

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

  1.   static void Main(string[] args)
  2.   {
  3.       int b= 11;
  4.       int c = 7;
  5.       int r = 5;
  6.       int e = 2;
  7.       int l;
  8.       int v = 109;
  9.       int k;
  10.       int z,t,p;
  11.       z = b * c;
  12.       t = b * b;
  13.       p = b * r * 2;
  14.       l = (b * c) + (r * e) + 10;
  15.       k = v - 8;
  16.       Console.WriteLine(Convert.ToString(Convert.ToChar(z)) + " " + Convert.ToString(Convert.ToChar(t)) + Convert.ToString(Convert.ToChar(p)) +   Convert.ToString(Convert.ToChar(l)) + Convert.ToString(Convert.ToChar(v)) + Convert.ToString(Convert.ToChar(k)));                
  17.       Console.ReadLine();
  18.   }

a) My Name
b) My nAme
c) My name
d) Myname
View Answer

Answer: c
Explanation: Solving the expression l = (b * c) + (r * e) + 10. While from left to right the parentheses are given preference first.

 Step 1 : b * c is evaluated first inside first parentheses.
 Step 2 : r * e is evaluated second on right side of first addition symbol.
 Step 3 : After evaluating both parentheses 10 is added to value of both.
Output : My name.

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

  1.  static void Main(string[] args)
  2.  {
  3.      int n = 5;
  4.      int x = 4;
  5.      int z, c, k;
  6.      for (c = 1; c <= n; c++)
  7.      {
  8.          for (k = 1; k <= c; k++)
  9.      {
  10.          z = 3 * x * x + 2 * x + 4 / x + 8;
  11.          Console.Write(Convert.ToString(Convert.ToChar(z)));
  12.      }
  13.          Console.WriteLine("\n");
  14.      }
  15.      Console.ReadLine();
  16.  }

a)

A
AA
AAA
AAAA

b)

A
AB
ABC
ABCD

c)

A
AA
AAA
AAAA
AAAAA

d)

A
BC
DEF
DEFG
View Answer
Answer: c
Explanation: Solving the expression for value of ‘z’as 65. With, each passage of loop value number of ‘z’ increases for each row as

                           Row 1: A
                           Row 2: AA 
                                  -
                                  -
                           Row 5: AAAAA
 Output : A
          AA
          AAA
          AAAA
          AAAAA

 
 

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

  1.  static void Main(string[] args)
  2.  {
  3.      int n = 5;
  4.      int x = 4;
  5.      int z, c, k;
  6.      z = 3 * x * x + 2 * x + 4 / x + 8;
  7.      for (c = 1; c <= n; c++)
  8.      {
  9.          for (k = 1; k <= c; k++)
  10.          {
  11.              Console.Write(Convert.ToString(Convert.ToChar(z)));
  12.              z++;
  13.          }
  14.          Console.WriteLine("\n");
  15.      }
  16.      Console.ReadLine();
  17.  }

a)

A
AA
AAA
AAAA
AAAAA

b)

A
AB
ABC
ABCD
ABCDE

c)

A
BC
DEF
GHIJ
KLMNO

d)

A
AB
BC
BCD
BCDE
View Answer
Answer: c
Explanation: Solving expression ‘z’ value is 65. Before going inside first loop

Step 1: c = 1, n = 5
k = 1, k <= 1. (as c = 1)
z = 65 converted to ‘A’ as ascii value of ‘A’ is 65.
z++ increment for next loop condition by ‘1’as 66.
Row 1: A
Step 2: c = 2, n = 5
k = 2, k <= 2. (as c = 2)
z = 66 from step 1 converted value of 66 is ‘B’. Since, k < =2
loop will again loop to second value after 66 which is 67 as z is
incremented from 66 to +1 as ’67’. So, converting ascii value of 67 to character as ‘C’.
Row 2: B C
Similarly,
Step 3:
Row 3: D E F
Step 4:
Row 4: G H I J
Step 5:
Row 5: K L M N O.

Output:  A
         BC
         DEF
         GHIJ
         KLMNO

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

  1.   static void Main(string[] args)
  2.   {
  3.       int a , b;
  4.       int c = 10;
  5.       int d = 12;
  6.       int e = 5;
  7.       int f = 6;
  8.       a = c * (d + e) / f + d;
  9.       Console.WriteLine(a);
  10.       b = c * ( d + e / f + d);
  11.       Console.WriteLine(b);
  12.       if (a < b)
  13.       {
  14.           Console.WriteLine(" parentheses changes values");
  15.       }
  16.       else if (a > b)
  17.       {
  18.           Counterintelligence("they have same value");
  19.       }
  20.       Console.ReadLine();
  21.   }

a) They have same value
b) Parentheses changes values
c) Since both have equal values, no conclusion
d) None of the mentioned
View Answer

Answer: b
Explanation: Solving for expression ‘a’ expression inside parentheses are given preference evaluating (d+e) as 17.

a = 10 * 17/6 + 12.
a = 40.
Solving for expression 'b' expression inside parentheses (d + e /f + d) 
are evaluated as (12 + 5/6 + 12)
b = 10 *(12 + 5/6 + 12).
b = 240.
Output : 40
         240
         parentheses changes values.

10. The correct way of incrementing the operators are:
a) ++ a ++
b) b ++ 1
c) c += 1
d) d =+ 1
View Answer

Answer: c
Explanation: This += is known as short hand operator which is same as variable = variable +1. Similarly, a-= 1 is a = a-1, a*=1 is a = a * 1. They are used to make code short and efficient.

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.

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 & technical discussions at Telegram SanfoundryClasses.