Q:

C#.Net find output programs (Operators) | set 1

belongs to collection: C#.Net find output programs

0

Find the output of C#.Net programs | Operators | Set 1: Enhance the knowledge of C#.Net Operators concepts by solving and finding the output of some C#.Net programs.

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int a = 10;
            int b = 20;
            int c = 0;

            c = ++a + ++b * 2+a++;

            Console.WriteLine(a + "," + b + "," + c);
        }
    }
}

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            double Z = 0.0;

            Z = Math.Abs((int)Math.PI*-23)*Math.Pow(2,0);
           
            Console.WriteLine(Z);
        }
    }
}

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int Z = 0;

            Z += sizeof(decimal)+ (Math.pow(2,8)%3);
           
            Console.WriteLine(Z);
        }
    }
}

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int Z = 0;

            Z += sizeof(decimal)+ ((int)Math.Pow(2,8)%3);
           
            Console.WriteLine(Z);
        }
    }
}

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            const float A = 4.23F;
                  float B = 3.23F;
            float Z = 2;

            Z *= ++B+A%B;
           
            Console.WriteLine(Z);
        }
    }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1 :

Output:

12,21,64
Press any key to continue . . .

Explanation:

In the above program, we created three integer variables a, b, and c initialized with 10, 20, and 0 respectively. Here, we used pre and post-increment operators with variable a and b. The pre-increment operator will evaluate before the expression and post-increment operator will evaluate after the expression.

The value of variable a will be 11 and the value of b will be 21 because of pre-increment operator. Let's evaluate the expression,

c = ++a + ++b * 2+a++;

Now remove all pre and post-increment operators from the expression.

c = a +b * 2+a;
c = 11+21*2+11;
c = 11+42+11’
c = 64;

Then post-increment operator will evaluate for variable a then the final value of a will be 22.

Answer 2:

Output:

69
Press any key to continue . . .

Explanation:

In the above program, we created a variable Z initialized with "0.0", and in the expression, we used Abs() and Pow() methods and PI property of Math class.

The Abs() method is used to return positive value, Pow() method is used to calculate the power, and TPI property will return 3.14.

Let's evaluate the expression:

Z = Math.Abs((int)Math.PI*-23)*Math.Pow(2,0);
Z = Math.Abs(3*-23)*Math.Pow(2,0);
Z = Math.Abs(-69)*Math.Pow(2,0);
Z = 69*1;
Z = 69; 

Answer 3:

Output:

main.cs(12,41): error CS0117: `System.Math' does not contain a definition for `pow'
/usr/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error)

Explanation:

The above program will generate syntax error because pow() is not a built-in method. Because, Pow() is a built-in method of Math class. Because C# is a case sensitive language.

Answer  4:

Output:

17
Press any key to continue . . .

Explanation:

The above program will print 17 on the console screen. In the above program, we created an integer variable Z initialized with 0.

Z += sizeof(decimal)+ ((int)Math.Pow(2,8)%3);

In the above expression, we used the sizeof() operator, here sizeof(decimal) will return 16 and Math.Pow() will return 256, the Math.Pow() function returns the value of double type, then we will typecast value returned by Pow() method, and modulus '%' operator is used to find the remainder.

Let's evaluate the expression,

Z += sizeof(decimal)+ ((int)Math.Pow(2,8)%3);
Z += 16+ (256%3);
Z += 16+ (256%3);
Z += 16+ 1;
Z = Z +17;
Z = 0 +17;
Z = 17;

Then the value of Z that is 17.

Answer 5:

Output:

8.46
Press any key to continue . . .

Explanation:

In the above program, we create one constant A and two variables B, Z that are initialized with "4.23F", "3.23F", and 2 respectively.

Let's evaluate the expression,

Z *= ++B+A%B;

Now evaluate pre increment operator then value of B will be 4.23F.

Z *= B+A%B;
Z *= 4.23+4.23%4.23;
Z *= 4.23+0;
Z = Z *4.23;
Z = 2 *4.23;
Z = 8.46;

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C#.Net find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C#.Net find output programs (Operators) | set 2... >>
<< C#.Net find output programs (Data Types) | set 3...