C# Questions & Answers – Introduction of Array Class

«
»

This set of C# Questions and Answers for Aptitude test focuses on “Introduction of array class”.

1. Select the class which is the base class for all arrays in C#?
a) Array
b) Text
c) arrays
d) Both Array & Text
View Answer

Answer: a
Explanation: None.

2. Select the interfaces implemented by array class.
a) ICloneable, ICollection
b) IEnumerable, IStructuralComparable, IStructuralEquatable
c) ICloneable, ICollection, IList
d) Only IEnumerable, IStructuralComparable, IStructuralEquatable & ICloneable, ICollection, IList
View Answer

Answer: d
Explanation: None.

3. Choose the correct statement about the IComparer interface in C#?
a) The IComparer interface is in System.Collections
b) It defines a method called Compare(), which compares the values of two objects
c) Both The IComparer interface is in System.Collections & It defines a method called Compare(), which compares the values of two objects
d) None of the mentioned
View Answer

Answer: c
Explanation: The IComparer interface is in System.Collections. It defines a method called Compare(), which compares the values of two objects. It is shown here: int Compare(object x, object y). It returns greater than zero if x is greater than y, less than zero if x is less than y, and zero if the two values are equal.
Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

4. Choose the correct statement about the IComparer<T> interface in C#?
a) The IComparer<T> is in System.Collections.Generic
b) It defines a generic form of Compare()
c) Only The IComparer<T> is in System.Collections.Generic
d) Both The IComparer is in System.Collections.Generic & It defines a generic form of Compare()
View Answer

Answer: d
Explanation: IComparer<T> is in System.Collections.Generic. It defines a generic form of Compare(), which is shown here:
int Compare(T x, T y). It works the same as its non-generic relative: returning greater than zero if x is greater than
y, less than zero if x is less than y, and zero if the two values are equal.

5. What does the following property defined in the array class defines in C#?

  1.  public bool IsReadOnly { get; }

a) a property is read only by nature
b) property is true if the array object is read only and false otherwise
c) value is false for arrays
d) all of the mentioned
View Answer

Answer: d
Explanation: A read-only property that is true if the Array object is read-only and false if it is not. This value is false for arrays.
advertisement

6. What does the following property define in C#?

  1. public static int BinarySearch&lt;T&gt;(T[] array, int index, int length, T value)

a) Searches a portion of the array specified by array for the value specified by value
b) The search begins at the index specified by index and is restricted to length elements. Returns the index of the first match
c) If value is not found, returns a zero value
d) All of the mentioned
View Answer

Answer: b
Explanation: Searches a portion of the array specified by array for the value specified by value. The search begins at the index specified by index and is restricted to length elements. Returns the index of the first match. If the value is not found, returns a negative value. The array should be sorted and one-dimensional.
advertisement

7. What will be the output of the following C# code snippet?

  1.  static void Main(string[] args)
  2.  {
  3.      int[] nums = { 5, 4, 6, 3, 14, 9, 8, 17, 1, 24, -1, 0 };
  4.      Array.Sort(nums);
  5.      int idx = Array.BinarySearch(nums, 14);
  6.      if (idx == 9)
  7.      {
  8.          Console.WriteLine(Convert.ToBoolean(1));
  9.      }
  10.      else
  11.     {
  12.         Console.WriteLine(Convert.ToBoolean(0));
  13.     }
  14.     Console.WriteLine("Index of 14 is " + idx);
  15.     Console.ReadLine();
  16. }

a)

True
0

b) Run time error
c)

True
9

d) None of the mentioned
View Answer

Answer: c
Explanation: Using Built in method Sort(), first array is sorted then index of number ’14’ is searched using array class built in method Array.BinarySearch(nums, 14) and hence at last if loop is used to make comparison of index position with random position ‘9’ chosen here.
Output:

        True
        9

8. What will be the output of the following C# code snippet?

  1.   static void Main(string[] args)
  2.   {
  3.       string[] strings = {"beta", "alpha", "gamma"};
  4.       Console.WriteLine("Array elements: ");
  5.       DisplayArray(strings);
  6.       Array.Reverse(strings); 
  7.       Console.WriteLine("Array elements now: ");
  8.       DisplayArray(strings);
  9.       Console.ReadLine();
  10.    }
  11.    public static void DisplayArray(Array array)
  12.    {
  13.        foreach (object o in array)
  14.        {
  15.            Console.Write("{0} ", o);
  16.        }
  17.            Console.WriteLine();
  18.    }

a)

   Array elements:
   beta alpha gamma
   Array elements now:
   ammag ahpla ateb 

b)

   Array elements:
   beta alpha gamma
   Array elements now:
   gamma beta alpha 

c)

   Array elements:
   beta alpha gamma
   Array elements now:
   gamma alpha beta 

d) None of the mentioned
View Answer

Answer: c
Explanation: ‘Reverse()’ a built in method to reverse an array of string defined in array class is used.
Output:

        Array elements:
        beta alpha gamma
        Array elements now:
        gamma alpha beta

9. Which among the following is the wrong way to define and initialize an array of 4 integers?
a) int[] a = {25, 30, 40, 5}
b)

   int[] a;
   a = new int[3]
   a[0] = 25
   a[1] = 30
   a[2] = 40
   a[3] = 5

c)

   int[] a
   a = new int[4]{25, 30, 40, 5}

d)

   int[] a
   a = new int[4]
   a[0] = 25
   a[1] = 30
   a[2] = 40
   a[3] = 5
View Answer
Answer: b
Explanation: None.
 
 

10. Which method will be used to copy content from one array to another array?
a) Copy()
b) copy()
c) Both Copy() & copy()
d) None of the mentioned
View Answer

Answer: a
Explanation: Copy() is a built-in method of array class used to copy the elements from one array to another array

11. What will be the output of the following C# code snippet?

  1.  static void Main() 
  2.  {
  3.      int[] nums = { 1, 2, 3, 4, 5 };
  4.      Console.Write("Original order: ");
  5.      foreach(int i in nums)
  6.      Console.Write(i + " ");
  7.      Array.Reverse(nums);
  8.      Console.Write("Reversed order: ");
  9.      foreach(int i in nums)
  10.      Console.Write(i + " ");
  11.      Console.WriteLine();
  12.  }

a) Run time error
b) 5, 4, 3, 2, 1
c) Compile time error
d) None of the mentioned
View Answer

Answer: b
Explanation: Reverse built in method() of array class is used to reverse the given array.
Output:

5, 4, 3, 2, 1

Sanfoundry Global Education & Learning Series – C# Programming Language.

Here’s the list of Best Books in C# Programming Language.

To practice all areas of C# for Aptitude test, here is complete set on 1000+ Multiple Choice Questions and Answers on C#.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.