C# program interview question for practice

Below are the programs to expect the output by examining the code.

Program 1

can we call methods and members from Class B for the below code ?

class A
    {
        protected void ADD()
        {
        }
        public int a;
        public int b;
    }
    private class B : A
    {
        A obja = new A();
      
    }

Output

Answer includes : No, we will not able to call any private ,protected, Public data members or functions from the class B

Program 2

what is the output of the below program ?

Below is the programs( Program 2 , Program 3 ) that tells the concept of method overriding and method hiding in c#

public  class A
    {
        public virtual void fun1()
        {
            Console.WriteLine("fun1 Parent class function");
        }
        public virtual void fun2()
        {
            Console.WriteLine("fun2 Parent class function");
        }
        public virtual void fun3()
        {
            Console.WriteLine("fun3 Parent class function");
        }
    }
    public  class B : A
    {
        public new void fun1()
        {
            Console.WriteLine("fun1 Child class function");
        }
        public override void fun2()
        {
            Console.WriteLine("fun2 Child class function");
        }
        public new void fun3()
        {
            Console.WriteLine("fun3 Child class function");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            B objb = new B();
            objb.fun1();
            objb.fun2();
            objb.fun3();
        }
    }

Output

fun1 Child class function
fun2 Child class function
fun3 Child class function

Program 3

what is the output of the below program

public  class A
    {
        public virtual void fun1()
        {
            Console.WriteLine("fun1 Parent class function");
        }
        public virtual void fun2()
        {
            Console.WriteLine("fun2 Parent class function");
        }
        public virtual void fun3()
        {
            Console.WriteLine("fun3 Parent class function");
        }
    }
    public  class B : A
    {
        public new void fun1()
        {
            Console.WriteLine("fun1 Child class function");
        }
        public override void fun2()
        {
            Console.WriteLine("fun2 Child class function");
        }
        public new void fun3()
        {
            Console.WriteLine("fun3 Child class function");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A objb = new B();
            objb.fun1();
            objb.fun2();
            objb.fun3();
        }
    }

Output

fun1 Parent class function
fun2 Child class function
fun3 Parent class function

Program 4

what is the output for the below code

Below is the program which tells the concept of inheritance in c#

public  class A
    {
      public int a;
      public void Display()
      {
          Console.WriteLine(a);
      }
    }
    public  class B : A
    {
        public int b;

        public void Display()
        {
            Console.WriteLine(b);
        }
    }
class Program
    {
        static void Main(string[] args)
        {
            B obj = new B();
            obj.a = 10;
            obj.b = 20;
            obj.Display();
        }
    }

Output

20

Program 5

For below how many time the loop will iterate

Below is the simple program to find the number of iterations the loop can run.

class Program
    {
        static void Main(string[] args)
        {
            int a= 9;

            while (a < 30)
            {
                 a=a+3;
            }

        }
    }

Output:

7

The loop in the above program will run for 7 iterations.

Leave a Comment