1. What are the 4 pillars of any object-oriented programming language?
- Abstraction
- Inheritance
- Encapsulation
- 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:
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
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->Bb)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();
}
}
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();
}
}
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
No
10. How do you prevent a class from being inherited ?
b)Add private constructors to the class.