Share It

Sunday, 6 July 2014

Functions (Part2)

Functions (C-FAQ's)



Q1: What is the output of the following code?

            int iReverse(int iNum)

            {

                        if( iNum == 0)

                                    return 0;

                        else

                                    printf("%d ", iNum);

                        iReverse(iNum--);

            }

            int main()

            {

                        int iNum=6;

                        iReverse(iNum);

                        return 0;

            }

 

            A.        6 5 4 3 2 1

            B.        1 2 3 4 5 6

            C.        6 5 4 3 2 1 0

            D.        6 till stack over flow

 

           


            Output:        
                        D. 6 till stack over flow
                         iReverse(iNum--) will not make any change in the value of iNum because we are using a post decrement operator here.          


Q2: What is the output of the following code?

            int iModify(int iNum1, int iNum2)

            {

                        int x,y;

                        x= iNum1 * iNum2;

                        y=iNum1 + iNum2;

                        return (x,y);                       

            }

            int main()

            {

                        int iA=5, iB=10;

                        iA=iModify(iA,iB);

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

                        return 0;

            }

 

            A.        iA=5

            B.        iA=10

            C.        iA=50

            D.        iA=15

 

         


            Output:        
      iA=15

                       

Q3: What is the output of the following code?


            void display (char **ptr)

            {

                        char *msg;

                        msg=(ptr += 4)[-2];

                        printf("%s\n",msg);

            }

            int main()

            {

                   char *msg[]={"Hi","Hello","world","function"};

                        display(msg);

                        return 0;

            }

 

            A.        function

            B.        world

            C.        Hello

            D.        Hi

               

         


            Output:
               world

            Explanation:

                        msg is an array of strings.
HI
Hello
World
Function





msg
msg+1
msg+2
msg+3
msg+4




            In display(), initially value of ptr is base address of msg.
                        ptr=msg;
            Then after expression ptr += 4, ptr is pointing to the address msg+4.
            Now ptr[-2] will get execute. Its means *(ptr - 2). Now ptr will move back two positions.
                        msg = *(msg+4 -2 );
                        msg = *(msg+2);
                        msg  =  "World";





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

Functions (Part1)

Functions (C-FAQ's)



Q1: Can you find out the error in the following code?

            func(int iA, int iB)

            {

                        int iA;

                        iA=40;

                        return iA;

            }

            int main()

            {

                        printf(" func = %d\n ", func(5,10) );

                        return 0;

            }

 

            A.        Missing parentheses in return statement

            B.        Redeclaration of iA

            C.        func should be defined as int func(int iA, int iB)

            D.        None of the above

 

           


            Output:        
                                B.        Redeclaration of iA.
        In function 'func':
        Line 3: error: 'iA' redeclared as different kind of symbol
        Line 1: error: previous definition of 'iA' was here
           


Q2: Can we use two return statement in a function ?

         


            Output:        
                        Yes we can you two return statements in a function.
                        Example:-
                                    int iMax ( int iA, int iB)
                                    {
                                                if ( iA > iB )
                                                            return  iA ;
                                                else
                                                            return iB;
                                    }
                       

Q3: Can we use two successive return statement in a function?

         


            Output:
                        No we can't use two successive return statements in a function.
                        Example :-
                                    int iMax()
                                    {
                                                return 100;
                                                return 200;
                                    }
                        Compiler will return an error.

Q4: What is the output of the following code?


            int func (int iNum)

            {

                        return (iNum++);

            }

            int main()

            {

                   int iNum= func(10);

                        printf(" iNum = %d \n ", --iNum);

                        return 0;

            }

 

            A.        iNum = 10

            B.        iNum = 9

            C.        iNum = 11

            D.        iNum = 8

               

           


            Output:
      iNum = 9 




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

Saturday, 5 July 2014

Expressions ( Part2 )

Expressions (C-FAQ's)



Q1: Do you know in which order Relational, Logical, Arithmetic and Assignment operators get evaluated?

 

  A.     Relational, Logical, Arithmetic, Assignment

  B.      Arithmetic, Relational, Logical, Assignment

  C.      Arithmetic, Logical, Relational, Assignment



           


            Output:        
                                B.        Arithmetic, Relational, Logical , Assignment.

                       


Q2: What is the output of the following code?

          int max(int iA, int iB)

            {

                        iA>iB? return (iA): return(iB);

            }

            int main()

            {

                        printf(" max = %d\n ", max(5,10) );

                        return 0;

            }

 

                   


            Output:        
                        Compiler will return an error because of not properly using return.
                        Expression should be written as :-
                                    return ( iA > iB ? iA: iB ) ;
                       

Q3: What is the output of the following code?

         

            #include<stdio.h>

            int main()

            {

                        int iNum = ( -1, 0, 2, 3, 5 );

                               

                        printf(" value of iNum = %d\n", iNum );

                        return 0;

            }

 

         



            Output:
           value of iNum = 5
                        Because comma operator has left to right associativity. The left operand is always evaluated first, the result of evaluation is discarded before the right operand.
                        Here right most is operand is 5.
                       

Q4: What is the output of the following code?


            int main()

            {

                        int i=3, j=-2, k=0, a, b, c, d;

                        a=i || j || k;

                        b= i && j && k;

                        c=i || j++  &&  k;

                        d= i && j || --k;

                        printf("%d %d %d %d\n ", a ,b ,c ,d);

                                               

                        return 0;

            }

 

            A.        1 0 1 1

            B.        1 1 0 1

            C.        1 1 1 1

            D.        Output may vary from compiler to compiler.

               

           


            Output:
           1 0 1 1




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


Expressions ( Part1 )

Expressions (C-FAQ's)



Q1: Can you explain

                   iNum = iNum++ * iNum++      expression result is undefined , but

                   iNum = iNum++ || iNum++    result is always same?


           


            Output:        
                                According to the C standard an object's stored value can be modified only once between two sequence points.

                        Sequence points occurs at following places:-
                                    1)        at the end of full expression( which is not a sub expression ).
                                    2)        at && , || and ?: operators.
                                    3)        at a function call, after evaluation of all the arguments, just before the actual call.
                       
                        So first expression is being modified twice between sequence points, that is why behavior is undefined. But the second expression is getting modified once before sequence point || and once after that.


Q2: Can you rewrite the following expression?

          iNum1 <= 50 ? iNum2=100 : iNum2=200;

         


            Output:        
                        The above expression can be rewrite as :-
                                    iNum2 = iNum1 <= 50 ? 100:200 ;
                       

Q3: Can you rewrite the following expression?

          iNum1 <=50 ? iNum2=200: iNum3=200;


         



            Output:
                        The above expression can be rewrite as:-
                                    *( ( iNum1 <= 50 ) ? &iNum2 : &iNum3 ) = 200 ;

Q4: What is the output of the following code?


            int main()

            {

                        int iNum=2;

                        printf("%d %d\n ", ++iNum, ++iNum );

                                               

                        return 0;

            }

 

            A.        3 4

            B.        4 3

            C.        4 4

            D.        Output may vary from compiler to compiler.

               

           


            Output:
                        D.        Output may vary from compiler to compiler
      4 4 (In GCC Compiler)      




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

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 >>

Control Instructions ( Part2 )

Control Instructions (C-FAQ's)



Q1: What is the output for the program given below?


            #include<stdio.h>

            int main()

            {

                        int x=0, y=10, z=20;

                        *( (x) ? &y: &z) = x ? y : z ;

                        printf(" %d %d %d\n ", x, y, z );

                                 

                        return 0;

            }


           

            Output:        
                0 10 20
                               


Q2: Predict the output for the following code ?

          #include<stdio.h>

            int main()

            {

                   int a=2, b=5;

                        if( a%2 = b%5 )

                                    printf("Hello World\n");

                       

            return 0;

            }

 

            A.        Error: " Invalid Rvalue assignment "

            B.        Error: "Expression Syntax"

            C.        Error: "Invalid Lvalue assignment"

            D.        Hello World



            Output:        
                        C.        Error: "Invalid lvalue assignment"



Q3: What is the output of the following code?

          #include<stdio.h>

            int main()

            {

                        int i = 5;

                        float j = 5.0 ;

                        if (  i == j )

                                    printf(" Equal \n");

                        else

                                    printf("Not Equal \n");

                        return 0;

            }



            Output:        
                Equal   
 


Q4: What is the output of the following code?


            int main()

            {

                        int x=0, y=5;

                        y == 5 ? x=10 : x=20 ;

                        if( x )

                        {

                                    printf("%d ", x );

                        }                     

                        return 0;

            }

 

            A.        10

            B.        20

            C.        No output

            D.        Invalid lvalue is assignment

               

           


            Output:
                        D.        Invalid lvalue is assignment




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