Arrays



1. Which of the following statements are correct about arrays used in C#.NET?  

  • A) Arrays can be rectangular or jagged.
  • B) Rectangular arrays have similar rows stored in adjacent memory locations.
  • C) Jagged arrays do not have an access to the methods of System.Array Class.
  • D) Rectangular arrays do not have an access to the methods of System.Array Class.
  • E) Jagged arrays have dissimilar rows stored in non-adjacent memory locations.


2. 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.


3. What is the base class for all arrays in C#?  

System.Array is the base class for all arrays in c#.


4. How do you sort an array in C#?  

The Sort static method of the Array class can be used to sort array items



5. 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 sorting
        Console.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 array
    Console.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 order
     Console.WriteLine("Printing the numbers in the array in desceding order");
     Array.Reverse(Numbers);
     foreach (int i in Numbers)
     {Console.WriteLine(i);
     }
    }
   }
}


6. 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);
   }
 }
}


7. 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);
}
}
}
}


8. 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 identical object.


9. What are the 3 different types of arrays?  

Single-Dimensional
Multi-Dimensional
Jagged


10. Can you use foreach iteration on arrays in C#?  

Yes,Since array type implements IEnumerable, you can use foreach iteration on all arrays in C#.


11. What is Jagged Arrays?  

The array which has elements of type array is called jagged array. The elements can be of different dimensions and sizes. We can also call jagged array as Array of arrays.


12. What is the difference between Array and Arraylist?  

In an array, we can have items of the same type only. The size of the array is fixed. An arraylist is similar to an array but it doesn’t have a fixed size.


13. How can we sort the elements of the array in descending order?  

Using Sort() methods followed by Reverse() method.


14. How we can create an array with non-default values?  

We can create an array with non-default values using Enumerable.Repeat.


15. What is Jagged Arrays?  

A jagged array is an array whose elements are arrays.
The elements of a jagged array can be of different dimensions and sizes.
A jagged array is sometimes called an array–of–arrays.


.Net Interview Question

PHP Interview Question

Java Interview Question

AngularJS Interview Questions