TeachingBee

Must Do OOPS Interview Questions C#

Must Do OOPS Interview Questions C#

Object Oriented Programming (OOP) is a programming model where programs are organised around objects and data rather than action and logic. C# is a object oriented language and develop a good software one needs to have solid understanding of OOPS concept. Thus, interviewers screen the candidates by asking questions around these concepts. In this article we will see some of the important and frequently asked OOPS Interview Questions C# with detailed explanation.

OOPS Interview Questions C#

1. What is an accessibility modifier ? What are the different types of Access Modifiers in C#?

Access modifiers are the keywords which are used to restrict or specify the scope of the class or members of the class.They are used to implement encapsulation of OOPS.

Types of Access modifiers In C#

ModifierScope of the modifier
publicNo restrictions on accessing public members.
privateAccess is limited to within the class definition. By default if nothing is specified, scope is private.
protectedAccess is limited to within the class definition and any class that inherits from the class
private protectedAccess is limited to the containing class or types derived from the containing class within the current project.
internalAccess is limited exclusively to classes defined within the current project.
protected internalAccess is limited to the current project and types derived from the containing class. All members in current project and all members in derived class can access the variables.   
OOPS Interview Questions C#: Types of Access modifiers In C#

2. What is the difference between Interface and Abstract Class?

Abstract ClassInterface
Both declaration and definition is provided, thus it doesn’t provide full abstractionOnly declaration is provided, thus providing full abstraction.
It can have a member fieldMember field cannot be declared inside it.
Various access modifiers can be used to limit the scopeBy default everything is public and therefore no access modifier can be used.
It can be have the keyword static, virtual, abstract or sealed associated with it.It cannot be defined using the keyword static, virtual, abstract or sealed.
Multiple inheritance cannot be achievedMultiple inheritance can be achieved
OOPS Interview Questions C#: Interface Vs Abstract Class

3. What is the difference between late binding and early binding?

Early Binding: In this type of biding compiler detects kind of obejct and detects methods and properties it contains at compilation level itself. If data type doesn’t match or method cannot be find, complier throws exception at compilation time. This helps in improving performance and helps application runs faster. For eg:

  • ComboBox comboItem;

In this complier has already binded comboitem object with ComboBox class at the time of compliation.

Late Binding: Late binding occurs at run time. The compiler is unaware about type of object at compilation level, rather its type is determine at run time level. It is mostly done using virtual methods. Late Binding is slower than early binding as run time lookups are performed to determine type of object.

For Eg:

  • Object objectItem;
    objectItem = CreateObject(“DLL name”);

In this type of objectItem is not known at compilation level rather determine at run time.

4. What is a virtual method in C#?

Virtual method is a method in a class that allows method overriding with same derived class signature.They are used to implement polymorphism concept of OOPS in C#. When a virtual method is called, which method to be invoked is decided at run time level (late binding).

A virtual method is used to override specified base class implementation when a runtime object is of the derived type. Thus, virtual methods facilitate the consistent functionality of a related object set. For eg:

// Must Do OOPS Interview Questions C#

using System;

public class InterviewQuestions

{
  public static void Main(string[] args) {
    Arithmetic arithmetic = new Arithmetic();
    Calculator cal = arithmetic;
    arithmetic.addition();
    cal.addition();
    arithmetic.subtraction();
    cal.subtraction();

  }
}

public class Calculator {
  public void addition() {
    Console.WriteLine("Perform Addition");
  }

  public virtual void subtraction() {
    Console.WriteLine("Perform Subtraction");
  }

}

// Derived Class
public class Arithmetic: Calculator {
  public void addition() {

    Console.WriteLine("Perform Addition in the derived class");
  }

  public override void subtraction() {

    Console.WriteLine("Perform Subtraction in derived class");
  }
}

Output:

Perform Addition in the derived class
Perform Addition
Perform Subtraction in derived class
Perform Subtraction in derived class

5. Can “this” keyword be used within a static method?

No, this keyword cannot be used within static method. this keyword refers to current instance of the class but in static method we don’t have that current instance.

6. What is ReadOnly? How is it different from Static?

ReadonlyStatic
Only the class level fields can be readonly. The local variables of methods cannot be readonly.Classes, constructors, methods, variables, properties, event and operators can be declared static. but not struct, indexers, enum, destructors, or finalisers.
Readonly fields can be initialised at declaration or in the constructor.
Therefore, readonly variables are used for the run-time constants.
Static members can only be accessed within the static methods. The non-static methods cannot access static members.
Readonly variable cannot be modified at run-time. It can only be initialized or changed in the constructor.Value of the static members can be modified.
Readonly members can be accessed using object, but not ClassName.ReadOnlyVariableName.Static members can be accessed using ClassName.StaticMemberName, but cannot be accessed using object.
Declared using the readonly keyword.Declared using the static keyword.
readonly vs static


7. What is the significance of cohesion in OOP?

Cohesion In C# refers to relationship within modules.It describes the structure inside a module and functional connections within module. The greater the cohesion, the better will be the program design.A good cohesive module should do exactly one thing.

Higher cohesion:

  1. Leads to simple classes performing only single task
  2. Simpler the class simple is to comprehend and use further
  3. Greater cohesion leads to greater reusability of modules.


8. How would you prevent a class from overriding in C#?

To prevent class method to get overriden in C#, sealed keyword is used with the method. The sealed method should be part of a derived class and the method must be an overridden method.

// Must Do OOPS Interview Questions C#

public sealed override void nonOverridenFunction() { }

Other OOPS Interview Questions C#

  1. What are implicit and explicit interface implementations?
  2. Enumerate differences between abstraction and encapsulation.
  3. How would you explain monads to a non-programmer?
  4. State differences between coupling and cohesion in C#?
  5. How would you catch multiple exceptions at once in C#?
  6. What is operator overloading?
  7. How is a static class different from a singleton instance?

Conclusion

In this article we discussed some Must Do OOPS Interview Questions C# with explanation. Checkout Other commonly asked questions here

Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

uses of computer network

Important Uses Of Computer Network

In this article we will see an overview of computer networks, various uses of computer network and its applications, and key technologies involved in computer networking. So, let’s begin. What

AWS Lambda Interview Questions-teachingbee

Important AWS Lambda Interview Questions And Answers

In today’s fast-paced tech landscape, AWS Lambda has emerged as a game-changer in serverless computing. As organizations increasingly shift towards serverless architectures, mastering AWS Lambda is becoming a crucial skill

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In