Q:

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

0

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

Question 1:

using System;

namespace Demo
{
    class Program
    {
        const int A = 10;
        
        //Entry point of the program
        static void Main(string[] args)
        {
            int C;
            const int B;
            
            B = 20;

            C = A + B;

            Console.WriteLine(C);
        }
    }

}

Question 2:

using System;

namespace Demo
{
    class Program
    {
        const int A = 10;
        const int B;

        Program():B(20)
        {
        }
        //Entry point of the program
        static void Main(string[] args)
        {
            int C;
            
            C = A + B;

            Console.WriteLine(C);
        }
    }
}

Question 3:

using System;

namespace Demo
{
    class Program
    {
        const void Fun()
        {
            Console.WriteLine("Fun() called");
        }
        //Entry point of the program
        static void Main(string[] args)
        {
            Program P = new Program();

            P.Fun();
        }
    }
}

Question 4:

using System;

namespace Demo
{
    class Program
    {
        void Fun()
        {
            Console.WriteLine("Fun() called");
        }
        //Entry point of the program
        static void Main(string[] args)
        {
            const Program P = null;
            Console.WriteLine("Hello World");
        }
    }
}

Question 5:

using System;

namespace Demo
{
    class Program
    {
        void Fun()
        {
            Console.WriteLine("Fun() called");
        }
        //Entry point of the program
        static void Main(string[] args)
        {
            const Program P = new Program();

            P.Fun();
        }
    }
}

All Answers

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

Answer 1:

Output:

ain.cs(13,24): error CS0145: A const field requires a value to be provided

Explanation:

The above program will generate syntax error because constant B must be initialized at the time of declaration. But we assigned 20 after its declaration.

Answer 2:

Output:

main.cs(8,20): error CS0145: A const field requires a value to be provided
main.cs(10,18): error CS1525: Unexpected symbol `B', expecting `base' or `this'

Explanation:

The above program will generate syntax errors because, in the above program, we tried to initialize const data members using the constructor.

Program():B(20)
{}

But we did not use the correct way to initialized const data members. The above constructor definition looks like the member initializer list in C++, but it is not supported in C#.

Answer 3:

Output:

main.cs(7,15): error CS1547: Keyword `void' cannot be used in this context
main.cs(7,23): error CS0145: A const field requires a value to be provided
main.cs(12,15): error CS1525: Unexpected symbol `void', expecting `class', 
`delegate', `enum', `interface', `partial', or `struct'
main.cs(12,32): error CS1514: Unexpected symbol `[', expecting `.' or `{'
main.cs(12,32): error CS1525: Unexpected symbol `]', expecting `class', `delegate', 
`enum', `interface', `partial', or `struct'
main.cs(14,25): error CS1530: Keyword `new' is not allowed on namespace elements
main.cs(14,28): error CS1525: Unexpected symbol `Program', expecting `class', 
`delegate', `enum', `interface', `partial', or `struct'

Explanation:

The above program will generate syntax errors because we cannot define a constant method in C#, here we defined method Fun() with const keyword, which is not correct.

Answer  4 :

Output:

Hello World
Press any key to continue . . .

Explanation:

The above program will print "Hello World" on the console screen. In the above program, we defined method fun() inside the Program class, and we created a const reference of Program class initialized with null and then we print "Hello World" on the console screen.

Answer 5:

Output:

main.cs(14,31): error CS0133: The expression being assigned to `P' 
must be a constant or default value

Explanation:

The above program will generate syntax error because a const reference except string can only be initialized with null, but here we created a const reference for Program class, which is not correct.

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now