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

Find the output of C#.Net programs | Arrays | Set 1: Enhance the knowledge of C#.Net Arrays concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 06, 2021

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int arr[]={1,2,3,4,5};

            for(int i=0; i<5;i++)
            {
                Console.WriteLine(arr[i]);
            }
        }
    }
}

Output:

main.cs(10,19): error CS1525: Unexpected symbol `[', expecting `,', `;', or `='
main.cs(10,25): error CS1525: Unexpected symbol `2', expecting `,', `;', or `='
main.cs(10,27): error CS1525: Unexpected symbol `3', expecting `,', `;', or `='
main.cs(10,29): error CS1525: Unexpected symbol `4', expecting `,', `;', or `='
main.cs(10,31): error CS1525: Unexpected symbol `5', expecting `,', `;', or `='

Explanation:

The above program will generate syntax errors. Because we used incorrect syntax for array declaration.

The correct syntax is given below:

int []arr={1,2,3,4,5};

Question 2:

using System;

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

            arr = new int[5];

            arr[0] = 32;
            arr[1] = 16;
            arr[2] = 48;
            arr[3] = 25;
            arr[4] = 67;

            ok = arr[0];
            for(int i=1; i<arr.Size();i++)
            {
                if (ok > arr[i])
                    ok = arr[i];
            }

            Console.WriteLine(ok);
        }
    }
}

Output:

main.cs(22,32): error CS1061: Type `int[]' does not contain a definition for `Size' and 
no extension method `Size' of type `int[]' could be found. Are you missing an assembly reference?
/usr/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error)

Explanation:

The above program will generate syntax error because Size() is not an inbuilt method in C#.

The Length property is used to get the length of an array in C#.

Question 3:

using System;

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

            arr = new int[5];

            arr[0] = 32;
            arr[1] = 16;
            arr[2] = 48;
            arr[3] = 25;
            arr[4] = 67;

            ok = arr[0];
            for(int i=1; i<arr.Length;i++)
            {
                if (ok > arr[i])
                    ok = arr[i];
            }

            Console.WriteLine(ok);
        }
    }
} 

Output:

16
Press any key to continue . . .

Explanation:

In the above program, we declared an integer array arr with size 5, and declared a local variable ok.

Here, we used Length property that will return 5, so the loop will execute until the value of the variable i is less than 5.

In the above statement, we assigned the first value of an array to the variable ok.

Let's iterate the loop step by step,

Iteration1:
ok=32,  i=1
Here we check condition 32>16, 
then assign 16 to the variable “ok”.

Iteration2:
ok=16,  i=2
Here we check condition 16>48, the condition is false. 
Then value "ok" will not change.

Iteration3:
ok=16,  i=3
Here we check condition 16>25, the condition is false. 
Then value "ok" will not change.

Iteration4:
ok=16,  i=4
Here we check condition 16>67, the condition is false. 
Then value "ok" will not change.

Now the value of 'i' will 5, and loop condition will false and the loop will terminate. And "16" will be printed on the console screen.

Question 4:

using System;

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

            arr = new int[5];

            arr[0] = 32;
            arr[1] = 16;
            arr[2] = 48;
            arr[3] = 25;
            arr[4] = 67;

            ok = arr[0];
            for(int i=1; i<arr.Length;i++)
            {
                if (ok > Convert.ToInt32(arr.GetValue(i)))
                    ok = arr[i];
            }

            Console.WriteLine(ok);
        }
    }
}   

Output:

16
Press any key to continue . . .

Explanation:

In the above program, we declared an integer array arr with size 5, and declared a local variable ok.

Here, we used Length property that will return 5, so the loop will execute until the value of the variable i is less than 5.

ok = arr[0];

In the above statement, we assigned the first value of an array to the variable ok.

Convert.ToInt32(arr.GetValue(i))

In the above program, we used the GetValue() method, this method is used to get elements of the array,

Based on the index, but this method return value of object type, that's why here we converted returned value into a 32-bit integer.

Let's iterate the loop step by step,

Iteration1:
ok=32,  i=1
Here we check condition 32>16, 
then assign 16 to the variable “ok”.

Iteration2:
ok=16,  i=2
Here we check condition 16>48, 
the condition is false. Then value "ok" will not change.

Iteration3:
ok=16,  i=3
Here we check condition 16>25, 
the condition is false. Then value "ok" will not change.

Iteration4:
ok=16,  i=4
Here we check condition 16>67, 
the condition is false. Then value "ok" will not change.

Now the value of 'i' will 5, and loop condition will false and the loop will terminate. And "16" will be printed on the console screen.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int []arr={1,7};

            arr = new int[5];

            for (int I = 0; I < 5; I++)
            { 
                Console.WriteLine(5*arr[I]); 
            }
        }
    }
}

Output:

0
0
0
0
0
Press any key to continue . . .

Explanation:

In the above program, we created an integer array initialized with 2 elements {1,7}. But after that, we reallocate space for array arr with 5 elements using new operator. That's why all 5 elements will be '0'.

In the loop body, we used the WriteLine() method that will print 0 in every iteration, because multiplication 5 with 0 will be 0. All elements of the array are 0.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.