Gate2017 ss Q57

0. Consider the following snippet of a C program. Assume that swap (&x, &y) exchanges the contents of x and y.

{
 int array[]={3,5,1,4,6,2};
 int done =0;
 int i;
 while (done = = 0)
 {
  done = 1;
  for (i = 0; i <=4; i ++)
  {
   if (array [i] < array [i +1])
   {
   swap (& array [i], &array [i+1]);
   done = 0;
  }
 }
 for(i = 5; i > =1; i --)
 {
  if (array [i] > array [ i-1])
  {
   swap (& array [i], &array [i-1]);
   done = 0;
  }
 }
}
printf (“%d “, array [3]);
}
The output of the program is _____.

  • Option : A
  • Explanation :
    For the first time the first for loop will be executed completely, the content of array will be as follows :
    5,3,4,6,2,1
    After the second for executed completely the content of array will be as follows:
    6,5,4,3,2,1
    Array[3] = 3 so ans is 3 Because of the array will start from zero address .
Cancel reply

Your email address will not be published. Required fields are marked *


Cancel reply

Your email address will not be published. Required fields are marked *