C# Questions & Answers on Introduction of Array Class for Freshers

1. Select the class which is the base class for all arrays in C#?

a) Array
b) Text
c) arrays
d) Both Array & Text
Answer: a
2. Select the interfaces implemented by array class:

a) ICloneable, ICollection
b) IEnumerable, IStructuralComparable, IStructuralEquatable
c) ICloneable, ICollection, IList
d) Only b & c
Answer: d
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 a & b
d) None of the mentioned
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.
4. Choose the correct statement about the IComparer interface in C#:

a) The IComparer is in System.Collections.Generic
b) It defines a generic form of Compare()
c) Only a
d) Both a & b
Answer: d

Explanation: IComparer 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
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.
6. What does the following property define in C#?
  1. public static int BinarySearch<T>(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
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.
7. What will be the output of given 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
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 given 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
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
Answer: b
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
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 given 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
Answer: b

Explanation: Reverse built in method() of array class is used to reverse the given array.
Output:5, 4, 3, 2, 1

Related

Multiple Choice Questions 8897227754861239836

Post a Comment

emo-but-icon

item