C# Object creation exercise test

  • What would be the output of-   

    classA { }

    classB { }

    classC { }

    classProgram

    {

        staticvoid Main(string[] args)

        {

            B objB = newB();

            C objC = newC();

            A objA = newA();

 

            Console.ReadLine();

        }

    }

1)  Compiler error

2)  Runtime error

3)  Run without error

 

  • In above program, if we write below line then

           B objB = newC();

1)  Compiler error as class C can’t be implicitly cast to class B

2)  No error

3)  Runtime error

 

  • What will be the result –

  classA

    { }

    classB : A

    { }

    classC :B

    { }

 

    classProgram

    {

        staticvoid Main(string[] args)

        {

            A objB = newB();      //1

            B objC = newC();      //2

            C objA = newA();      //3

            C objAC = (C) newA();  //4

 

            Console.ReadLine();

        }

    }

1)  No error at any line

2)  Line 4 will through runtime error while line3, compile time

3)  Line 3 will through runtime error while line4 compile time

4)  Only line 3 will give compile time error