Pre Course Home Next

C# Programming Interview Questions Tutorial

Tricky Questions about C#

This section lists some common tricky questions asked during interviews.

Inheritance Related

Q. What will be output of below:

    class A
    {
        public void hello(double d)
        {
            Console.WriteLine("hello from A");
        }
    }

    class B : A
    {
        public void hello(int a)
        {
            Console.WriteLine("hello from B");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            double i = 5;
            B b = new B();
            b.hello(i);
            Console.ReadLine();
        }
    }

A. Output will be: hello from A

In this case, when B inherits A, there is hello method overloading. So when client will access the instance of B, they will find two overloads of method hello. Here the tricky part is the datatype of the variable i.

Here the instance of B is created, so first it will try to access the hello method of class B, but it takes integer type parameter, but the data type of variable i is double, and double can not be implicitly converted to int, so then it moves to the base class A which includes the hello method that accepts double data type value.

Q. Similar to above question, what will be the output of below: 

    class A
    {
        public void hello(int a)
        {
            Console.WriteLine("hello from A");
        }
    }

    class B : A
    {
        public void hello(double d)
        {
            Console.WriteLine("hello from B");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int i = 5;
            B b = new B();
            b.hello(i);
            Console.ReadLine();
        }
    }

A. Output will be: hello from b

Here the instance of B is created, so first it will try to access the hello method of class B, but it takes double type parameter, but the data type of variable i is int, and int can be implicitly converted to double data type, that's why it uses the hello method of B instead of going to base class A.

 

Q. Another inheritance related question, what will be the output of below: 

    class A
    {
        public void Hello()
        {
            Console.WriteLine("hello from A");
        }
        
        public virtual void HelloAgain()
        {
            Console.WriteLine("hello again from A");
        }
    }

    class B : A
    {
        public new void Hello()
        {
            Console.WriteLine("hello from B");
        }
        
        public override void HelloAgain()
        {
            Console.WriteLine("hello again from B");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            A obj = new B();
            obj.Hello();
            obj.HelloAgain();
            Console.ReadLine();
        }
    }

A. Output will be: 

hello from A
hello again from B

Here Instance of class B is created and then it is casted as class A. Class B is child class and class A is parent class. This type of casting is called upcast, because we are casting lower class to upper class. In class B, Hello method uses 'new' keyword, and this is called method hiding. HelloAgain Method is overridden in class B. So when upcast happens, overridden method will be returned from Child class and hidden method will be returned from Parent class. Even if we dont use new keyword for Hello in child class, it will call the parent class method.

Bit Manupulation Related

Q. Add two numbers without using add operator in C#?

A. When  type of questions are asked, where interviewer asks to perform any matemetical operations like addition, subtraction, multiplication etc, it means they are trying to know if you are aware of bit manupulation or not.

These type of questions can be solved using bit manupulation concepts. Below is the solution of how to add two numbers without using addition operator.

using System;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine(Program.Add(1,2));
		Console.WriteLine(Program.Add(1,5));
		Console.WriteLine(Program.Add(100,2));
	}
	
	public static int Add(int a, int b) {
        while(a != 0){
            int carry = a & b;
			// after this first loop, now b will carry addition value and a will carry "carry" value
            b = b ^ a;
			a = carry << 1;
        }
        
        return b;
		
    }
}

Here three concepts of bit manupulation are used. AND (& operator), XOR (^ operator) and left shift (<< operator). AND operator gives carry, XOR operator gives acctual addition and then we left shift one position the current carry and in next iteration we again perform the same operation untill carry becomes zero.

A good explanation of this concepts is explained in the below video:

 

 

Pre Course Home Next