Engineering Placements :: Blog :: Some interview questions


May 31, 2008

hi all these are some questions.......... i will be answering them in the next thread.... till then allow your brain to work....

Q1 int checkzero(int a[], int n)

{

int i;

for(i=0;i<n;i++)

{

    if(a[i]==0)

    {

        return(TRUE);

     }

    }     

   return(FALSE); 

}

Now it checks for every element in array a and does n comparisons to find presence of a zero element

no solve this by doing only one comparison.

 Q2 int func(int n)

    {

        if(n<=1)

         {

              return(1);

         }

         else

          return(n*func(n-2));

}

}

what will be the output of x=func(11);

 

Q3 how to check whether a no. is a power of 2? write a c code?

 

Q4 write a c function to generate the nearest integer value 

int nearestint(float f) 

Keywords: interview, interview question, power of 2

Posted by Engineering Placements - hammer


Comments

  1. Ans 1 :

               int CheckZero (int a[], int n)

               {

                            int i;

                            int k = 1;

     

                            for (i = 0; i < n; i++)

                          {

                                k = k * a[i];

                            }

                            if (k == 0)

                            return true;

                           else

                            return false;

               }

     

    Ans 2 :The value of x is 11*9*7*5*3*1 = 10395

     

    Ans 3 :

        if (!a || (a & (a - 1)))

        {

          printf("%d is not a power of 2", a);

        }

        else

        {

          printf("%d is a power of 2", a);

        }

     

    Ans 4 :

       int nearestint(float f)

      { 

         int i=(int)f;

        float j=f-i;

        if(j >= 0.5)

           int k=i+1;

        else

           int k=i;

        return k;

    }

     

    nicknick on Sunday, 01 June 2008, 19:12 PDT # |


You must be logged in to post a comment.