OOPs in C# Interview Questions


Question 1:

What is Boxing and Unboxing in C#?

Boxing -  Boxing is the process to convert value type into the Object type.

Unboxing  - Converting object type into the value type.

Example:

        
        public void BoxingTest()
        {
            int number = 16;
            //Boxing -- converting number in object
            object obj = (object)number;

            //Unboxing -- converting object obj back to value type and printing on console
            Console.WriteLine((int)obj);
        }

Output

16
Question 2:

Why we need boxing in C#. Explain.

Boxing is used to convert value type (primitive data type) into Reference type.

For example, suppose you want to use ArrayList of type double of int i.e. ArrayList<int> or ArrayList<double> then we need to convert Int or double to object type as ArrayList does not support object types.

Usage

Boxing is used to pass the value type parameters as reference type parameters.

Question 3:

What is Value type in C#?

Value types are directly derived from System.ValueType and directly stores the values.

Value type does not contains the NULL values instead each value type has a default constructor that assign it to default value. E.g. int has default value 0.

In C# we have major categories of Value types.

  • Struct
  • User defined struct
  • Enumration
  • Premitive types
    • Int
    • float
    • double
    • boolean
    • short
    • long
    • decimal
    • char

 

Question 4:

What is Reference type in c#? Explain.

Reference types stores the address of the memory location and points the memory location.

Lets take a real life example to understand the reference types, We can think the reference type as the business card. A business card stores basic information and contact details of a bussiness man, i.e it is as a reference to that person. When we need to contact to him then we can refer the business card.

Reference Type

Similary reference types stores the reference of a some memory location. So when we need to access the value stored in that memory location then we can refer that using reference type.

 

Question 5:

What is the difference between Object and Reference variable?

Consider the below example

Student stu = new Student()

Here Student is a Class.

We are creating an object of class Student using new keyword new Student() will create an object of type student and allocate memory in the heap for created object.

This will return the memory address that will be stored inside the variable stu. So stu is a reference variable of Type Student which is storing the address of object created in the heap. Stu is pointing to the object in the heap.

Object vs Reference variable

Multiple reference variable can points to same object. Consider below example

Student stu1 = new Student();
Student stu2 = stu1;

In the above exapmple stu1 and stu2 are 2 reference variable of type Student, which are pointing to the same object in the heap.


Question 6:

What is pass by value and Pass by reference in C#?

We can call a method then there are two ways to pass arguments to the calling method.

  • Pass by Value
  • Pass by Reference

By default C# uses pass by value approach by calling a method.

Pass by Value

By default when we call a method in C# and pass parameter in that method, C# compiler creates a new copy of each variable that is passed into the method. So, any changes that we make in the method parameters will apply only in that parameter itself inside the scope of the method.

Consider the below example

 class Program
   {
        static void Main(string[] args)
        {
            int number = 10;

            IncrementNumber(number);
            Console.WriteLine($"Value of number in Main method ={number}");

            Console.ReadLine();
        }

        static void IncrementNumber(int num)
        {
            num+=15;
            Console.WriteLine($"Value of num inside Increment Number ={num}");
        }
   }

Output

Value of num inside Increment Number =25
Value of number in Main method =10;

In the above example when we call the IncrementNumber (num) method by passing number variable then C# compiler creates a new copy of num variable and copies the value inside number variable into num argument variable. After this, it does not matter, how many changes we make in num variable it does not affect the value stored in the number variable. This behaviour is known as pass by value and is default in C#.

Pass By Ref

Pass by reference is another type to pass parameter in a method. C# provides ref keyword for passing arguments by reference in a method.

Consider the below example

   class Program
   {
        static void Main(string[] args)
        {
            int number = 10;

            IncrementNumber(ref number);
            Console.WriteLine($"Value of number in Main method ={number}");

            Console.ReadLine();
        }

        static void IncrementNumber(ref int num)
        {
            num+=15;
            Console.WriteLine($"Value of num inside Increment Number ={num}");
        } 
  }

Output

Value of num inside Increment Number =25
Value of number in Main method =25;

In the above example when we call the IncrementNumber (num) method by passing number variable by reference, then C# compiler will pass the reference of number variable and copies the reference of number variable into num argument variable. So both number and num are pointing to the same memory location. After this, If we make any change in through the number or num variable it will be reflected in both variable as both pointing to the same memory address. This behaviour is known as pass by reference in C#.

Note: In the above example we have used C# string interpolation ("${}") syntax to format string. Which is new feature of C# 6.0. Refer here to explore more new features of C# 6.0

Question 7:

How the value types are stored in C#? Explain

Value types in the C# are stored in the stack if they are declared inside the local method. If Value types are declared inside a class (reference type) then compiler allocates memory in Heap.

Memory allocation for value type and reference type

Generally, it completely depends on JIT compiler how to allocate memory to variables. For the optimization purpose JIT compiler sometimes stores local which are involved in mathematical operations inside registers, but we can't guarantee that if there are not sufficient registers are available then Stack is used.

Question 8:

Where Reference Types are stored in C#?

Reference types are stored inside the Heap. Heap memory is for the dynamic memory allocation. Heap is space inside the RAM (Random Access Memory)

Memory allocation for value type and reference type

Question 9:

Why we use operator overloading? Explain some cases where we can use operator overloading.

Operator overloading makes the code cleaner and simpler architecture. 

If Operator overloading is not available then we have to write methods in the class to perform the same logic and have to call the method by name.

Usage

Adding or subtracting two complex numbers/ imaginary numbers (a + b) -  we can overload + and - operator to perform add and subtract operations respectively on complex number

Adding or subtracting rational numbers - we can overload + and - operator to perform add and subtract operations on rational number (a/b + c/d)

We can overload both unary and binary operators.

Question 10:

How to pass the variable number of arguments in C#?

C# provides param keyword, which allows to pass the variable number of arguments in a method. We can pass comma (,) separated list of specified type while calling the method.

Length of param argument will be zero if we do not pass any argument while calling the method.

Example

    
   class Program
    {
        static void Main(string[] args)
        {
            // Passing 4 aarguments
            PrintNames("Ashish", "Amit", "User1", "User2");

            //passing 2 arhuments
            PrintNames("Ashish", "Shukla");

            //calling without passing any arguments
            PrintNames();
            Console.ReadLine();
        }

        static void PrintNames(params string[] names)
        {
            foreach (var name in names)
            {
                Console.WriteLine(name);
            }
        }
   }
Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media