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.

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
192k+ students have already unlocked exclusive benefits with Test Prime!

Free Tests

Free
Must Attempt

Current Affairs (Jul 16, 2025 - Jul 23, 2025)

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

General Awareness Section Test 01

languageIcon English
  • pdpQsnIcon20 Questions
  • pdpsheetsIcon20 Marks
  • timerIcon8 Mins
languageIcon English
Free
Must Attempt

IB ACIO Grade-II Executive (Held on 17 Jan 2024 S1)

languageIcon English
  • pdpQsnIcon100 Questions
  • pdpsheetsIcon100 Marks
  • timerIcon60 Mins
languageIcon English