OOPs Interview Questions and Answers in C#
OOPs in C# - Interview Questions
1. What is OOP?
OOP stands for object-oriented programming language. It means that the program code is written in such a way that the methods, functions, and data is contained within an object.
This approach makes the program structure modular and easier to execute. It also helps in achieving the reusability concept i.e we don’t have to write a certain code again and again in the program as we can bind that code in a method in a class and use it for as many times as we want.
2. What are the classes and objects?
A class in OOP is a collection or template of objects and an object is an instance of a class.
We can create many objects of a single class and these objects inherit all the properties, methods, and data of that class.
3. Difference between abstract class and interface in C#?
- An abstract class is an incomplete class whose object cannot be instantiated. The abstract class allows us to create functionality that subclasses can implement or override. We use a keyword abstract before we declare a class to be abstract.
- The interface is a blueprint of a class but it has no implementation. The things that are contained in the interface are the declarations of events, indexers, methods, or properties.
- An abstract class contains both declaration and definition part whereas an interface contains only the definition part.
- A class can only use one abstract class but many interfaces can be used in a class.
- An abstract class can not implement multiple inheritances whereas an interface can do so.
4. Explain early binding and late binding in C#.
Binding is the process in which association of method call to method body takes place.
The two types of binding are:
- Early or static binding
-
- In early binding, the methods and properties are detected and checked during compile time itself. Also, the compiler knows about what kind of object it is.
- If a method or property does not match or exist or it has some data type problem, then an exception is thrown by the compiler during the compile time.
- Late binding or dynamic binding
-
- In late binding, the methods and properties are detected and checked during runtime.
- Methods and properties which bypass compile-time checking are dynamic types that are detected during runtime.
- The compiler does not know what type of object or what types of methods and properties are contained in an object so it bypasses the compile-time checking which was handled by run time.
- This is also called polymorphism.
5. What are the different ways of method overloading?
Method overloading means when two or more than two methods have the same name but different parameters.
The three ways of method overloading are:
- We can change the number of arguments or parameters.
For eg:
public static int sum(int num1, int num2)
{
//method body;
}
public static int sum(int num1, int num2, int num3)
{
// method body;
}
- We can change the data type of the arguments.
For eg:
public static int sum(int num1, int num2)
{
//method body;
}
public static double sum(double num1, double num2)
{
// method body;
}
- We can change the order of data types of arguments.
For eg:
public void address(string locality, int home_no)
{
// method body;
}
public void address( int home_no, string locality)
{
// method body;
}
6. Explain access modifiers and their types in C#.
- Access modifiers in C# are used to specify the scope of accessibility of a member of a class or type of the class itself.
- Access modifiers are a significant part of object-oriented programming. They help in achieving the encapsulation of OOP. They specify who can or who cannot access certain features in a class.
There are 6 types of access modifiers in C#:
- Public: It allows full and easy access to the members of the class or the class itself which has been declared public.
- Private: It limits the access of member functions and data of a class to itself that is declared private. All the members of a class are private by default in the program.
- Protected: The member functions of a protected class are allowed for their access by the definition class or by the class that inherits the protected class.
- Internal: The internal members can be accessed only within the files contained in the same assembly(.dll) but not by the derived classes in that project.
- Protected internal modifier: The access is available to current assembly files and the derived classes and members within the current project.
- Private protected: The access is available to the containing class or types derived from the containing class within the current project in an assembly.
7. What do you mean by virtual method?
A virtual method is a method that can be redefined in the derived class. A virtual method can be implemented in both the base class as well as the derived class.
A virtual method is declared in the base class using the virtual keyword. It is used so as to modify a method or event declared in the base class and allows it to be overridden in the derived class.
For a method to be overridden, it must be declared with override keyword in the derived class.
For eg:
using System;
namespace ConsoleApp11
{
public class one
{
public virtual void num()
{
Console.WriteLine("one :: num()");
}
}
public class two:one
{
public override void num()
{
Console.WriteLine("two :: num() ");
}
}
class Program
{
public static void Main(string[] args)
{
two n = new two();
one m = n;
m.num();
n.num();
}
}
}
Output:
two:: num()
two:: num()
- The num() method gets overridden in the derived class which changes its behavior.
8. What is boxing and unboxing in C#?
Boxing is the conversion of a variable from value type to reference type or object type. It is an implicit conversion.
For eg:
int i= 10;
Object obj= i ; // boxing
Unboxing is just the reverse of boxing as a variable is converted from an object or reference type to a value type. This is an explicit conversion.
Here, the value of the variable is extracted from its reference type and assigned to the value type.
For eg:
object obj = 18;
Int i = (int)obj; // unboxing
9. Can “this” be used in a static method?
A static method is the one that does not need the creation of an object. In other words, static methods do not belong to a particular object. Their existence is independent of the creation of the object.
On the other hand, “this” keyword always points to a reference or instance of a class.
Simply there is no object in the static method, therefore, “ this “ keyword cant be used in a static method.
10. Explain generics in C#.
Generics allows us to write a class or method that can work with any data type. It allows us to define the specification of the data type of the data of a class or a method until it is used in the program.
It helps in defining the class or method with placeholders. These placeholders are replaced with specified types by the Compiler during the compilation process.
We can Implement generics by using < >. Generics allow the reusability of a class or method as they become functional for any data type value supplied to them.
For eg:
using System;
namespace generics1
{
class Gen<T>
{
public Gen(T var)
{
Console.WriteLine(var);
}
}
class Program
{
static void Main(string[] args)
{
Gen<int> g1= new Gen<int>(10);
Gen<string> g2 = new Gen<string>("generic class");
}
}
}
11. What do you know about namespaces in C#?
A namespace is used to separate one set of names from another . It is of great importance as the names of a class declared in one namespace do not conflict with the same set of names of classes in different namespaces.
A namespace is declared by using the keyword namespace:
For eg:
namespace <namespace_name>
{
// code
}
12. What is a delegate and what are its uses?
A delegate is a function pointer that holds a reference to a method. The reference can be changed at the run-time.
Delegates are mainly used to implement events and call-back methods. The delegates are implicitly derived from System.Delegates class.
13. What is the difference between String and StringBuilder in C#?
Both string and StringBuilder is a sequence of characters. The difference between them is:
- A string is immutable. Immutable means once we create an object for string it can not be modified later. Any operation like insert, replace, or append needed to change the string will need their separate objects to be created and the old value will simply be discarded.
- It belongs to System
For eg:
string s = “hello” ;
s += “hey”; // new object will be created and the old one will be discarded.
- A StringBuilder is mutable which means once we create an instance for a StringBuilder, the same instance can be used over and over again for a different set of operations like insert, replace, or append.
- It belongs to Text namespace.
For eg:
StringBuilder sb = new StringBuilder(“ ”);
sb.Append(“hello”);
string str = sb. ToString();
14. Is Multiple inheritance present in C#?
Multiple inheritance allows the features and traits of two or more classes to be inherited by a single class. In other words, a single child class can have properties of two or more parents in itself.
C# does not support the feature of Multiple Inheritance because it adds too much complexity in the program providing very few benefits.
But there is a key feature that can be used in C# called interfaces that are used to achieve multiple inheritances in C#.
An interface is somewhat like a class but it only contains the declaration of the members but has no implementation. It is a reference type that only includes the signatures of methods, properties, or indexers. To implement an interface member, the corresponding member of the implanting class should be declared public, non-static, and have the same name and signature as an interface member.
For eg:
using System;
namespace interface1
{
public interface pare1
{
void message();
}
public interface pare2
{
void message();
}
public class child: pare1, pare2
{
public void message()
{
Console.WriteLine("multiple inheritance using interface");
}
}
class Program
{
static void Main(string[] args)
{
child c = new child();
c.message();
Console.ReadKey();
}
}
}
15. What do you mean by multi-dimensional and arrays in array in C#?
Multi-dimensional array:
In multi-dimensional arrays, the data or the array elements are stored in a tabular form or matrix form.
These arrays are also called rectangular arrays due to the presence of rows and columns making it 2D or 3D.
Multidimensional arrays are declared by using commas within the square brackets.
A jagged array is a collection of arrays or we can say an array of arrays is called a jagged array.
- The arrays present in the jagged array can be of any size or length.
- In other words, the jagged arrays store arrays instead of actual values.
- A jagged array is declared by using two square brackets.
- The first bracket specifies the number of separate arrays stored in the jagged array.
- The second bracket tells about the type of arrays stored in the jagged array. i.e, whether single-dimensional array or multi-dimensional array.