Set - 4

Question 1 :

Why do I get a syntax error when trying to declare a variable called checked?

Answer :

The word checked is a keyword in C#.

Question 2 :

Why are there five tracing levels in System.Diagnostics.TraceSwitcher?

Answer :

The tracing dumps can be quite verbose and for some applications that are constantly running you run the risk of overloading the machine and the hard drive there. Five levels range from None to Verbose, allowing to fine-tune the tracing activities.

Question 3 :

What is the syntax for calling an overloaded constructor within a constructor (this() and constructorname() does not compile)?

Answer :

The syntax for calling another constructor is as follows:

class B{
	B(int i)
	{ }
class C : B{
	C() : base(5) // call base constructor B(5)
    { }
	C(int i) : this() // call C()
    { }
	public static void Main() {}


Question 4 :

Why do I get a "CS5001: does not have an entry point defined" error when compiling?

Answer :

The most common problem is that you used a lowercase 'm' when defining the Main method. 
The correct way to implement the entry point is as follows:

class test{
	static void Main(string[] args) {}


Question 5 :

What does the keyword virtual mean in the method definition?

Answer :

The method can be over-ridden.

Question 6 :

What optimizations does the C# compiler perform when you use the /optimize+ compiler option?

Answer :

The following is a response from a developer on the C# compiler team:
We get rid of unused locals (i.e., locals that are never read, even if assigned).
We get rid of unreachable code.
We get rid of try-catch w/ an empty try.
We get rid of try-finally w/ an empty try (convert to normal code...).
We get rid of try-finally w/ an empty finally (convert to normal code...).
We optimize branches over branches:
gotoif A, lab1
goto lab2:
turns into: gotoif !A, lab2
We optimize branches to ret, branches to next instruction, and branches to branches.

Question 7 :

How can I create a process that is running a supplied native executable (e.g., cmd.exe)?

Answer :

The following code should run the executable and wait for it to exit before

using System;
using System.Diagnostics;
public class ProcessTest {
	public static void Main(string[] args) {
		Process p = Process.Start(args[0]);
		Console.WriteLine(args[0] + " exited.");

Remember to add a reference to System.Diagnostics.dll when you compile.

Question 8 :

What is the difference between the System.Array.CopyTo() and System.Array.Clone()?

Answer :

The first one performs a deep copy of the array, the second one is shallow.

Question 9 :

How do I declare inout arguments in C#?

Answer :

The equivalent of inout in C# is ref. , as shown in the following

public void MyMethod (ref String str1, out String str2){

When calling the method, it would be called like this:

String s1;
String s2;
s1 = "Hello";
MyMethod(ref s1, out s2);

Notice that you need to specify ref when declaring the function and calling it.

Question 10 :

Is there a way of specifying which block or loop to break out of when working with nested loops?

Answer :

The easiest way is to use goto:

using System;
class BreakExample{
	public static void Main(String[] args){
		for(int i=0; i<3; i++){
			Console.WriteLine("Pass {0}: ", i);
			for( int j=0 ; j<100 ; j++ ){
				if ( j == 10) goto done;
					Console.WriteLine("{0} ", j);
			Console.WriteLine("This will not print");
		Console.WriteLine("Loops complete.");


Question 11 :

What is the difference between const and static read-only?

Answer :

The difference is that static read-only can be modified by the containing class, but const can never be modified and must be initialized to a compile time constant. To expand on the static read-only case a bit, the containing class can only modify it: -- in the variable declaration (through a variable initializer). 
-- in the static constructor (instance constructors if it's not static).

Question 12 :

What does the parameter Initial Catalog define inside Connection String?

Answer :

The database name to connect to.

Question 13 :

What is the difference between System.String and System.StringBuilder classes?

Answer :

System.String is immutable; System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.

Question 14 :

What is the top .NET class that everything is derived from?

Answer :



Question 15 :

Can you allow class to be inherited, but prevent the method from being over-ridden?

Answer :

Yes, just leave the class public and make the method sealed

Question 16 :

Why are there five tracing levels in System.Diagnostics.TraceSwitcher?

Answer :

The tracing dumps can be quite verbose. For applications that are constantly running you run the risk of overloading the machine and the hard drive. Five levels range from None to Verbose, allowing you to fine-tune the tracing activities.

Question 17 :

Where is the output of TextWriterTraceListener redirected?

Answer :

To the Console or a text file depending on the parameter passed to the constructor.

Question 18 :

How do you debug an ASP.NET Web application?

Answer :

Attach the aspnet_wp.exe process to the DbgClr debugger.

Question 19 :

What are three test cases you should go through in unit testing?

Answer :

1. Positive test cases (correct data, correct output).
2. Negative test cases (broken or missing data, proper handling).
3. Exception test cases (exceptions are thrown and caught properly).

Question 20 :

Can you change the value of a variable while debugging a C# application?

Answer :

Yes. If you are debugging via Visual Studio.NET, just go to Immediate window.

Question 21 :

Describe ways of cleaning up objects?

Answer :

There is a perfect tool provide by .net frameworks calls Garbage collector, where by mean of GC we can clean up the object and reclaim the memory. The namespace used is System.GC 

the run time will maintain a service called as garbage collector. This service will take care of deallocating memory corresponding to objects. it works as a thread with least priority. when application demands for memory the runtime will take care of setting the high priority for the garbage collector, so that it will be called for execution and memory will be released. the programmer can make a call to garbage collector by using GC class in system name space.

Question 22 :

How can you clean up objects holding resources from within the code?

Answer :

Call the dispose method from code for clean up of objects

Question 23 :

Which controls do not have events?

Answer :

Timer control.

Question 24 :

What is the maximum size of the textbox?

Answer :


Question 25 :

Which property of the textbox cannot be changed at runtime? 

Answer :

Locked Property.