hamburger menu
All Coursesall course arrow
adda247
reward-icon
adda247
    arrow
    arrow
    arrow
    What will be the output of the program?
    Question



    What will be the output of the program?

    A.

    7 7 7

    B.

    7 8 9

    C.

    8 9 10

    D.

    Compilation Error

    E.

    None of the above

    Correct option is B

    Let’s analyze the code:
    1. Static Variable Behavior:
    · The variable i is declared as static inside the function func().
    · Static variables are initialized only once, retain their value between function calls, and persist for the lifetime of the program.
    · i is initialized to 7 during the first call to func() and is not reinitialized in subsequent calls.
    2. First Call to func():
    · i = 7 (initialization happens here).
    · The value of i is printed: 7.
    · i is incremented: i = 8.
    3. Second Call to func():
    · i retains its value from the previous call (i = 8).
    · The value of i is printed: 8.
    · i is incremented: i = 9.
    4. Third Call to func():
    · i retains its value from the previous call (i = 9).
    · The value of i is printed: 9.
    · i is incremented: i = 10 (though this is not printed).
    Final Output:
    The program prints: 7 8 9
    Key Points:
    1. Static Variables:
    · Retain their value between function calls.
    · Are initialized only once during their first encounter in the program.
    · Persist for the program's lifetime.
    2. Function Calls: Each call to func() uses the updated value of the static variable i from the previous function call.
    Knowledge Booster:
    · Difference Between Static and Automatic Variables:
    · Automatic Variables: Declared without static, are reinitialized every time the function is called.
    · Static Variables: Preserve their value across multiple function calls.
    · Use Cases of Static Variables:
    · Counting function calls.
    · Retaining state information in recursive or iterative functions.

    Free Tests

    Free
    Must Attempt

    IBPS RRB Clerk Prelims Full Mock Test 01

    languageIcon English
    • pdpQsnIcon80 Questions
    • pdpsheetsIcon80 Marks
    • timerIcon45 Mins
    languageIcon English
    Free
    Must Attempt

    RRB Clerk Pre: Quant Section Test 01

    languageIcon English
    • pdpQsnIcon40 Questions
    • pdpsheetsIcon40 Marks
    • timerIcon20 Mins
    languageIcon English
    Free
    Must Attempt

    RRB Clerk Pre: Reasoning Section Test 01

    languageIcon English
    • pdpQsnIcon40 Questions
    • pdpsheetsIcon40 Marks
    • timerIcon25 Mins
    languageIcon English
    test-prime-package

    Access ‘IBPS RRB SO IT Officer (Scale-II)’ Mock Tests with

    • 60000+ Mocks and Previous Year Papers
    • Unlimited Re-Attempts
    • Personalised Report Card
    • 500% Refund on Final Selection
    • Largest Community
    students-icon
    253k+ students have already unlocked exclusive benefits with Test Prime!
    test-prime-package

    Access ‘IBPS RRB SO IT Officer (Scale-II)’ Mock Tests with

    • 60000+ Mocks and Previous Year Papers
    • Unlimited Re-Attempts
    • Personalised Report Card
    • 500% Refund on Final Selection
    • Largest Community
    students-icon
    253k+ students have already unlocked exclusive benefits with Test Prime!
    Our Plans
    Monthsup-arrow