Dot Net For All

OOP and C# Tricky Interview Question

Image Courtesy Pixabay

Hello, In this article I will ask some of the tricky C# and OOP interview question which are asked at experienced level. These questions can be faced by all the experienced C# professionals appearing for the IT companies interviews.

To learn C# and face interview with confidence I have recommended some very good books and courses. The references are at the end of article.

Irrespective of your experience level, you need to know the important data structure questions. You can get the most frequently asked DS and Algo questions here.

 

C# and OOP tricky Interview Questions

Please go thought one of my previous article for interview questions here. Though we can easily answer these questions if we have a computer in our hand. But in interview we don’t have access to computers, that is why we should be strong in the basic concepts.

The interview can give you wrong options to confuse you. For question 1 and 2 please refer below figure.

interview question 1
  1. Please refer the above figure for this question.Each class in the above figure contains an instance constructor and static constructor. B class is derived from the A class. Now coming to question, what will be the sequence of the constructors called among 1, 2, 3, and 4 in each of the following cases.
    A a = new A();
    B b = new A();
    A c = new B();
    B d = new B();
  2. Referring the above figure, Method() from which class will be called in case of method overloading and method hiding respectively for the below cases. The methods are numbered as 5 and 6
    a.Method();
    b.Method();
    c.Method();
    d.Method();
  3. Please refer the below figure for this question.
Interview Question 3

Suppose we have a singleton class. We have three different assemblies which use three different instances of this singleton class. How is this possible. Please make sure that this design should be easily extensible. There can be more assemblies in the future which will be using different instances.

You can find the answer of the question in one of my post mentioned here.

The above question can be asked in another way. How we can create only n number of instance of a class. E.g. How can we create only 5 instances of the class. And if anyone is creating more then 5 instance an exception should be thrown.

How would you suspend the execution of the of the below code till the results of GetStrignAsync() are not available.

    HttpClient client = new HttpClient();
    string URL = "https://www.dotnetforall.com";
    string output = null;
    Task<string> task = client.GetStringAsync();

8. Why do you thing we will get the compile time error in the below code?

 class Program
    {
        static int Main(string[] args)
        {
            int y = 3;
            Write(out y);
            return 0;
        }

        private static void Write(out int y)
        {
            Console.WriteLine(y);
            y = 1;
        }
    }

9. What is abstraction and encapsulation in OOP. Is the abstract class same as abstraction in C#?

You can read more about abstraction and encapsulation in this article. In this article I have clear explained the two concepts.

8.How can you create a thread safe singleton class without using lock.

The best way to create a thread safe singleton class without using a lock is to have a static field. The field will contain the instance of the Singleton class. And since the field is static, the instance will be created by the CLR. And hence the threading scenarios will be taken care by CLR.

The code for same is shown in below snippet.

public class Singleton
{
private static readonly Singleton singleton = new Singleton();
private Singleton()
{
Console.WriteLine("Instanse Created");
}
public static Singleton Instance
{
get
{ return singleton; }
}
}
public class Singleton
{
    private static readonly Singleton singleton = new Singleton();
    private Singleton()
    {
        Console.WriteLine("Instanse Created");
    }
    public static Singleton Instance
    {
        get
        { return singleton; }
    }
}

9. What is association, aggregation and composition?

I have already discussed association, aggregation and composition in OOP here in one of my article.

10. What do you think can be use and utilization of public static factory method in a class?

A public static factory method can be used to create an instance of the class. It can be specifically be use full in scenario where you want to have overloads of the constructor. 

public class Complex
   {
       private Complex()
       {

       }

       private Complex(int realnumber, int imaginaryNumber)
       {

       }

       public static Complex FromRealNumer(int realnumber, int imaginaryNumber)
       {
           return new Complex(realnumber, imaginaryNumber);
       }
   }

As you can see in the above code snippet, I wanted to have two constructors to my Complex class. We could have simply create one more constructor, but having a static method with meaningful name give a clear idea to the user of this class.

11. What is cohension and coupling. How you can use them to write better code?

Cohesion is the degree to which the various parts of a software component are related.

Coupling is defined as the level of inter dependency between various software components.

We should always strive for high cohension and low coupling. In this post you can learn more about cohension and coupling with code example.

12. How would you print in console without writing anything in the below main method?

    public class Mainclass
    {
        public static void Main(string[] args)
        {

        }
    }

For Further Preperations:

  1. Object-Oriented Programming Fundamentals in C#
  2. C# Fundamentals
  3. Introduction To Async And Parallel Programming in .NET
  4. TPL Async

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview