QUESTION 32 What will be the output of the program given below?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
QUESTION 32
What will be the output of the program given below?
#include <stdio.h>
int a = 40;
int main( )
{
int a, b = 5;
a = b + 0;
printf("%d", a);
return 0;
}
Show answer & explanation
Answer: B. 5
The global variable a = 40 is shadowed by the local variable a declared in main(). The local a is uninitialized when declared, but then immediately assigned b + 0 where b = 5, so a = 5. The printf outputs the local a, which is 5, not the global value of 40.
Step-by-step Derivation:
- Global scope:
int a = 40;declares global variable a - main() function scope:
int a, b = 5;declares local variables a (uninitialized) and b (initialized to 5) - Local variable a shadows the global a within main()
a = b + 0;assigns 5 + 0 = 5 to the local variable aprintf("%d", a);prints the local a, which is 5- Output: 5