What is the output of the above program?
Palo Alto Networks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
#include <stdio.h>
int x=10;
int main(void) {
static int x=10;
x+=f1()+f2()+f3()+f1();
printf("%d",x);
return 0;
}
int f1()
{
static int x=25;
x++;
return x;
}
int f2()
{
int x=50;
x++;
return x;
}
int f3()
{
x=10;
return x;
}
What is the output of the above program?
Select any one of the following
Show answer & explanation
With static initial value 100 in main, f1() returns 26 and then 27 (static x), f2() returns 51, and f3() returns 10. Total addition is 26 + 51 + 10 + 27 = 114, resulting in 100 + 114 = 214.
Step-by-step Derivation:
Step 1: First call to f1(): static x is initialized to 25, x++ becomes 26, returns 26.
Step 2: f2(): local x is 50, x++ becomes 51, returns 51.
Step 3: f3(): assigns global x = 10, returns 10.
Step 4: Second call to f1(): static x was 26, x++ becomes 27, returns 27.
Step 5: Sum of returns = 26 + 51 + 10 + 27 = 114.
Step 6: Added to static x = 100 gives 100 + 114 = 214.