Monday, 21 April 2014

INTERVIEW QUESTIONS ABOUT ARRAYS ,STRINGS,THREADING IN C#

INTERVIEW QUESTIONS ABOUT ARRAYS, STRINGS ,THREADING AND DATAGRID IN C#


ARRAYS

What is the difference between arrays in C# and arrays in other programming languages?
Arrays in C# work similarly to how arrays work in most other popular languages There are, however, a few differences as listed below1. When declaring an array in C#, the square brackets ([]) must come after the type, not the identifier. Placing the brackets after the identifier is not legal syntax in C#.
int[] IntegerArray; // not int IntegerArray[];2. Another difference is that the size of the array is not part of its type as it is in the C language. This allows you to declare an array and assign any array of int objects to it, regardless of the array's length.int[] IntegerArray; // declare IntegerArray as an int array of any sizeIntegerArray = new int[10]; // IntegerArray is a 10 element arrayIntegerArray = new int[50]; // now IntegerArray is a 50 element arrayWhat are the 3 different types of arrays that we have in C#?1. Single Dimensional Arrays2. Multi Dimensional Arrays also called as rectangular arrays3. Array Of Arrays also called as jagged arraysAre arrays in C# value types or reference types?Reference types.
What is the base class for all arrays in C#?
System.Array
How do you sort an array in C#?
The Sort static method of the Array class can be used to sort array items.
Give an example to print the numbers in the array in descending order?
using System;
namespace ConsoleApplication
{class Program{static void Main()
{int[] Numbers = { 2, 5, 3, 1, 4 };//Print the numbers in the array without sortingConsole.WriteLine("Printing the numbers in the array without sorting");
foreach (int i in Numbers){Console.WriteLine(i);}//Sort and then print the numbers in the arrayConsole.WriteLine("Printing the numbers in the array after sorting");Array.Sort(Numbers);
foreach (int i in Numbers){Console.WriteLine(i);}//Print the numbers in the array in desceding orderConsole.WriteLine("Printing the numbers in the array in desceding order");Array.Reverse(Numbers);
foreach (int i in Numbers){Console.WriteLine(i);}}}}
What property of an array object can be used to get the total number of elements in an array?
Length property of array object gives you the total number of elements in an array. An example is shown below.
using System;
namespace ConsoleApplication
{
class Program
{
static void Main()
{int[] Numbers = { 2, 5, 3, 1, 4 };
Console.WriteLine("Total number of elements = " +Numbers.Length);
}}}
Give an example to show how to copy one array into another array?
We can use CopyTo() method to copy one array into another array. An example is shown below.
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);
}
}
}
}
3) Which of these statements correctly declares a two-dimensional array in C#?
int[,] myArray;
int[][] myArray;
int[2] myArray;
System.Array[2] myArray;
Can you store multiple data types in System.Array?No.
What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element's object, resulting in a different, yet identacle object.
How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.

STRINGS


Write a method to reverse a string in C#?
public string Reverse(String str)
{
char[] arr = str.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}

Write a method to reverse the order of the words in a sentence? For example For example for a given string: "This is a pen", convert it to "pen a is This".

public static string WordReversal(string sentence)
{
string[] words = sentence.Split(' ');
Array.Reverse(words);
return string.Join(" ", words);
}
Will the following code compile and run?string str = null;
Console.WriteLine(str.Length);
The above code will compile, but at runtime System.NullReferenceException will be thrown.
How do you create empty strings in C#?
Using string.empty as shown in the example below.string EmptyString = string.empty; 

How do you determine whether a String represents a numeric value?
To determine whether a String represents a numeric value use TryParse method as shown in the example below. If the string contains nonnumeric characters or the numeric value is too large or too small for the particular type you have specified, TryParse returns false and sets the out parameter to zero. Otherwise, it returns true and sets the out parameter to the numeric value of the string.
string str = "One";
int i = 0;
if(int.TryParse(str,out i))
{Console.WriteLine("Yes string contains Integer and it is " + i);
}
else
{
Console.WriteLine("string does not contain Integer");
}

THREADING

What is Thread ?
Threads are the basic unit to which an operating system allocates processor time, and more than one thread can be executing code inside that process. Each thread maintains exception handlers, a scheduling priority, and a set of structures the system uses to save the thread context until it is scheduled. The thread context includes all the information the thread needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process.
When to use multiple threads?
To increase responsiveness to the user and decrease the data processing time of your application. If you are doing intensive input/output work, you can also use I/O completion ports to increase your application's responsiveness.
In what kind of scenarios a single application domain could use multiple threads?
Without modification, the same application would dramatically increase user satisfaction when run on a computer with more than one processor. Your single application domain could use multiple threads to accomplish the following tasks:
Communicate over a network, to a Web server, and to a database.
· Perform operations that take a large amount of time.
· Distinguish tasks of varying priority. For example, a high-priority thread manages time-critical tasks, and a low-priority thread performs other tasks.
· Allow the user interface to remain responsive, while allocating time to background tasks.

What is the use of volatile keyword?
The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.
The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serializeaccess.

What kind of types of fields volatile support?
The volatile keyword can be applied to fields of these types:
· Reference types.
· Pointer types (in an unsafe context). Note that although the pointer itself can be volatile, the object that it points to cannot. In other words, you cannot declare a "pointer to volatile."
· Integral types such as sbyte, byte, short, ushort, int, uint, char, float, and bool.
· An enum type with an integral base type.
· Generic type parameters known to be reference types.
· IntPtr and UIntPtr.

Can I declare a local variable as volatile?Local variables cannot be declared volatile.


DATAGRID


In DataGrid, is it possible to add rows one by one at runtime. That means after entering data in one row next row will be added. 
Yes, for this you have to use datatable and after inserting the first row in datatable u have to bind that datatable to the grid and next time , first u have to add the row in datatable and next bind it to datagrid. keep on doing. u have to maintain the datatable state.
In A Page I have gridview with Options of select and delete using hyperlink when i am selecting any one of then it has to open another page how can it?
Ans 1: You can have template column for select and delete instead of the databound column. In which you can mention thedestination page where you need to navigate.Ans 2: Using RowDataBound event, you can add attribute to the select and delete hyperlink like:e.Row.Cells(CellPosition).Controls(0).Attributes.Add("OnClick","return fnJavascriptFunction()")e.Row.Cells(CellPosition).Controls(0).Attributes.Add("OnClick","return fnJavascriptFunction('"& If any argument &"')").

No comments:

Post a Comment

Receive All Free Updates Via Facebook.