OOPS Interview
OOPS Interview
Readonly is the keyword whose value we can change during runtime or we can
assign it at run time but only through the non-static constructor.
Const
Const is nothing but "constant", a variable of which the value is constant but at
compile time. And it's mandatory to assign a value to it. By default a const is static
and we cannot change the value of a const variable throughout the entire program.
Static ReadOnly
A Static Readonly type variable's value can be assigned at runtime or assigned at
compile time and changed at runtime. But this variable's value can only be changed
in the static constructor. And cannot be changed further. It can change only once at
runtime.
Static Class: Declared with Static keyword, methods in Static Class are also static along with
variables of the class.
This class cannot be instantiated, i.e we cannot have objects of this class. To access methods of this
class, you can directly use classname.method. Also this class cannot be inherited.
Sealed Class: Declared with Sealed keyword, which enables this class to seal all its variables,
methods and properties. No other class can inherit anything from this class or in other words, this class
cannot be inherited. But we can instantiate this class, i.e we can have any number of objects of a
sealed class.
Abstract Class: Declared with abstract keyword, this class is primarily created as a
Inheritable class. An abstract class enables other classes to inherit from this class, but forbids to
instantiate. One can inherit from an abstract class but we cannot create objects of an abstract class.
Abstract class can have abstract as well as non abstract methods. Abstract methods are those which
are not having method definition.
What is OOPS?
Object-Oriented Programming System (OOPs) is a programming concept that works on the
principles of abstraction, encapsulation, inheritance, and polymorphism. It allows users to
create objects they want and create methods to handle those objects. The basic concept of
OOPs is to create objects, re-use them throughout the program, and manipulate these
objects to get results.
1) Class
The class is one of the Basic concepts of OOPs which is a group of similar entities. It
is only a logical component and not the physical entity.
2) Object
An object can be defined as an instance of a class, and there can be multiple
instances of a class in a program.
3) Inheritance
Inheritance is one of the Basic Concepts of OOPs in which one object acquires the
properties and behaviors of the parent object. It’s creating a parent-child
relationship between two classes. It offers robust and natural mechanism for
organizing and structure of any software.
4) Polymorphism
Polymorphism is the ability of a variable, object or function to take on multiple
forms. For example, in English, the verb run has a different meaning if you use it
with a laptop, a foot race, and business. Here, we understand the meaning
of run based on the other words used along with it. The same also applied to
Polymorphism.
5) Abstraction
Abstraction : Abstraction is the process of showing only essential/necessary
features of an entity/object to the outside world and hide the other irrelevant
information. For example to open your TV we only have a power button, It is not
required to understand how infra-red waves are getting generated in TV remote
control.
Advantages of Abstraction
• • It reduces the complexity of viewing the things.
• • Avoids code duplication and increases reusability.
• • Helps to increase security of an application or program as only important
details are provided to the user.
6) Encapsulation
Encapsulation is one of the best Java OOPs concepts of wrapping the data and
code. In this OOPs concept, the variables of a class are always hidden from other
classes. It can only be accessed using the methods of their current class. For
example – in school, a student cannot exist without a class.
using System;
class GFG {
// Main method
// Declaring variable
int G;
Sum(out G);
}
// Method in which out parameter is passed
G = 80;
G += G;
}
}
The ref is a keyword in C# which is used for the passing the arguments by a
reference. Or we can say that if any changes made in this argument in the
method will reflect in that variable when the control return to the calling method.
The ref parameter does not pass the property.
class GFG {
// Main Method
public static void Main()
{
Overloading
int a = 5;
int b = 6;
calc.Add(a, b);
double c = 4.2
double d = 5.41
double e = 2.57
calc.Add(c, d, e);
Overriding
Parameters
Occurrence
In C#, overriding occurs within the base In C#, overloading occurs within the
class and the derived class. same class.
Binding Time
Synonyms
You can go with the abstract class if you wanted to provide the default
implementation to all the subclasses.
You can use an abstract class if we don’t want to implement all of the
methods in a derived class but only some of them in a parent class.
An interface can be used when you want the derived classes to fully
implement all of the methods introduced in the base class.
Constructor
Special method of the class that is automatically invoked when an instance of the class is created is called
a constructor.
The main use of constructors is to initialize the private fields of the class while creating an instance for
the class.
• • The constructor in C# has the same name as class or struct.
• • A class can have any number of constructors.
• • A constructor doesn't have any return type, not even void.
• • A static constructor can not be a parametrized constructor.
• • Within a class, you can create one static constructor only.
Type of constructor
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Static Constructor
5. Private Constructor
Default Constructor:
A constructor without any parameters is called a default constructor; in other words, this type of
constructor does not take parameters.
The drawback of a default constructor is that every instance of the class will be initialized to the same
values and it is not possible to initialize each instance of the class with different values.
Parameterized Constructor:
A constructor with at least one parameter is called a parameterized constructor. The advantage of a
parameterized constructor is that you can initialize each instance of the class with a different value.
Copy Constructor:
The constructor which creates an object by copying variables from another object is called a copy
constructor. The purpose of a copy constructor is to initialize a new instance to the values of an existing
instance.
Static Constructor:
When a constructor is created using a static keyword, it will be invoked only once for all of the instances
of the class and it is invoked during the creation of the first instance of the class or the first reference to a
static member in the class. A static constructor is used to initialize static fields of the class and to write the
code that needs to be executed only once.
A static constructor does not take access modifiers or have parameters.
Private Constructor:
When a constructor is created with a private specifier, it is not possible for other classes to derive from
this class, neither is it possible to create an instance of this class.
They are usually used in classes that contain static members only.
Points To Remember :
It is the implementation of a singleton class pattern.
use private constructor when we have only static members.
Using private constructor, prevents the creation of the instances of
that class.
Base keyword
We can use the base keyword to access the fields of the base class within derived class. It is useful if base
and derived classes have the same fields. If derived class doesn't define same field, there is no need to use
base keyword. Base class field can be directly accessed by the derived class.
Yield Keyword
The functionality this keyword provides is that when iterating a list, we can read an element of the loop,
return to the calling code and go back to the loop again at the same point, from where it left the loop and
continue processing the records in the loop. So this will be the basic idea behind the example that we will
be using.
Sealed class
C# sealed keyword applies restrictions on the class and method. If you create a sealed class, it cannot be
derived. If you create a sealed method, it cannot be overridden.
Static class
It is the type of class that cannot be instantiated, in other words we cannot create an object of that class
using the new keyword, such that class members can be called directly using their class name.
Method Hiding
A concept to hide the methods of the base class from derived class, this concept is known as Method
Hiding. It is also known as Method Shadowing.
In method hiding, you can hide the implementation of the methods of a base class from the derived
class using the new keyword. Or in other words, in method hiding, you can redefine the method of the
base class in the derived class by using the new keyword.
The type of the variable is decided by the The type of the variable is decided
compiler at compile time. by the compiler at run time.
If the variable does not initialized it throw If the variable does not initialized it
an error. will not throw an error.
C# collection types are designed to store, manage and manipulate similar data more efficiently.
Data manipulation includes adding, removing, finding, and inserting data in the collection.
Collection types implement the following common functionality:
• • Adding and inserting items to a collection
• • Removing items from a collection
• • Finding, sorting, searching items
• • Replacing items
• • Copy and clone collections and items
• • Capacity and Count properties to find the capacity of the collection and number of items
in the collection
.NET supports two types of collections, generic collections and non-generic collections.
In non-generic collections, each element can represent a value of a different type. The collection
size is not fixed. Items from the collection can be added or removed at runtime.
Types of non-generic collections:
1. ArrayList
2. HashTable
3. SortedList
4. Stack
5. Queue
Generic Collections
A generic collection is strongly typed (you can store one type of objects into it) so that we can
eliminate runtime type mismatches, it improves the performance by avoiding boxing and
unboxing.
It can accept all types of the object at runtime.
Types:
• • List
• • Dictionary
• • Generics
• • Queues Generics
Tuples
Tuple is a data structure that is used to store sequence of elements. Tuple with n elements are
known as n-tuple.
We can use Tuple for the following reasons.
• • To represent a single set of data
• • To provide easy access and manipulation of data
• • To return multiple values from a method without using out parameter
• • To pass multiple values to a method through a single parameter
Dependency Injection
Dependency Injection (DI) is a software design pattern. It allows us to develop loosely-coupled code.
The intent of Dependency Injection is to make code maintainable. Dependency Injection helps to reduce
the tight coupling among software components.
Dependency Injection reduces the hard-coded dependencies among your classes by injecting those
dependencies at run time instead of design time technically.
Design Pattern
Design Patterns are nothing but documented and tested solutions for recurring problems in a given
context. So, in simple words, we can say that the Design Patterns are reusable solutions to the problems
that as a developer we encounter in our day to day programming.
Types of design pattern
• • Creational Design Pattern
• • Structural Design Pattern
• • Behavioral Design Pattern
Repository Pattern
A Repository in C# mediates between the domain and data mapping layers.
Repository pattern C# is a way to implement data access by encapsulating the set of objects persisted in
a data store and the operations performed over them, providing a more object-oriented view of the
persistence layer.
In other words, we can say that a Repository Design Pattern acts as a middleman or middle layer
between the rest of the application and the data access logic.
Loose and Tight Coupling
Loose Coupling means reducing dependencies of a class that use a different class directly.
In tight coupling, classes and objects are dependent on one another.
In general, tight coupling is usually bad because it reduces flexibility and re-usability of code and it
makes changes much more difficult and impedes testability etc.
SOLID principles
SOLID principles are the design principles that enable us to manage most of the software design
problems. These principles provide us with ways to move from tightly coupled code and little
encapsulation to the desired results of loosely coupled and encapsulated real needs of a business
properly.
1. S: Single Responsibility Principle (SRP)
2. O: Open closed Principle (OCP)
3. L: Liskov substitution Principle (LSP)
4. I: Interface Segregation Principle (ISP)
5. D: Dependency Inversion Principle (DIP)
Model Binding
Model binder allows you to map Http Request data with the model.
Model binding is a well-designed bridge between the HTTP request and
the C# action methods. It makes it easy for developers to work with
data on forms (views), because POST and GET is automatically
transferred into a data model you specify. ASP.NET MVC uses default
binders to complete this behind the scene.
[HttpPost]
public ActionResult Create(FormCollection collection){
try{
// TODO: Add insert logic here
return RedirectToAction("Index");
}catch{
return View();
}
}
Extension Method
The extension method concept allows you to add new methods in the existing
class or in the structure without modifying the source code of the original type
and you do not require any kind of special permission from the original type and
there is no need to re-compile the original type.
Difference Between Const, ReadOnly and Static ReadOnly
Const is nothing but "constant", a variable of which the value is constant but at
compile time. And it's mandatory to assign a value to it. By default a const is static
and we cannot change the value of a const variable throughout the entire
program.
Readonly is the keyword whose value we can change during runtime or we can
assign it at run time but only through the non-static constructor.
A Static Readonly type variable's value can be assigned at runtime or assigned at
compile time and changed at runtime. But this variable's value can only be
changed in the static constructor. And cannot be changed further. It can change
only once at runtime
Key Difference – Field vs Property in C#
The key difference between field and property in C# is that a field is a variable of
any type that is declared directly in the class.
While property is a member that provides a flexible mechanism to read, write or
compute the value of a private field.