Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 26154 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > C# Interview Questions

Now you don't need go anywhere to get the best interview questions on Microsoft technology. We are trying to gather all real interview questions asked from MNCs and put them here. You can also assist us in doing so by submitting .net interview questions. These questions are generally different from certification questions however it may help you in that too.



Page copy protected against web site content infringement by Copyscape
By using Length property, we can find the total number of elements in an array.
Below is an example which explains clearly.

Example:

using System;
namespace ConsoleApplication
{
class Program
{
static void Main()
{
int[] Numbers = { 2, 5, 3, 1, 4 };
Console.WriteLine("Total number of elements = " +Numbers.Length);
}
}
}

We can copy one array into another array by using copy To() method.

Example:

using System;
namespace ConsoleApplication
{
class Program
{
static void Main()
{
int[] Numbers = { 2, 5, 3, 1, 4 };
int[] CopyOfNumbers=new int[5];
Numbers.CopyTo(CopyOfNumbers,0);
foreach (int i in CopyOfNumbers)
{
Console.WriteLine(i);
}
}
}
}

We can do so if we declare the method as static. Then by using Classname.MethodName() we can invoke it.

Example:

class Program

{
static void Main(string[] args)
{
MyBase.Hello();

Console.ReadKey();
}

}

public abstract class MyBase
{
public static void Hello()
{
Console.WriteLine("Hello");
}
}

Let us first make the environment.First let us declare an Employee class

class Employee

{
public string EmpName { get; set; }
public int Salary { get; set; }
}


Then populate some records to it

private static List<Employee> GetEmpRecord()

{
List<Employee> empCollection = new List<Employee>();
empCollection.Add(new Employee { EmpName = "Emp1", Salary = 10000 });
empCollection.Add(new Employee { EmpName = "Emp2", Salary = 3456 });
empCollection.Add(new Employee { EmpName = "Emp3", Salary = 14256 });
empCollection.Add(new Employee { EmpName = "Emp4", Salary = 15000 });
empCollection.Add(new Employee { EmpName = "Emp5", Salary = 10000 });
empCollection.Add(new Employee { EmpName = "Emp6", Salary = 6000 });
empCollection.Add(new Employee { EmpName = "Emp7", Salary = 2000 });
empCollection.Add(new Employee { EmpName = "Emp8", Salary = 5000 });
empCollection.Add(new Employee { EmpName = "Emp9", Salary = 7000 });
empCollection.Add(new Employee { EmpName = "Emp10", Salary = 7000 });
return empCollection;
}

Finally let us write the program

var empCollection = GetEmpRecord();

int whichEmpSalary = 3;

var employees = (from emp in empCollection
group emp by emp.Salary into g
orderby g.Key descending
select new
{
EmpRecord = g.ToList()
}).ToList();

employees[whichEmpSalary - 1].EmpRecord
.ForEach(i => Console.WriteLine("Emp Name {0} earns {1}", i.EmpName, i.Salary));

Console.ReadKey();


Let us analyze the program. First we are grouping up the Employee Record Set by their salary.If we put a break point there and check , we will find that there are a total of 8 elements since both Emp1 and Emp5 are earning 10000 while Emp9 and Emp10 are earning 7000 each.But it is not sorted. In order to sort the record set, we have introduce the order by clause that will guaranteed to bring the result in a sorted order.Finally we are projecting the record using the select statement.At this stage we will get the count as 8 only but with sorted record set.Suppose we want to find out the third highest salary which should be "Emp1" and "Emp5" (both earning 10000 each). The first will be "Emp4" with 15000 and second is "Emp3" with Salary = 14256.Since it is a collection, we can use index as shown under to obtain the desired result.
var result = employees[2];

will give the desire result.
Output
Emp Name Emp1 earns 10000

Emp Name Emp5 earns 10000


//Lambda version
empCollection

.GroupBy(g=>g.Salary)
.OrderByDescending(o=>o.Key)
.Select(s=>new{EmpRecord = s.ToList()})
.ToList()[whichEmpSalary - 1].EmpRecord
.ForEach(i => Console.WriteLine("Emp Name {0} earns {1}", i.EmpName, i.Salary));

Its an ambiguity arises when two class inherits the same base class and one more class inherits these two classes. If class Derived A and class Derived B inherits class BaseClass where BaseMethod() is a method and class Derived C inherits class Derived A and class Derived B and override the method BaseMethod() , then there will be a confusion for class Derived C to inherit the method BaseMethod(). The confusion is from which class it will inherit. It is the diamond of death in OOPS jargon.

When an object is created, if the object size is greater than 85000 bytes, it will be created in a separate heap called, Large Object Heap. If the size is lesser than 85000 bytes, it will be created in a heap called, Smaller Object Heap.When the Garbage Collector collects the objects, it will perform the compaction on Small Object Heap for more contiguous free memory. However, in case of Large Object Heap, it will not perform any compaction, as it takes more resources to move the Large Objects.

If a language is CLS-Compilant(Common Language specifications), and also if the class developed by using CLS specifications, then it can be inherited in another CLS-Compilant language.Both F# and C# are CLS-Compilant languages. So, it is possible to inherit the base class in C# where the base class is developed in F#.

While sending an object from a source to destination, an object will be converted into a binary/xml format. This process is called serialization .

After it is reached to destination, it will be converted back to original object. This process is called deserialization .

Serialization/deserialization will avoid problems like byte reordering,memory layout etc and gives better performance.

Byte Reordering :
Machines might be of type Big-Endian or Small-Endian. Depends on Endian type, most significant bytes will be stored in left-side or right-side. When an object traverse from One Endian machine to another Endian machine, these bytes needs to be reordered. By serializing/deserializing we are avoiding this.

Memory Layout :
Different programming languages might store the object in different ways. When the object is traversed,it needs to be stored in memory according to language architecture. By serializing/deserializing we can avoid this problem.

var ans =  Enumerable.Range(0, 500).Where(i => i % 7 == 0 || i % 11 == 0).Sum();


Enumerable.Range generates a sequence of integral numbers within a specified range.
In this case it is between 0 to 500. After that inside the Where extension method we are checking if the number is divisible by 7 as well as 11 or not.Which ever number satisfies the condition, we are taking a sum of that.

Result: 27660

Delegate.Invoke: Executes synchronously, on the same thread.

Delegate.BeginInvoke: Executes asynchronously, on a threadpool thread.

Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.

Control.BeginInvoke : Executes on the UI thread, and calling thread doesn't wait for completion.

NOTE: This is objective type question, Please click question title for correct answer.

namespace DynamicPolymorphism

{
public abstract class Animal
{
public abstract void Run();
}

public class Tiger : Animal
{
public override void Run()
{
Console.WriteLine("Tiger runs at 80 Km/hr");
}
}

public class Lion : Animal
{
public override void Run()
{
Console.WriteLine("Lion runs at 60 Km/hr");
}

}

class Program
{
static void Main(string[] args)
{
Animal objAnimal = new Lion();
objAnimal.Run();
objAnimal = new Tiger();
objAnimal.Run();
Console.Read();
}
}
}


Here i have a abstract class called Animal. This Animal class contains an abstract method called Run(). Note that here method declaration is only written and not implementation.
Now I have two classes called Tiger and Lion. These two classes inherits from base class Animal.
  public class Lion : Animal

public class Tiger : Animal

I override the base class Run() method in my Lion and Tiger class.
The keyword override is used to override the base class function.
public override void Run() ---> in Tiger and Lion class overrides the base class Run() function.

I am printing in Lion class
 Console.WriteLine("Lion runs at 60 Km/hr");

I am printing in Tiger class
 Console.WriteLine("Tiger runs at 80 Km/hr");

In program class the real implementation is done.
At first I am creating base class reference pointing to derived class.
In first line I am creating Animal class reference pointing to Lion class using "new" keyword
Animal objAnimal = new Lion();
Then I am calling Run() function, now it calls the Run() function of Lion class and executes the line
 Console.WriteLine("Lion runs at 60 Km/hr"); 

Now I am reassigining or changing the reference from Lion class to Tiger class . Note that I am using the same object "objAnimal"
 objAnimal = new Tiger();   
Then I am calling Run() function, now it calls the Run() function of Tiger class and executes the line
 Console.WriteLine("Lion runs at 60 Km/hr"); 

For Integers
           int i = 10;

int j = 15;
i = i + j;
j = i - j;
i = i - j;
Console.WriteLine("The swapped values are i={0},j={1}", i, j);

In the above code I am adding the two variables. Here i+j = 25 and assigning to i. Now I am subtracting j from i which is 25-15 is equals 10 and assigning to j. In third line I am subtracting j (currently it is 10) from i and assigning to i. So here 25-10=15. At last we will be having the values swapped i=15 and j=10.

For String:
--------------
I used replace function here. If any one has better solution then this please let me know.

            string a = "Naga";

string b = "Sundar";
a = a + b;
b = a.Replace(b, "");
a = a.Replace(b, "");
Console.WriteLine("The swapped values are a={0},b={1}", a, b);

I am using the same logic that I used for Integers. I am appending string "a" and "b" and saving it in "a". Now the variable "a" contains text "Nagasundar". From here I am using replace function and eliminates "Sundar". Now string "b" contains "Naga". Once again I am using replace function so that I am removing string "b" (currently "Naga") from "a". So I am getting "Sundar" in variable "a" and "Naga" in variable "b"

NOTE: This is objective type question, Please click question title for correct answer.

NOTE: This is objective type question, Please click question title for correct answer.

Found this useful, bookmark this page link to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape
Navigate to Page: 1 | ... | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 |

 More Exclusive C# Interview Questions and Answers here


Found interesting? Add this to:


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/20/2013 4:14:42 AM