Tuesday, October 23, 2012

Question 11:  Predict the output
    int main()
    {
        int x = 0;
        while(x++<5)
        {
            static int x;
            x += 2;
            std::cout<<x<<" ";
        }
    }


    Options:

    a)     1 2 3 4 5
    b)    2 4 6 8 10
    c)     Compile Time error
    d)    Runtime Error


    Answer: b

2 comments:

  1. This explains the concept of scope of variables or more accurately the concept of local variables.

    ReplyDelete
    Replies
    1. I would say it explains the scope of static variable.
      To see the difference remove static keyword from the above code and see the behavior.

      Delete