• +91 9723535972
  • info@interviewmaterial.com

CSharp Interview Questions and Answers

CSharp Interview Questions and Answers

Question - 61 : -
Define method overloading with example.

Answer - 61 : -

Method overloading allows programmers to use multiple methods but with the same name. Every defined method within a program can be differentiated on the basis of the number and the type of method arguments. It is a concept based on polymorphism.

Method overloading can be achieved by the following:

  • By changing the number of parameters in the given method
  • By changing the order of parameters passed to a method
  • By using different data types as the passed parameters
For example:

public class Methodoveloading    
  {    
    public int sum(int a, int b)  //two int type Parameters method  
    {    
        return a + b;    
            }    
    public int sum(int a, int b,int c)  //three int type Parameters with same method same as above  
    {   
        return a + b+c;    
    }    
    public float sum(float a, float b,float c,float d)  //four float type Parameters with same method same as above two method 
    {    
        return a + b+c+d;    
    
    }    
  }   

Question - 62 : - What are the control statements that are used in C#?

Answer - 62 : -

You can control the flow of your set of instructions by using control statements and we majorly focus on if statements. There are a few types of if statements that we consider for making situations to control the flow of execution within a program.

These are the 4 types of if statements:

If
If-else
Nested if
If-else-if 
These statements are commonly used within programs.

If statements checks for the user given condition to satisfy their programming condition. If it returns true then the set of instructions will be executed.

Syntax:

If(any condition)
{
//code to be executed if the condition returns true
}
If-else statement checks for the given condition, if the condition turns out to be false then the flow will transfer to the else statement and it will execute the else instructions. In case, the if condition turns out to be true then the if instructions will get executed.

Syntax:

If(condition)
{
//code to be run if the condition is true
}
Else
{
//code to be run if the if-condition is false
}
Nested if statement checks for the condition, if the condition is true then it will check for the inner if statement and keeps going on for the last if statement. If any of the conditions are true then it will execute the particular if instructions and stops the if loop there.

Syntax:

If (condition to be checked)
{
//code
If(condition 2)
{
//code for if-statement 2
}
}
If else-if checks for the given condition, if the condition is not true then the control will go to the next else condition, if that condition is not true it will keep on checking for next else conditions. If any of the conditions did not pass then the last else instructions will get executed.

Syntax:

If(condition 1 to be checked)
{
//code for condition 1
}
Else (condition 2 to be checked)
{
//code for condition 2
}
Else
{
//code will run if no other condition is true
}

Question - 63 : - Explain the concept of boxing and unboxing of the value type and object type in C#.

Answer - 63 : -

Boxing- is a process of converting a value type to an object type where value type is placed on the stack memory, and the object type is placed in the heap memory. This conversion is an implicit conversion and you can directly assign any value to an object, and C# will handle the rest of the conversion on its own.

Example:

public void function()
{
Int a=111;
Object b=a; //implicit conversion
Console.WriteLine(b);
}
Unboxing- it is the reverse process of the boxing process. It is a conversion of the object type to the value type and the value of the boxed object type placed on the heap memory which will be transferred to the value type which is placed on the stack. This conversion of the unboxing process has to be done explicitly.

Example:

public void function()
{
Object b=111;
Int a=(int)b; //implicit conversion
Console.WriteLine(a);
}

Question - 64 : - How can you check if a number is an Armstrong number or not with C#?

Answer - 64 : -

using System;  
  public class ArmstrongDemo  
   {  
     public static void Main(string[] args)  
      {  
       int  n,b,sum=0,num;      
       Console.Write("Enter the Number= ");      
       n= int.Parse(Console.ReadLine());     
       num=n;      
       while(n>0)      
       {      
        b=n%10;      
        sum=sum+(b*b*b);      
        n=n/10;      
       }      
       if(num==sum)      
        Console.Write("Armstrong Number.");      
       else      
        Console.Write("Not Armstrong Number.");      
      }  
  }  
Output:

Enter the Number= 371
Armstrong Number.

Question - 65 : - What is a different approach to the passing parameter in C#?

Answer - 65 : -

Parameters can be passed in three different ways to any defined methods and they are defined below:

Value Parameters:  it will pass the actual value of the parameter to the formal parameter. In this case, any changes that are made into the formal parameter of the function will be having no effect on the actual value of the argument.

Reference Parameters: with this method, you can copy the argument that refers to the memory location into the formal parameter that means any changes made to the parameter affect the argument.

Output Parameters: This method returns more than one value to the method.

Question - 66 : - What is a multicast delegate in C#?

Answer - 66 : -

A multicast delegate holds the references or addresses to more than one function at a single time. Whenever we invoke the multicast delegate, it will invoke all the other functions that are being referred by that multicast delegate. You should use the complete method signature the same as the delegate to call multiple methods. For example:

namespace MulticastDelegate
{
public class Rectangle
{
public void Area(double Width, double Height)
{
Console.WriteLine(@"Area is {0}", (Width * Height));
}
public void Perimeter(double Width, double Height)
{
Console.WriteLine(@"Perimeter is {0}", (2 * (Width + Height)));
}
static void Main(string[] args)
{
Rectangle rect = new Rectangle();
rect.Area(23.45, 67.89);
rect.Perimeter(23.45, 67.89);
Console.ReadKey();
}
}
}
Here, we created an instance of the Rectangle class and then called the two different methods. Now a single delegate will invoke these two methods Area and Perimeter. These defined methods are having the same signature as the defined delegates that hold the reference to these methods.

Creating multicast delegate:

namespace MulticastDelegateDemo
{
public delegate void RectangleDelete(double Width, double Height);
public class Rectangle
{
public void Area(double Width, double Height)
{
Console.WriteLine(@"Area is {0}", (Width * Height));
}
public void Perimeter(double Width, double Height)
{
Console.WriteLine(@"Perimeter is {0}", (2 * (Width + Height)));
}
static void Main(string[] args)
{
Rectangle rect = new Rectangle();
RectangleDelete rectDelegate = new RectangleDelete(rect.Area);
rectDelegate += rect.Perimeter;
rectDelegate(23.45, 67.89);
Console.WriteLine();
rectDelegate.Invoke(13.45, 76.89);
Console.WriteLine();
//Removing a method from delegate object
rectDelegate -= rect.Perimeter;
rectDelegate.Invoke(13.45, 76.89);
Console.ReadKey();
}
}
}

Question - 67 : - How you can implement nullable<> types in C#? explain with the syntax of Nullable type.

Answer - 67 : -

In C#, you cannot put a null value directly into any variable and the compiler does not support it. So, the revised version C# 2.0 provides you with a special feature that will assign a null value to a variable that is called as the Nullable type. You cannot make the nullable types to work with value types. Nullable value can only work with the reference types as it already has a null value. System.Nullable structure creates the instance nullable type, where T defines the data type. This T contains a non-nullable value type that can be any data type you want.
Syntax

Nullable variable_name=null;
OR
Datatype? variable_name=null;

There is no possibility that you can access the value of the nullable value type directly with assigning the value. For getting its original assigned value you have to use the method GetValueOrDefault(). If the value is null then it will provide zero as it is its default value.

Question - 68 : - What do you mean by value types and reference types in C#?

Answer - 68 : -

Value type:
The memory allocated for the value type content or assigned value is stored on the stack. When we create any variable, space is allocated to that variable and then a value can be assigned to that variable. Also if we want to copy the value of that variable to another variable, its value gets copied and that creates two different variables.

Reference Type:
It holds the reference to the address of the object but not the object directly. Reference types represent the address of the variable and assigning a reference variable to another does not copy the data but it creates a second copy of the reference which represents the same location on the heap as the original value. Reference values are stored on the heap and when the reference variable is no longer required it gets marked for garbage collection.

Question - 69 : -
What are various types of comments in C#, explain with example?

Answer - 69 : -

C# supports three types of comments-

1. Single line comment

Syntax:  //single line

2. Multiple line comment

Syntax: /* multiple lines

*/

3. XML comment

Syntax: /// set erro

Question - 70 : - What are the constructors?

Answer - 70 : -

In C#, there is a special method that is invoked automatically at the time of object creation. It initializes the data members of a new object and has the same name as the class or the structure. There are two types of constructors:

Default constructor: it has no parameter to pass.
Parameterized constructor: it is invoked with parameters that are passed to the class during object creation.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners