C# Interview Questions (part-3)

What is code access security (CAS)?

Code Access Security (CAS) is the security sandbox for .NET. Local apps typically have full trust which means they can do anything. The .NET apps that are hosted in the browser can't do much. In between, just about any security setting can be fine-tuned using CAS. More on.... Code Access Security

Which is the best place to store connection string in .NET projects?

Web.config and App.config are the best places to store connection strings. If it is a web-based application "Web.config" file will be used and if it is a windows application "App.config" files will be used.

What is strong-typing versus weak-typing?

  1. Strong-typing is compiler enforced declaration and maintenance of variable types such that you do not fall into the trap of performing operation and analysis on types which are incompatible with each other.
  2. Weak-typing allows ambiguities and maintenance of variable operations is more, so on the coder, opening up possibilities of creating difficult to trap errors.

Difference between Math.Floor() and Math.Truncate()?

Mat.floor() will always round down and Math.Truncate rounds towards zero.
Math.Floor(2.5) = 2 Math.Truncate(2.5) = 2
Math.Floor(-2.5) = -3 Math.Truncate(-2.5) = -2

What are pointer types in C#?

In an unsafe context, a type may be a pointer type.

type* identifier;

The following types may be a pointer type:

  1. sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool.

  2. Any enum type.

  3. Any pointer type.

  4. Any user-defined struct type that contains fields of unmanaged types only.

What is the smallest unit of execution in .NET?

An assembly is the smallest unit of execution in .Net

How do you convert a value-type to a reference-type?

With the help of Boxing and UnBoxing you can convert any value type to reference type.

int i = 10; Object obj = (Object)i;
int j=(int)obj;

What does the keyword "virtual" declare for a method or property?

The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class. By default a method cannot be overridden in a derived class unless it is declared virtual, or abstract.

Is XML case-sensitive?

Yes - XML is case sensitive

What is the Global.asax used for?

The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level and session-level events raised by ASP.NET or by HTTP modules.

What is Multi-tasking?

It is a feature of modern operating systems with which we can run multiple programs at same time example Word, Excel etc.

How can you reference current thread of the method?

"Thread.CurrentThread" refers to the current thread running in the method. Thread.getCurrentThread() is a static method, meaning it does not operate on an instance of Thread but rather on its class.

How to escape curly brackets in a format string in C#?

In order to output "{" in string.Format you have to escape it like this {{ and to output a "}" you can use }}.

string str = "1, 2, 3"; string output = String.Format(" foo {{{0}}}", str);

Is overriding of a function possible in the same class?

No. It is not possible, the override modifier is used to extend the base class method not for hiding method definitions in the current class.

How do you prevent a class from being inherited?

In C# you use the "sealed" keyword in order to prevent a class from being inherited. In VB.NET you use the "NotInheritable" keyword.

Can a private virtual method be overridden?

No. You can't even declare private virtual methods. Because private methods can ONLY be accessed from the class defining them, hence a private virtual method would be useless.

List the types of Constructors in C#?

Constructors can be divided into 5 types:

  1. Default constructor
  2. Parameterized constructor
  3. Instance constructor
  4. Static constructor
  5. Private constructor

What is the difference between bool and Boolean types in C#?

Both are same, bool is an alias for System.Boolean.

You can assign a Boolean value to a bool variable.

Can you store multiple data types in System.Array?

Array store only single datatype. If you want to store different types, use System.Collections.ArrayList or object[]

Array is reference type or value type ?

Array in .net is of reference type.

All array types are implicitly derived from System.Array, which itself is derived from System.Object . This means that all arrays are always reference types

What are different types of an Array ?

  1. Single Dimension Arrays
  2. Multi-Dimensional Arrays
  3. Jagged Arrays

Can finally bock have return statement?

You can't return from finally block. You will get compiler error:

"Control cannot leave the body of a finally clause"

It is a compile-time error for a return statement to occur in a finally block.

Does C# have a throws clause?

No, because there are no checked exceptions in C#.

Can multiple catch blocks be executed in C#?

No, because only one of the exceptions will be thrown. But, you can have multiple catch blocks associated with a try block ,but only a single catch block can ever handle your exception. Once the proper catch code executed, the control is transferred to the finally block and then the code that follows the finally block gets executed. It doesn't go back into the try block, so you can't end up with a second exception.

Will finally block get executed if the exception had not occurred?

The code inside a finally block will get executed regardless of whether or not there is an exception. Also the catch block is only executed if a managed exception is thrown within the associated try block.
try { return "something"; } finally { // This will always be invoked }