Advanced C# Interview Question and Answers

6/16/2009 No Comment

Advanced C# Questions for experienced professionals.

This post is only intended for experienced professionals of Microsoft.NET. The Advanced C# questions are gathered from job interviews of top companies.

Under what circumstances so you get a "CS5001: does not have an entry point defined" error when compiling a project?
Answer : The issue is related to case-sensitivity of the language. When you uses a lowercase 'm' when defining the Main method, this error message prompts. The correct way to implement the entry point is cited below. The modifier for Main is made public here and inside the class default constructor is always necessary.
class test
{
static void Main(string[] args) {}
}
Can you tell us what optimizations does the C#.NET compiler perform when you use the /optimize+  option while compiling a program?
Answer : The cited below reasons is a response from a developer on the C# compiler team.


  • We can get rid of the unreachable code errors and routines.
  • We can get rid of the try-finally w/ an empty try (convert to normal code).
  • We can get rid of the unused locals (locals that are never read, even if assigned).
  • We can get rid of the try-catch w/ an empty try.
  • We can get rid of the try-finally w/ an empty finally (convert to normal code).
  • We can optimize the branches over branches:
goto if A, lab1goto lab2:lab1:turns into: gotoif !A, lab2lab1:
We can optimize branches to ret, branches to next instruction, and finally branches to branches using this option.

Explain the process of creating a process that is running a supplied native executable (For instance, the cmd.exe of Windows)?
Answer : For running a native executable like Windows Command Prompt, the below cited code should be used to run the executable and then wait for it to exit before continuing. Please also endure that you 
add a reference to System.Diagnostics.dll when you compile the program.
using System;using System.Diagnostics;public class ProcessTest {public static void Main(string[] args) {Process p = Process.Start(args[0]);p.WaitForExit();Console.WriteLine(args[0] + " exited.");}}

Can you tell us what does the C# keyword "Virtual" means in the method definition?
Answer : The keyword "Virtual" means that the class should be inherited. The virtual keyword should be used in the base class. Now a method which is specified as Virtual can be overridden by a derived class implementation of the same method in the definition.

Describe with an example how you will  I declare in/out arguments in C#.NET?
Answer :
 The equivalent of inout in C# .NET is implements using ref. , as shown below
For Eg

public void MyMethod (ref String str1, out String str2)
{...}
While calling the method, the cited below way

String s1;String s2;s1 = "Hello";MyMethod(ref s1, out s2);Console.WriteLine(s1);Console.WriteLine(s2);
You need to specify ref at the time of declaring the function and also while calling it.

Tell us in brief what is the difference between the System.Array.CopyTo() and System.Array.Clone() in C#? 
Answer : Some of the differences are cited below.


CopyTo() -  The destination array need to already exist for this as also it needs to be large enough to hold all the elements in the source array from the index that is specified as the destination.
Clone() -The destination array need not exist yet since a new one will be created right from scratch.

The first one Array.CopyTo()  performs a deep copy of the array, the second one Array.Clone() will performshallow copy. Shallow copy simply copies the elements of the array where as an Deep copy copies all the elements as well as any referenced elements that is present in the array.

We would be presenting more Advanced C# Interview Questions in the coming days. Thank You !!

No comments :

 

Aired | The content is copyrighted and may not be reproduced on other websites. | Copyright © 2009-2016 | All Rights Reserved 2016

Contact Us | About Us | Privacy Policy and Disclaimer