Oops interview questions and answers

In this blog, I have collected Interview questions and answers of OOPS (Object Oriented Programming System).

Q. What are OOPS principals?

Object oriented is an approach to do programming in C#, Java etc.
OOPS Principals -

1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism

Q. Is inheritance possible if private constructor declared in paren class?

Answer - No.

Q. What is Early Binding and Late Binding?

Early Binding  = Method Overloading
Late Binding = Method Overriding

Read about Early Binding and Late Binding in OOPS Concept.


Q. How to call an Abstract Class function from a child class?


Ans - base.functionname();


class Test
    {
        static void Main(string[] args)
        {
            B b = new B();
            b.Greetings();
        }
    }

    abstract class A
    {
        public void Greetings()
        {
            Console.Write("Hello User ");
        }
    }
    class B : A
    {
        public void Greetings()
        {
            base.Greetings();
            Console.WriteLine("Welcome to the Interview");
        }
    }



Q. Abstract vs Sealed Class


Abstract Class
Sealed Class
An abstract class cannot be instantiated. 
A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. 
The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
Sealed classes prevent derivation. Because they can never be used as a base class.
Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods.

When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.




Q. What is Abstract Class?


Abstract class is a class which contains Abstract or non abstract methods.

See below sample code -

In abstract class TestAbstract you may include abstract method or non abstract method.

Abstract methods are those method which doesn't contains body i.e. abstract method has just an declaration.

If you have abstract method(s) in an abstract class and if that abstract class is a base class then child class must implement all the abstract methods available in Abstract class.

In below case, if you not implement Test1() method then compiler will throw an error.



class Program:TestAbstract
    {
        static void Main(string[] args)
        {
        }

        public override void Test1()
        {
            throw new NotImplementedException();
        }
    }

    abstract class TestAbstract
    {
        public virtual void Test()
        {
            Console.WriteLine("Hello");
        }

        public abstract void Test1();
    }




Q. Use of virtual keyword.


Answer - virtual keyword is used to implement method overriding concept.
If you have declared a method with keyword virtual , then this method must override the method in child class. See below code.


public class Program : Class1
{
static void Main(string[] args)
{

}
public override void Method()//Access modifier is changed from protected to public
{
base.Method();
}
}
public class Class1
{
protected virtual void Method() //Here the virtual method is protected
{
Console.Write("Hello");
}
}

Q. Constructor vs Destructor


A method with the same name as the class name is called a constructor.
Constructor by default public.
A constructor will not return anything.
Constructor name is same as class name.
Constructor with no arguments and nobody is called the default constructor.
We can create private constructors.


A constructor is a special method of a class which is automatically called as soon as the object of a class is created.



3 types of constructor Default Constructor, Parametrized Constructor, Copy Constructor



Default constructor:

Public class Employee()

{

Public Employee()

{

}



Parameterized Constructor:

Public Employee(int id, string name, decimal sal)
{
//Assign property
}

}



Copy Constructor:

Employee emp = new Employee();
Emploee emp1 = new Employee(1,"Emp1",10000)
emp1 = emp;
emp1.Name = "KK";
emp1.Sal = 20000;
emp1.ID = "Emp2";
emp = new Employee(emp1); //Copy Constructor

Destructor
It is called automatically when an object is no longer in use. This activity is automatically controlled by Garbage Collector.

Q. Abstract class vs Interface



Interface
Abstract
An interface has no implementation. It only contains a signature.
Abstract Class cannot be instantiated. It allows other class to inherit from.
A class cannot have multiple base class, i.e. a class cannot inherit multiple class. But a class can inherit multiple interfaces.  i.e. Multiple inheritance supported by the interface.

An interface cannot have the implementation for any of its members.
An abstract can or cannot have the implementation of its members.
Interface members cannot have access modifiers.
Abstract class members can have an access modifier.
An interface cannot have any fields.
An abstract class can have fields.
                                                                         

Q. Sealed and Virtual Class


Sealed class prevent itself from being inherited by other class.
sealed class ProgramNew
    {

    }

    class ExtendProgram : ProgramNew
    {

    }

If you compile the above code, it will throw an error. 
Error: cannot derive from a sealed type

Q. What is a Partial Class?

Partial class allows developers to split the class into 2 or more source files.
Below is the example.
public partial class Employee
   {
       public void SalaryProcess()
       {
           //Do coding for salary calculation like basic+hra+medical
       }
   }

   public partial class Employee
   {
       public void TaxCalculation()
       {
           //Do coding for Tax calculation
       }

   }



Q. Static Constructor

Static constructor will be invoked only once for any number of instance of a class. A static constructor must be parameterless.
   class Program
    {
        /// <summary>
        /// Static Constructor
        /// </summary>
        static Program()
        {
            Console.WriteLine("Static Constructor");
        }

        /// <summary>
        /// Default Constructor
        /// </summary>
        Program()
        {
            Console.WriteLine("Default Constructor");
        }

        /// <summary>
        /// Parameterize Constructor
        /// </summary>
        /// <param name="msg"></param>
        Program(string msg)
        {
            Console.WriteLine("Hello ! " + msg);
        }

        static void Main(string[] args)
        {
            Program obj = new Program();
            Program obj1 = new Program();
            Program obj2 = new Program("C#");
           
        }
       
    }



O/P- Static Constructor
Default Constructor
Default Constructor
Hello ! C#

Note: Default constructor executed twice, but static constructor invoked only once even non-parametrized constructor called 2 times.

Note: We can not create an object of the class which has a private constructor.

You may like other blogs -

Important ASP.Net Interview Questions and Answers
MVC Interview Questions and Answers
Top Web API interview questions and answers
C# Important interview questions and answers

Comments

Popular