What will be the output of the following C# code snippet? [code] static void Main(string[] args) { int[] nums = { 5, 4, 6, 3, 14,

Hương Trang

New member
What will be the output of the following C# code snippet?
Code:
 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
 
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:
Code:
        True
        9
 

Tìm kiếm

Back
Top