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
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
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
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<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
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#?
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
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#?
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
View Answer
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 the following C# code snippet?
static void Main(string[] args)
{
int[] nums = { 5, 4, 6, 3, 14, 9, 8, 17, 1, 24, -1, 0 };
Array.Sort(nums);
int idx = Array.BinarySearch(nums, 14);
if (idx == 9)
{
Console.WriteLine(Convert.ToBoolean(1));
}
else
{
Console.WriteLine(Convert.ToBoolean(0));
}
Console.WriteLine("Index of 14 is " + idx);
Console.ReadLine();
}
a)
True 0
b) Run time error
c)
True 9
d) None of the mentioned
View Answer
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?
static void Main(string[] args)
{
string[] strings = {"beta", "alpha", "gamma"};
Console.WriteLine("Array elements: ");
DisplayArray(strings);
Array.Reverse(strings);
Console.WriteLine("Array elements now: ");
DisplayArray(strings);
Console.ReadLine();
}
public static void DisplayArray(Array array)
{
foreach (object o in array)
{
Console.Write("{0} ", o);
}
Console.WriteLine();
}
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
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] = 5View Answer
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
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?
static void Main()
{
int[] nums = { 1, 2, 3, 4, 5 };
Console.Write("Original order: ");
foreach(int i in nums)
Console.Write(i + " ");
Array.Reverse(nums);
Console.Write("Reversed order: ");
foreach(int i in nums)
Console.Write(i + " ");
Console.WriteLine();
}
a) Run time error
b) 5, 4, 3, 2, 1
c) Compile time error
d) None of the mentioned
View Answer
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#.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Apply for Computer Science Internship
- Apply for C# Internship
- Check C# Books
- Check MCA Books
- Check Computer Science Books