C# Interview Questions and Answers (958) - Page 39

What is operator precedence in C#?

precedence explains the order of evaluation of an expression that has multiple operators. The operator with higher precedence will get the higher priority.

For example,
10 + 2 * 5
In the above code, priority is given to the '*' operator because of its higher precedence (BODMAS rule).
What is operator associative in C#?

Associativity comes in to the picture when we have same operators in an expression. It tells the order of evaluation that which operator should work first.

Binary operators are left associative (executes from left to right).
Example,
4 + 3 + 2
In the above example, first the sum is performed between 4 and 3. Then the result will be summed to 2.

Assignment, conditional, lambda and null coalescing operators are right associative (executes from right to left).
Example,
x = y = z = 6
In the above example, 6 is first assigned to z and then to y and so on.
What are the void expressions in C# programming language?

Void expressions are the empty valued expressions. They do not have any values.

Example,
Console.WriteLine("Hi");
The above code prints the stuff present in the double quotes but we didn't had any value in it.

We cannot generate complex expressions by using void expressions as operands. If we do, it will throw a compile time error.
Example,
Console.WriteLine("Hi") + 5; // Error (Compile time)
The above code will gives a compile time error as we are trying to generate a complex expression with void expression.
What do you mean by 'ref' modifier in C# programming language?

ref is a C# reference which is used to pass the reference types. It cannot pass the value types. This is mostly used in swapping implementations.
We have to declare it while writing a method and again need to use at calling point.

Example,
class MyClass

{
static void Reverse(ref string first, ref string second)
{
string name = first;
first = second;
second = name;
}
static void Main()
{
string firstName = "Krishna";
string lastName = "Vamshi";
Reverse(ref firstName, ref lastName); // Calling method

Console.WriteLine(firstName); // Vamshi
Console.WriteLine(lastName); // Krishna
}
}
In the above code, we have implemented a swap method and interchanging the first and last names using ref modifier.
What do you mean by 'out' modifier in C# programming language?

out is a C# modifier used in two scenarios.

1. Used as a parameter modifier.
2. Used in the declaration of generic types in delegates and interfaces.

It must be assigned just before it comes out from a method and no need to assign before entering into a method.
What is a variable in C#?

Variable is a modifiable value which represents a storage location of the memory.
Variable can be a parameter like out, ref (or) local variable (or) static and instance field (or) an array element.
Variables reside in Stack or Heap of the memory.
What is a stack in C# memory?

Stack is a block of memory which stores parameters, local variables etc.
The special functionality of stack is that it grows when a function comes in and automatically shrinks when it is exited.
Stack is a static memory.
What is Heap in C# memory?

Heap is a dynamic memory block.
It is derived to store objects such as reference type instances.
Any object that is created in a method gets allocated in the Heap and returns that object reference.

During the execution of the program, Objects get filled in the Heap.

Cleaning of objects from the Heap is done by the runtime's Garbage Collector. This will helps our computer to perform faster.

Heap is capable of storing static fields and constants.
What do you mean by definite assignment in C#?

C# obeys Definite Assignment policy.

Definite Assignment policy:
It states that, If there is any free space in the memory, that cannot be accessed by any outside members.

It has three implications,
1. Local variables need to get assigned by any value before they are about to read.
2. Function arguments must be supplied while calling any method.
3. Some of the elements like fields, arrays etc. are automatically initialized by the runtime.
What are the default values in C#?

Default values are the values declared by the compiler by default if a programmer did not assigned any value.

default keyword is used to get the default values.

It results as bitwise zeroing of the memory.

-For all the reference types, the default value will be null.
-For all enum types, it is 0.
-For all bool types, it will be false.
-For all numeric types, it is 0.
-For all char types, it will be \0.
What is an array in C# programming?

Array is a set of variables which are called as its elements. It is a fixed set of variables.
Array elements store in the contiguous block of memory due to their high efficient access.

We can create arrays with char, string etc. For example,
char[] vowels = new vowels[5]; // Array with 5 elements
This is the declaration of array for char's.
The length of the above array is 5.
Now we can create elements like below,
vowels[0] = 'A';

vowels[1] = 'E';
vowels[2] = 'I';
vowels[3] = 'O';
vowels[4] = 'U';
Array index must start with 0.

Other ways of declaring an array are,
char[] vowels = new vowels[5] { 'A', 'E', 'I', 'O', 'U' };
or
char[] vowels = { 'A', 'E', 'I', 'O', 'U' };

What is default element initialization of arrays in C#?

If we have an array which has no initialization of elements such as,
int[] rank = new int[10];
then now if we try to get any element with the array index like,
Console.WriteLine(rank[4]); // prints default value i.e. 0
It will call the default values of the elements.
Default value means the result of bitwise zeroing of the memory.

This process is known as Default Element Initialization of arrays.
What are the escape sequences in C# programming?

These are the special characters which cannot be expressed literally. They start with backward clash (\).

In C#, we have so many escape sequences such as,
-For single quote, it is \'
-For double quotes, it is \"
-For backslash, it is \\
-For new line, it is \n
-For alert, it is \a
-For null, it is \0
-For form feed, it is \f
-For horizontal tab, it is \t
-For vertical tab, it is \v

Escape Sequences cannot be interpreted.
What is a string in C# programming?

Generally string is an immutable object which represents a series of unicode characters.
Immutable means they cannot be modified.

But, for example,
string str = "You";

str = "Me";
In C#, we are able to modify a string.

Hence, the important thing of immutability is about where the string reference is pointing to.

In the above example, str is pointed to "You" and then it has been changed (pointed) to "Me".
What is a Null literal in C#?

Null literal is a null type which indicates that the reference is pointing to no object.

Example,
public class Employee

{
int name, id;
}
static void Main()
{
Employee emp = null;
..............;
}
In the above example, we have an Employee class.
emp is an object created to Employee.
But it is not referring to any object.

If we try to call the variables like,
emp.name;

emp.id;
will throws an error at the runtime which is known as Null Reference Exception.
What is the implicit conversion in C# programming?

In C#, We can convert instances of compatible types, which creates a new value from the old one.
Implicit conversions will happen automatically and hence they are also called as normal conversions.

Example,
int x = 5;

long y = x;
In the above example, we are converting a 32 bit integer (int) to a 64 bit integer (long) implicitly (normally).

There are some conditions that must be satisfied to allow the implicit conversions. They are,
-Implicit conversions can be allowed only if the compiler guarantee's the success of conversion.
-Implicit conversions can be allowed only if there will be no loss of information in the conversion process.
What is the explicit conversion in C# programming?

In C#, We can convert the instances of compatible types, which creates a new value from the old one.
Explicit conversions cast to convert.

Example,
int x = 5;

short y = (short)x;
In the above example, we are converting a 32 bit integer (int) to a 16 bit integer (short) by using cast (short) of that type.

Explicit conversions doesn't need any guarantee of the compiler for the successful conversion.
There might be a loss of information while converting explicitly.
What do you mean by Common Type System of C#?

Common Type System, generally called as CTS tells that all the types including built in types (Ex: System.Int32) derived from a single base class (Ex: System.Object).
Generally C# supports inheritance which is the process of inheriting methods, properties etc. from the base class.
What are the value types in C# programming?

Value types are mostly built in types such as numeric, char, bool etc. which are derived from System.ValueType (derived from System.Object).
Value types have very special behavior in the CLR.

Example for custom value types are structs and enum types.

C# memory does not allocate any separate block of memory to the value types such as heap.
What are the reference types in C#?

These are important as well as complex than value types in C#.

Reference type comprises all the class, array, interface and delegate.
A reference type contains an object and a reference to that object.

These types are derived from the C# reference. class is the most widely used reference type in C#.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories