Saturday 5 July 2014

Control Instructions ( Part1 )

Control Instructions (C-FAQ's)



Q1: How many times while loop will get executed in the following code ?


            #include<stdio.h>

            int main()

            {

                        int iCtr=0;

                        while( iCtr<= 300);

                        {

                                    printf(" iCtr  = %d\n ",  iCtr );

                        }                 

                        return 0;

            }


            A.        Infinite Times

            B.        300 times

            C.        301 times

            D         Only Once;

 


            Output:        
                                A. Infinite Times , because the value of iCtr is not getting changed so the while loop condition will always be true.


Q2: How many times will the message "Hello World" get printed ?

          #include<stdio.h>

            int main()

            {

                   int iCtr;

                        for( iCtr=0; iCtr <= 20 ; iCtr++ )

                        {

                                    if ( iCtr < 3)

                                                continue;

                                    else

                                                break;

                                    printf("Hello World\n");

                        }

            return 0;

            }

 

            A.        Infinite Times

            B.        20 times

            C.        21 times

            D.        0 times



            Output:        
                        D.        0 times

Q3: What is the output of the following code?

          #include<stdio.h>

            int main()

            {

                        int i = 2,  j = 3;

                        for( ; j ; printf("%d %d\n",i,j) )

                                    j= i++ <=6;

                        return 0;

            }



            Output:        
        3 1
        4 1
        5 1
        6 1
        7 1
        8 0


Q4: What is the output of the following code?


            int main()

            {

                        char loop=1;

                        while ( loop<= 255 )

                        {

                                    printf("%d ", loop++ );

                        }                     

                        return 0;

            }

           


            Output:
      1 2 3 ... 126 127 -128 -127 ... -2 -1 0 1 2 3 ... 127 -128 -127 ... -2 -1 .... Infinite Times 




<< PREV               [C FAQ's]            NEXT >>

No comments:

Post a Comment