Q:

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

belongs to collection: C#.Net find output programs

0

Find the output of C#.Net programs | Data Types | Set 1: Enhance the knowledge of C#.Net Data Types 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 Len = 0;
            int A = 100;

            Len = sizeof(int);
            Console.WriteLine("Len : " + Len);

            Len = sizeof(A);
            Console.WriteLine("Len : " + Len);
        }
    }
}

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            var Len = 0;
            var A = 100;

            byte[] bytes = BitConverter.GetBytes(A);
            Len = bytes.Length;
            Console.WriteLine("Len : " + Len);
        }
    }
}

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            var Len = 0;
            var A = "Hello";

            byte[] bytes = BitConverter.GetBytes(A);
            Len = bytes.Length;
            Console.WriteLine("Len : " + Len);
        }
    }
}

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            unsigned short A = sizeof(double);
            unsigned short B = 20;
            int    C = 0;

            C = A * B + 100;

            Console.WriteLine("C: " + C);
        }
    }
}

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            ushort A = sizeof(double);
            ushort B = 20;
            ushort C = 0;

            C = A * B + 100;

            Console.WriteLine("C: " + C);
        }
    }
}

All Answers

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

Answer 1:

Output:

main.cs(16,26): error CS0246: The type or namespace name `A' could not be found. 
Are you missing an assembly reference?

Explanation:

The above program will generate syntax error because we can get the size of the built-in type using sizeof(), but we cannot find the size of a variable in C#. That's why error will be generated by the above program.

To find out the size of a variable we need to use Bitconverter class. The code is given below:

byte[] bytes = BitConverter.GetBytes(A);
int LEN = bytes.Length; 

Answer 2:

Output:

Len : 4

Explanation:

In the above program, we declared two implicit type variables Len, A, which is initialized with 0, 100 respectively. Then we convert variable A into bytes using GetBytes() method of BitConverter class, then find the length using Length property.

Answer 3:

Output:

main.cs(13,41): error CS1502: The best overloaded method match for 
`System.BitConverter.GetBytes(bool)' has some invalid arguments
/usr/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error)
main.cs(13,50): error CS1503: Argument `#1' cannot convert `string' 
expression to type `bool'

Explanation:

The above program will generate syntax error because we cannot use GetBytes() method of the BitConverter class to convert the string into bytes.

We can get length of specified string using below code,

var Len = 0;
var A = "Hello";

Len = A.Length;
Console.WriteLine("Len : " + Len); 

Answer 4:

Output:

main.cs(10,21): error CS1525: Unexpected symbol `short'
main.cs(11,21): error CS1525: Unexpected symbol `short'

Explanation:

The above program will generate syntax error, because unsigned short is not a built-in data-type in C#. In C#, ushort is used for unsigned short.

Answer 5:

Output:

main.cs(14,17): error CS0266: Cannot implicitly convert type `int' to `ushort'.
An explicit conversion exists (are you missing a cast?)

Explanation:

The above program will syntax error because the below expression will return an integer value,

C = A * B + 100;

If you want to assign the result of the above expression then we need to typecast the above expression in ushort. The correct expression is given below:

C = (ushort)(A * B + 100);

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 (Data Types) | set 2... >>