Inheritance Related Interview Question

 


1. What are the 4 pillars of any object-oriented programming language?
  1. Abstraction
  2. Inheritance
  3. Encapsulation
  4. Polymorphism
2. What is output of following C# code?

public class A
{
public void sum(){
int sum =0;
int firstNumber = 2, secondNumber = 3;
sum = firstNumber + secondNumber;
System.Console.WriteLine(sum);
}
}
public class B : A
{
public void sub()
{
int sub = 0;
int firstNumber = 5, secondNumber = 3;
sub = firstNumber - secondNumber;
System.Console.WriteLine(sub);
}
}
public class Program
{
public static void Main(string[] args)
{
B a = new A();
}
}



Output: Compiler error
Explanation: 
error CS0266: Cannot implicitly convert type 'A' to 'B'. An explicit conversion exists (are you missing a cast?)


3. What is the main advantage of using inheritance? 

a)Code re usability-Public and protected methods can be used in derived classes

b) Extensibility- base class can be extended as per business logic in derived classes.

c)Polymorphism

4. Does C# support multiple class inheritance?


No, Because of the complexities involved where method name can clash when two diff classes have same method name.This is resolved by pointers in C++ but its not possible in c#.

5. What are diff types of inheritance?

a) Single inheritance --Class B is derived from base class A. A->B
b)Multiple inheritance ---Class C derives from both A and B.  A,B->C
c)Multilevel inheritance--Class B derives from Class base class A.class C derives from B.  A->B->C
d)Hybrid inheritance --class B and C derives from same base class A.  A->B,C



6. What is output of following C# code?

public class Bird
{
public void fly(){
Console.WriteLine("Bird can fly");
}
}

public class Parrot : Bird
{
public void fly()
{
Console.WriteLine("Parrot can fly");
}
}

public class Program
{
public static void Main(string[] args)
{
Bird b = new Bird();
b.fly();
Bird b1 = new Parrot();
b1.fly();
}
}

Bird can fly
Bird can fly


7. What is output of following C# code?

public class Bird
{
private Bird()
{
}
public void fly(){
Console.WriteLine("Bird can fly");
}
}

public class Parrot : Bird
{
public void fly()
{
Console.WriteLine("Parrot can fly");
}
}

public class Program
{
public static void Main(string[] args)
{
Parrot b1= new Parrot();
b1.fly();
}
}


Output: Compiler error
Explanation: This C# code flashes compile time error as we know that when we create a derived class object then first base class constructor and then derived class constructor get called. But, since, base class constructor is private, it will not be accessible to derived class resulting compilation error.

8. What is output of following C# code?
sealed class X
{
public void f1()
{
Console.WriteLine("Base f1()");
}
}

class Y : X
{
public void f2()
{
Console.WriteLine("Derived f2()");
}
}

public class Program
{
public static void Main(string[] args)
{
Y obj = new Y();
obj.f2();
}
}

Output: Compiler error
Explanation: Class Y is derived from a sealed class. In C# object oriented programming a sealed class cannot be inherited. through a compile time error.


9. Is circular inheritance possible. like A:B ,B:A ?
No

10How do you prevent a class from being inherited ?

a)Make the class as sealed.
b)Add private constructors to the class.