OOP and C# Tricky Interview Question

By | August 2, 2016

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.

Become an Expert in Programming and Web Development.View all courses!

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

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 2

    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.

  4. 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.
  5. How would you suspend the execution of the of the below code till the results of GetStrignAsync() are not available.

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

  7. 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.

 

I will come up with the answers and explanations to these questions in my future article. If you want you can answer and discuss these question in comments section.

  • shardha

    Question No 6 ==> compile time error
    as Out value is intialised after Console.Writline..
    Out should be intialized First and then be assigned

  • SanMissar

    Q1

    i
    A a = new A();
    The sequence would be
    1. Static ctor of A
    2. Normal ctor of A

    ii
    B b = new A();
    error

    iii
    A c = new B();
    1. Static ctor of B
    2. Normal ctor of A
    3. Normal ctor of B

    iv
    B d = new B();
    1. Normal ctor of A
    2. Normal ctor of B